We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Right way of carrying some cron task

This is more like a logic question than coding, I have been in to programming but procedural way, opted for Phalcon to wrap my head around in OOP as well this great framework. Been reading a lot about oop and stuff including design pattterns, fancy programming terminology etc..

So here is a task I need to to , made a small application which needs to send out notification regulary on following basis until its completed.

  1. Daily
  2. Weekly
  3. Monthly

So, a table with lastRun and status of completion is made. Keeping it simple, have plan to check notification those are NOT COMPLETED and on the basis of lastRun , notification will be sent out.

Now need to know correct way to do it in Phalcon. I made a controller "CronController.php" where indexAction() run query to pull out data of pending notificaitons.
It has following so there is no output when its executed:

 $this->view->setRenderLevel(
            View::LEVEL_NO_RENDER
        );

That data now need following operation: Find type of Run ie. daily , weekly, monthly Check lastRun time, if daily ,then if not sent out in last 24 hours, SEND It. if Weekly, not sent out in last 7 days, days, SEND it. if Monthly, not sent out in last 30 days / month , SEND it.

And then update lastRun time with current time. Ofcourse, it will need some validation if notification was successful , only then update lastRun etc.

I am tempted to use switch / case to carry it out. But I feel that I guess there could be some classy OOP way to do it. If its overkill then still want to know it. Need to know what way to do in Phalcon , should I add another method in CronController.php , but that would be bad as Phalcon associate it with someAction. Should I carry out above logic in another library file and use results here ?

edited May '18

switch control structure is still heavily used in OOP.

In general, whenever I have upfront defined time for cron tasks such as yours - the most KISS is to use GNU/Linux crontab:

@daily php /path/to/yourApp/cli.php 1 argsIfAny

@weekly php /path/to/yourApp/cli.php 2 argsIfAny

@monthly php /path/to/yourApp/cli.php 3 argsIfAny



85.5k

i am not sure really, and i am not sure if i am correct. but all my crons are in my cli folder/app. Its extra work , but hey that's what an cli app is for ( i think ) :-)



8.8k

i am not sure really, and i am not sure if i am correct. but all my crons are in my cli folder/app. Its extra work , but hey that's what an cli app is for ( i think ) :-)

Yes, I have seen that but right now I decided to go for turning of OFF view. Besides, this one is more a learning experience than real use.



8.8k
Accepted
answer

switch control structure is still heavily used in OOP.

In general, whenever I have upfront defined time for cron tasks such as yours - the most KISS is to use GNU/Linux crontab:

@daily php /path/to/yourApp/cli.php 1 argsIfAny

@weekly php /path/to/yourApp/cli.php 2 argsIfAny

@monthly php /path/to/yourApp/cli.php 3 argsIfAny

May be I was not able to explain, I can't divide task in monthly , daily or weekly cron separately , its because a task created today will have month after 30 days, however a task created 10 days back will have a month after next 20 days.

Wanted to keep in single cron job with which will run daily , I will go in indexAction() to do all stuff. May be I will it first and share here to see what can be improved to make it proper OOP and Phalcon way. (avoid CLI bootstrap for now).

Crontab is the solution, just set your 3 crons daily then you'll get proper separate processes but be carefull about the datetime cron server and database server if they're not the same.