Welcome Guest, Not a member yet? Register   Sign In
Where is the Best Place for Cron Methods ?
#1

Hi, i've a cron controller that calls its own methods. Pretty common. Now : these methods calls others methods like clean old entry from others coltroller/model processes. These others methods:
  1. not requires authentication
  2. not requires views (obviously)
So, Where is the Best Place for these methods ?

  1. Cron Controller
  2. Parent's methods controllers
  3. involved models
which ones ?
thanks
Reply
#2

Individual user cron files are kept in /var/spool/cron, and system services and applications usually keep their cron job files in /etc/cron.
Reply
#3

(02-04-2022, 05:20 AM)Kerry Watson Wrote: Individual user cron files are kept in /var/spool/cron, and system services and applications usually keep their cron job files in /etc/cron.

Ok, you haven't read the question or you doesn't understand the question...maybe both.
Reply
#4

(This post was last modified: 02-16-2023, 02:51 PM by evansharp. Edit Reason: clarity )

You should follow a DRY (Don't Repeat Yourself) approach to these methods. If they're actions only performed by the cron job via you cron controller, then I'd say writing a private cron controller method to perform them makes sense. If these are methods that properly belong in another class because of the resources they need or semantics, then you could look at calling them statically from the cron controller or else instantiating their class in the cron controller.

In any event, don't create duplicates of the methods. Put them somewhere according to where they fit. Namespacing makes this pretty painless.
Reply
#5

it sounds like the methods you're referring to are related to database maintenance tasks, such as cleaning up old entries. Since these methods don't require authentication or views, it's best to keep them separate from your controllers and views.

Here are a few options for where you could place these methods:

Models: If the methods are directly related to database operations, you could consider placing them in the relevant models. For example, if you have a "User" model and a "Post" model, you could create methods in each model to clean up old user and post entries, respectively.

Helper classes: Another option is to create a separate helper class specifically for these maintenance tasks. This would allow you to keep the methods separate from your models and controllers, making them easier to manage and maintain.

Custom command: Finally, you could create a custom command that can be run from the command line. This would allow you to automate these maintenance tasks and run them independently of your application's controllers and views.

Ultimately, the best approach will depend on your specific application architecture and needs. However, in general, it's a good idea to keep maintenance tasks like these separate from your main application logic, to avoid cluttering your controllers and models and making it more difficult to maintain your codebase.
Reply
#6

I think you can create a cron API controller that contain all your cron task as individual controller action.
Then you can schedule the job using tools like hangfire base on your need.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB