Creating multiple cron jobs with PHP - php

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.

Related

Laravel: Schedule a job or an artisan command?

I have a Product model with id,name,price.
The price value is stored in an external API and i need to fetch it every minute in order to update it in the database.
Looking through the Laravel documentation I found two ways to implement:
Create an artisan command (https://laravel.com/docs/8.x/artisan) and add it to task scheduling (https://laravel.com/docs/8.x/scheduling#scheduling-artisan-commands)
Create a job (https://laravel.com/docs/8.x/queues) and add it to task scheduling (https://laravel.com/docs/8.x/scheduling#scheduling-artisan-commands)
First of all, is there any other approach i should take in consideration?
If not, which one of the above would be the best approach and why is it correct for my use case?
As per my comments on one of your previous questions on this topic, whether you use a queue or not depends on your use case.
An Artisan command is a process that executes once and performs a task or tasks and then exits when that task is complete. It is generally run from the command line rather than through a user action. You can then use the task scheduling of your command's host operating system (e.g. a CRON job) to execute that command periodically. It will faithfully execute it when you schedule it to be done.
A Queued job will execute when the Job turns up next in the queue, in priority order. Let's say you send your API call (from your other post) to the queue to be processed. Another system then decides it needs to send out emails urgently (with a higher priority). Suddenly, your Job, which was next, is now waiting for 2000 other Jobs to finish (which might take a half hour). Then, you're no longer receiving new data until your Job executes.
With a scheduled job, you have a time critical system in place. With queues, you have a "when I get to it" approach.
Hope this makes the difference clearer.
With laravel it is a lot easy to use the built in scheduler. You have to add only one entry to the crontab and that is to run the command php artisan schedule:run EVERY MINUTE on your project. After that you dont have to thing about configuring the crontab on the server, you just add commands to the laravel scheduler and they will work as expected.
You should probably use Cron Job Task Scheduling which would be the first approach you mentioned.
Commonly for this type of use-cases commands are the easiest and cleanest approach.
There are a few things to do in order to make it work as expected:
Create a new command that will need to take care of hitting the endpoint and storing the retrieved data to the database
In Kernel.php file register your command and the frequency of running (each minute)
Run php artisan schedule:run
You can read more about how to create it here:

anyway to dynamically schedule to run cron job using PHP

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.

Handling mass cron jobs

I have a website that users create their own tasks to be run at a specific time.
At the moment, when the user clicks create task it will add to the database table 'tasks'.
I then have a cron job, which runs every 15mins. It gets all the entries in the table and checks first whether it has been run today using the date time. (every midnight, another cron runs through and resets all these values). If it has not ran this day, it will then run it and afterwards change the value to ran.
The cron job does this by running php file on my server that checks for tasks, and then uses a simple foreach statement.
At the moment, there are only 11 users and my server seems fine. I am wondering what will happen with lets say 10,000 users.
Is this an efficient way to handle thousands of cron jobs? How yould you go about running this?

Run scheduled job in PHP

I am using Cakephp.
I want to run scheduled jobs.
Here's the situation:
User set time for some task on UI (say 1 week).
When the time is over, I want to execute some specific task. Meanwhile, the user can also change the time, and the task should be executed at updated time.
What is the best way (and reliable) to achieve this?
PS: Complexity is not the issue. But the task must always run after specific time under all circumstances.
Set the execution date in your table in a field
Set a status (pending) as well
Run a cron job that runs a CakePHP shell every X seconds or minutes, whatever you need OR create a shell that keeps running all time and check the records every X seconds in a loop.
The shell will process tasks that are configure for an execution date lower than the current date
Set the status to success or failed depending on the outcome
It's up to you how you want to handle failed tasks and if it's OK if a task executes 10secs later than configured or 10 minutes. There are multiple factors that play into this: Your interval of the cron job / query against the table. Do they have to processed in parallel? Is it OK to process them after each other? Your information is to vague.
The way to do that in CakePHP is to create a shell and run it with Cronjobs.

Limit to cron job with PHP

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.

Categories