So I have cron jobs running every minute. These cron jobs all finish within 30 seconds. However, my last cron job is random and only starts working when it finds the data in the system. Sometimes there is a lot of data and the cron job usually takes about 10 minutes to finish, which I am perfectly fine with. However, once this cron job initiates and the minute passes, the other cron jobs aren't started until the cron job is done. So I have about 15+ cron jobs waiting for that one cron job to finish and then they start up again.
I used withoutOverlapping and it doesn't seem to work? Is there a solution?
Here is a list of my jobs:
$schedule->command('sms:schedule')->everyMinute();
$schedule->command('sms:sendbulk')->everyMinute();
$schedule->command('sms:notifyevent')->everyMinute();
$schedule->command('voice:sendbulk')->everyMinute();
$schedule->command('email:sendbulk')->everyMinute();
$schedule->command('email:schedule')->everyMinute();
$schedule->command('voice:schedule')->everyMinute();
$schedule->command('sms:sendrecurringsms')->everyMinute();
$schedule->command('sms:sendrecurringvoice')->everyMinute();
$schedule->command('email:sendrecurringemail')->everyMinute();
$schedule->command('event:recurring')->everyMinute();
$schedule->command('senderid:validation')->dailyAt('12:01');
$schedule->command('invoice:recurring')->dailyAt('12:01');
$schedule->command('reports:daily')->dailyAt('06:00');
$schedule->command('checksms:status')->everyMinute();
$schedule->command('checkroom:status')->everyMinute();
$schedule->command('location:minute')->everyMinute();
$schedule->command('location:checkcircle')->everyMinute();
$schedule->command('recurring:setsmsname')->everyMinute();
$schedule->command('recurring:setvoicename')->everyMinute();
$schedule->command('site:schedulingevents')->everyMinute();
$schedule->command('sms:contactnameset')->everyMinute();
$schedule->command('daily:checklocation')->withoutOverlapping();
That last one is the one that has differentiating times. But I need the other ones to keep running every minute if that is the case.
Related
Currently, I have done the following:
I created one scheduled task which runs daily to get the Scheduled time from Mysql DB for the currentdate and store it into the .txt file
SELECT workflow_id, DATE_FORMAT(schedule_datetime,'%H:%i')TIMEONLY FROM scheduling_event
where DATE(schedule_datetime) = CURDATE()
Created one more scheduled task that runs each 5mins to check if the scheduled time present in the .txt file matches the CURRENT TIME if yes then it calls the scheduled_program.php file.
The issue here is - this is not an efficient way if nothing is scheduled on the current date. So Is there any way to create/update a dynamic scheduled task instead of running each 5mins? ie: the first scheduled task will run and take the scheduled time on the current date then it will create a task based on the scheduled time. if the day ends delete all the scheduled tasks for the day.
Note: Number of the scheduled task is not fixed. imusing Windows 10, php7.
I am trying to achieve, run a scheduled_program php file on schedule Date and TIME
It looks like you're trying to solve a problem that's not serious. I guess you don't want to waste your computer's time running a cron job every five minutes if it has nothing to do.
But here's the thing:
a cron job
that runs a php program
that does a single query
to retrieve a list of workflows
and run them
has negligible cost if you run the cron job every five minutes, or even every single minute, and there are no workflows to run.
On the other hand, debugging and troubleshooting cronjobs is hard, especially in production.
So, I respectfully suggest you keep this system as simple as you possibly can. You will have to explain it over the telephone to somebody in the middle of the night at least once. That's the unfortunate truth of scheduled tasks. The dynamic scheduled task system you propose is not simple.
I'm in need of a way to create dynamic, one-off cron jobs to execute tasks at different times. Ideally, I would like to achieve this using PHP, so that when a user completes a certain action, the cron job is created and scheduled for a time that is calculated based on the time that the user completes said action. At any one time, there could be multiple cron jobs scheduled at once for different times. These cron jobs also need to be deleted upon completion.
I have tried searching around for something appropriate, however haven't encountered anything that works as I need. If anybody could point me in the right direction, that would be greatly appreciated.
Cheers
A possible solution:
Setting a Cron job that calls to CronJobManager.php every second.
Just make a regular daemon that calls for the CronJobManager.php.
Create a cronjob table in your database
The cron job table should contain these basic fields: path (to php file), run_time(datetime), last run (datetime) and type (like suicidal, if as you explain you want some cron jobs to delete themselves)
Connect CronJobManager.php with the cronjob table
Every time CronJobManager.php runs (that is, every second), loads the cron jobs. Then, comparing "now"'s time with each cron job's run_time you'll get which cron jobs to run.
For example, if cron job "foo" run_time is set to 18/04/2014 22:02:01, CronJobManager will run it when reaching that moment.
Notice that if Cron jobs executing time needs a lot of time, they'll get delayed and eventually a second or two will get lost.
Now, for every cron job that needs an execution, you would execute the related php file of that cron job, indicated in the path.
This is a general idea, and of course you would have to extend it with for example cron job states (idle, running, stop, etc).
In order to delete cron jobs you would implement this feature in the cron job object.
That is: the Cron Job class, once it has executed what it had to do, it would check its type (as defined in database). If it is 'suicidal', then it would delete the database row.
UPDATE
I updated the answer but I want to note something. If what you need is several cron jobs to run at once, in a specific second with 0 delay, then you need a cron job per task out of php that runs a specific file.
To achieve this functionality, you need to have a daemon that is running all the time that checks for these dynamic jobs and launches them. The setup is a tad complicated to put together, but if you are ready for such an endeavor, you can use the project PHP Resque Scheduler.
https://github.com/chrisboulton/php-resque-scheduler
You start up a daemon that runs all the time and then you can add jobs to a dynamic queue to be executed at any specified time in the future. I think you will find this suitable to everything you are looking to do.
I have a question in cron jobs where i create a job to run a page every one minute.
If this page didn't finish its job completely (not executed completely) in this minute, then
will the cron job command run this page again from the beginning?
or will it run this page again but will allow the first to complete?
or will it wait for the page to complete and do the command?
or will it do some thing else????
Every cron job is run independently in a separate sub process, so the cron job will run irrespective of whether some other job is running or not.
Hence, if your script is taking more than one minute to run, and you have scheduled it to run every minute, it will have unintended consequences because two process, or n+1 process for script execution time in range [n, n+1) minutes, will now be running in different execution stage.
This will be true for any overlaps in execution time of cron jobs.
I have a php script that I'd like to run for a specific amount of time (e.g. 5 minutes), but only once. With cron jobs this will run indefinitely. Is there another way?
The way to handle this is:
When something triggers to need for the cron job to run, put a flag somewhere that the cron job will read.
Have cron job that runs all the time asking "do I need to run?". It checks hte flag. If it sees it, it deletes the flag and runs for specified time. If not, just wait until it next runs (to check for the flag)
In the actual cron job, set "max_execution_time" to 5 minutes. Then it shouod stop at (or just over) 5 minutes. To be more precise, include a timer in your script running in a loop, if you can.
I have a Cron Job with PHP which I want to set up on my webhost, but at the moment the script takes about 20 seconds to run with only 3 users data being refreshed. If I get a 1000 users - gonna take ages. Is there an alternative to Cron Job? Will my web host let me run a cron job which takes, for example, 10 minutes to run?
Your cron job can be as long as you want.
The main problem for you is that you must ensure the next cron job execution is not occuring while the first one is still running. You have a lot of solutions to avoid it, basically use a semaphore.
It can be a lock file, a record in database. Your cron job should check if the previous one is finished or not. A good thing is maybe sending you an email if he cannot run because of a long previous job (this way you'll have some notice alerting you that something is maybe getting wrong) By default cron jobs with bad error dstatus on exit are outputing all the standard output to the email of the account running the job, depending on how is configured the platform you could use this behavior or build an smtp connexion on the job (or store the alert in a database table).
If you want some alternatives to cron jobs you should have a look at work queues. You can mix work queues with a cron job, or use work queue in apache-php envirronment, lot of solutions, but the main idea is to make on single queue of things that should be done, and execute them one after the other (but be careful, if you handle theses tasks very slowly you'll get a big fat waiting queue).
A cron job shouldn't have any bearing on how long it's 'job' takes to complete. If you're jobs are taking 20 seconds to complete, it's PHP's fault, not cronjob.
Will my web host let me run a cron job which takes, for example, 10 minutes to run?
Ask your webhost.
If you want to learn about optimizing php scripts, take a look at Profiling PHP Code.