CodeIgniter Forums
Where is the Best Place for Cron Methods ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Where is the Best Place for Cron Methods ? (/showthread.php?tid=81195)



Where is the Best Place for Cron Methods ? - RedWd - 02-03-2022

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


RE: Where is the Best Place for Cron Methods ? - Kerry Watson - 02-04-2022

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


RE: Where is the Best Place for Cron Methods ? - RedWd - 11-19-2022

(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.


RE: Where is the Best Place for Cron Methods ? - evansharp - 02-16-2023

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.


RE: Where is the Best Place for Cron Methods ? - SharphSonirtm - 03-27-2023

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.


RE: Where is the Best Place for Cron Methods ? - Soliu - 06-19-2023

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.