CodeIgniter Forums
Hey guys - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Hey guys (/showthread.php?tid=37623)

Pages: 1 2


Hey guys - El Forum - 01-15-2011

[eluser]paulipv[/eluser]
I develop a hosting automation control panel with codeigniter.

However i need some suggestions.

As type says "automation" there are many tasks that have to run.

Multiple cronjobs can be a sollution but system allows to create random tasks.

So i've thinked to combine cronjob and php forking.

- Only 1 cronjob that will verify if daemon is running
- A daemon file what will read the task table and execute tasks (multiple childs).

For example we have CI app cron

Task table schema looks like:

- task_id
- task_name
- task_run_every ( Day|Week|Month|Time)

If "Time" is selected then you can schelude a task to run every 5 minutes, every 10 minutes, etc.

- task_run_time (applied only for EveryDay|EveryWeek|EveryMonth)

- task_sch_date (when task is schelude to run)
- task_last_run
- task_path (for example http://domain/cron/controller/method)



What you think about that ?


Hey guys - El Forum - 01-17-2011

[eluser]nilesh+[/eluser]
Overall looks okay however if anticipated task runtime of an instance of task is anywhere close to the lowest desired frequency you could make sure that a previous instance of a given task [controller/method] is not already running. Ignoring this will not affect in many trivial cases but I'd check it nonetheless.


Hey guys - El Forum - 01-17-2011

[eluser]paulipv[/eluser]
Yup there will be a field that will be marked "In Progress" when a task is running.


Hey guys - El Forum - 01-17-2011

[eluser]smilie[/eluser]
Hi,

Do take in account from the very start a good error handling.
I have build similar process (also for hosting automation), but very first version of such cron did not handle error well. By this, I mean: task starts and marks 'in progress' so that another instance can not start. But, in execution of the task_path (script itself) it resolves in 'critical' error and PHP 'crashes'. This then means that 'in progress' will remain in database and no new (next) instance will start until you manually set it from 'in progress' to 'available' (or what ever).

So, try to handle as many errors as you can to prevent this. Mainly in weekend it can be pain in the ass, then on Monday script has to do quite a lot work to catch up :-)

By the way I have also placed all that in a 'flow' - so I have only 1 cron which takes all actions (task_path) and goes one by one on all of them.

Cheers,
Smilie


Hey guys - El Forum - 01-17-2011

[eluser]paulipv[/eluser]
True.
System should have a very good error handling.

I have an external helper that run in cronjob and verify daemon.
Also daemon generate database logs and /var/log/ logs.


However if you dont specify an external path, jobs will be handled in daemon.

For every job a new thread will be created, so if we have 3 jobs that run every minute will work simultaneously.

However system allows you to limit the maximum of instant processes, so if you limit to 50 workers,
then when limit is reached parent will wait for every child to complete.



Another concept is to make an job deamon. (if we have more then 100 crons =) )


Hey guys - El Forum - 01-17-2011

[eluser]smilie[/eluser]
Well, that's what we did (job deamon) which is placed in cron.
We store all actions per defined object. These actions have defined path/script.php

Deamon then looks up all actions (that it may process) in the database and executes one by one all of them. Deamon is set up to run every 15 minutes (as new actions appear).

This way, only 1 cron at 1 time is running. But it's a b***h if it crashes as by next run it has to do a lot more :-)

Cheers,
Smilie


Hey guys - El Forum - 01-17-2011

[eluser]paulipv[/eluser]
Se attached image.


Hey guys - El Forum - 01-17-2011

[eluser]paulipv[/eluser]
You have better solution than this for detecting?

Mine:

Example:
*/15 = Every 15 minutes
*/2 = Every 2 minutes

Mine:

Code:
$type    = "*/20";
$current = date('i');

if (eregi("\*/", $type)) {
  
  $now = FALSE;

    list($k, $min) = explode('/', $type);

    if (($current % $min) == 0) {
        $now = TRUE;
    }
}



Hey guys - El Forum - 01-17-2011

[eluser]smilie[/eluser]
I simply place an DB value;
0 - not running;
1 - running;

Problem with that approach is if cron 'crashes' it leaves 1 in DB and next process never starts.
However, we receive e-mail if status '1' is longer then 30 minutes so we can check what's going on.

Cheers,
Smilie


Hey guys - El Forum - 01-17-2011

[eluser]nilesh+[/eluser]
[quote author="smilie" date="1295302945"]I simply place an DB value;
0 - not running;
1 - running;

Problem with that approach is if cron 'crashes' it leaves 1 in DB and next process never starts.
However, we receive e-mail if status '1' is longer then 30 minutes so we can check what's going on.

Cheers,
Smilie[/quote]


Solution to this is to double check with an equivalent of 'ps' command [exec, system etc.] to see if indeed you can grep controller/action for status = 1. If you can't its safe to reset.