Welcome Guest, Not a member yet? Register   Sign In
How to manage and control over cron jobs ?
#1

[eluser]Ravi Raj[/eluser]
We have seen some times that cron jobs overlap and possibly corrupt our data.make a library in codeigniter to avoid such overlaps by using simple locking techniques. However there can be various other methods of achieving the same thing. few links are http://timkay.com/solo/ & http://kevin.vanzonneveld.net/techblog/a...ns_in_php/

The helper class create a lock file inside LOCK_DIR.Lock file name suffix can be configured using LOCK_SUFFIX.MY_cron_helper::lock() places the current running cron job process id inside the lock file. Upon job completion MY_cron_helper::unlock() deletes the lock file.
If MY_cron_helper::lock() finds that lock file already exists, it extracts the previous cron job process id from the lock file and checks whether a previous cron job is still running. If previous job is still in progress, we abort our current current job. If previous job is not in progress i.e. died abruptly, current cron job acquires the lock.

Code:
TEST SCRIPT

<?php
class Welcome extends Controller {
    function cron()
    {
        $this->load->library('MY_cron_helper');
        if(($pid = MY_cron_helper::lock()) !== FALSE) {
        sleep(20);
        MY_cron_helper::unlock();
        }
    }
}
?>

Run from command line and check apache error log.

you can verify better if you run test script from command line and terminate the cron job in between by pressing CNTR+C. By pressing CNTR+C, we simulate the cases when a cron job can die in between due to a fatal error or system shutdown. In such cases, helper class fails to release the lock on this cron job.also run the cron job from two command line window.

Whilst the implementation above simply uses files, it could be implemented with shared memory (SHM/APC), distributed caching (memcached), or a database. If over a network, factors such as packet loss, latency etc can cause race conditions and should be taken into account. Depending on the application it maybe better to implement as a daemon. If your looking to distribute tasks amongst servers.
#2

[eluser]Unknown[/eluser]
Great one, I've added to my CI to control the mail queue cron job... Thanks
#3

[eluser]tkyy[/eluser]
instead of controlling cronjobs i have written some bootstrap file for codeigniter (this way apache doesn't have to bog down your server). and instead of cron, persay, i install screen to the linux machine and just control the screen instances of php processes (using codeigniter and exec()Wink running on an endless loop. i've done alot with applications requiring cron and have found this is the absolute best way to control threads (assuming you are unsetting variables etc). give it a try Smile




Theme © iAndrew 2016 - Forum software by © MyBB