I'm aware of cron jobs to execute commands at a certain time, but what if that time is not constant? For instance, suppose a user asks for a reminder email exactly 1hr after signing up for something, is there an easy way to go about doing this?
Timing is critical. I am actually trying to create AI that will essentially act on its own but only at variable points during the day. Any help would be appreciated!
You can use at to schedule jobs for specific times. cron is for repeating jobs, at is for one-shot/oddball interval ones. Both have a resolution of 1 minute, though, so you can't specify a start period with seconds granularity.
The command's available on both Unix/Linux and Windows.
Here a workable flow:
user Requests email in 1 hour
You insert into the a table action (action_id, time)
On the PHP server create a cron job to check the action in the action table every minute, then do the action that need to be done at that time
That is a simple example from the request. It might get a bit more complex then that.
EDIT : this suggestion might be good only if you need to be very precise with the time management!
if you dont wanna use the cron triggers and you are not comfortable with them here are two php scheduling libraries..
1) http://www.php.brickhost.com/
2) http://www.phpjobscheduler.co.uk/
Try them if you like:
Related
I came across a situation i want to trigger some code at specific time, i.e when user does booking, the freelancer must accept/reject the booking request, if he doesnt, after x duration (15* mins lets say) it would be rejected and user would get push notification. All code is done but currently im running a cronjob after each 1 minute which checks for any unresponded bookings and checks when their time (15mins, dynamic) passed so then I execute my code for afterward, it is not good i guess as its running db queuries over and over each minute.
I'm aware with laravel queue jobs as well but didnt see anything for me to run that job for a specific time only (i.e execute this job after 15mins, if it isnt responded, reject it)
have you looked at Queue delay?
https://laravel.com/docs/9.x/queues#delayed-dispatching
This sounds like what you are looking for, I would just trigger the queue and delay when they make a booking so it executes 15 minutes after.
Use scheduled tasks.
use App\Console\Commands\SendEmailsCommand;
$schedule->command('emails:send Taylor --force')->daily();
$schedule->command(SendEmailsCommand::class, ['Taylor', '--force'])->daily();
https://laravel.com/docs/9.x/scheduling#scheduling-artisan-commands
I'm new to this cronjobs and I want few emails (2k to 3k mails to be more precise) to be sent at specific time and date which are in the database table.Currently to achieve this, I'm calling my mail function file(sendmail.php) for every minute using cron job and comparing the current time and the time which comes from the db table, if true the mail will be sent.By doing this I'm afraid there will be some effect on the performance.
Can we schedule cronjob right after the insert query in the php script. So that I can pass those time and date variables to it?
Does calling the file for every minute in the cron job is a good practice? Will the performance get effected because my application will be used by 25 users at a time?
Although by calling the file for every minute achieves my task, but still want to know if there are any better ways.
Thank you in advance.
for every minute using cron
If you're firing off cron jobs every minute then you're doing some thing wrong. There are problems with jitter, and concurrency.
comparing the current time and the time which comes from the db table
Does that mean you are doing the tie check outside of the DBMS? That would be very silly.
Can we schedule cronjob right after the insert query in the php script
Yes, although you'd need to use sudo to create privilege separation. However you having (potentially) thousands of cron jobs is a very bad idea.
While there is a lot missing from your problem statement, based on what you have said, I'd suggest having a cron job running once every (say) 15 minutes, polling the database for the emails to be sent in that time window - with the time comparison and concurrency locking done in the database.
I'm currently working on a browser game with a PHP backend that needs to perform certain checks at specific, changing points in the future. Cron jobs don't really cut it for me as I need precision at the level of seconds. Here's some background information:
The game is multiplayer and turn-based
On creation of a game room the game creator can specify the maximum amount of time taken per action (30 seconds - 24 hours)
Once a player performs an action, they should only have the specified amount of time to perform the next, or the turn goes to the player next in line.
For obvious reasons I can't just keep track of time through Javascript, as this would be far too easy to manipulate. I also can't schedule a cron job every minute as it may be up to 30 seconds late.
What would be the most efficient way to tackle this problem? I can't imagine querying a database every second would be very server-friendly, but it is the direction I am currently leaning towards[1].
Any help or feedback would be much appreciated!
[1]:
A user makes a move
A PHP function is called that sets 'switchTurnTime' in the MySQL table's game row to 'TIMESTAMP'
A PHP script that is always running in the background queries the table for any games where the 'switchTurnTime' has passed, switches the turn and resets the time.
You can always use a queue or daemon. This only works if you have shell access to the server.
https://stackoverflow.com/a/858924/890975
Every time you need an action to occur at a specific time, add it to a queue with a delay. I've used beanstalkd with varying levels of success.
You have lots of options this way. Here's two examples with 6 second intervals:
Use a cron job every minute to add 10 jobs, each with a delay of 6 seconds
Write a simple PHP script that runs in the background (daemon) to adds an a new job to the queue every 6 seconds
I'm going with the following approach for now, since it seems to be the easiest to implement and test, as well as deploy on different kinds of servers/ hosting, while still acting reliably.
Set up a cron job to run a PHP script every minute.
Within that script, first do a query to find candidates that will have their endtime within this minute.
Start a while-loop, that runs until 59 seconds have passed.
Inside this loop, check the remianing time for each candidate.
If teh time limit has passed, do another query on that specific candidate to ensure the endtime hasn't changed.
If it has, re-add it to the candidates queue as nescessary. If not, act accordingly (in my case: switch the turn to the next player).
Hope this will help somebody in the future, cheers!
I am trying to create a site in PHP and MySQL where a person is assigned to do a task for another person. Time period allotted to complete the task is 72 hours.
If done, the other person will confirm so. If not, I want to take certain actions (like blocking his account, sending an email, assign the task to someone else etc.) What is the best way to do this?
As the count-down runs second-by-second, I guess I will have to run a script every second.
You could use a cronjob to check for the deadline and eventually send the email, block the account, etc...
Although, if you actually need a persecond precision, you might want to use a single process with an "infinite" cicle and some sleeps in between.
If your server is from *nix family, you could use cron/crontab. See examples of usage at: http://en.wikipedia.org/wiki/Cron and https://help.ubuntu.com/community/CronHowto
Very few things have to happen truely immediately. If you've waited 72 hours for something to happen, having an action occur within a matter of a few minutes will not matter so much.
If you really do want something to happen so quickly, and you will always have a long time between potential events, one thing I would suggest is not using a cronjob, but something more akin to what #muc has suggested - a script that runs regularly within a shell script that will keep it going (just doing an 'exec $0' at the end to rerun the script when it would exit).
The script (in PHP for example) that checks the database doesn't have to be running all the time either. Whenever it finds something that is due to happen in more than a few seconds time, it sleeps till the next event is due to happen. When it wakes up, it can double-check that it is still required, and then performs the relevant job.
<?php
$jobId = whatToRunNext();
if (! $jobId) {
exit;
}
$secsToNextJob = howLongToNextJob($jobId);
if ($secsToNextJob > 0) {
sleep($secsToNextJob);
if (! jobStillRequired($jobId)) {
exit;
}
}
doJob($jobId);
exit;
Wrap that in a shell sript that will keep it running, maybe with a small sleep in the bash script, and you'd be good - while not being in a very long-running, cpu-killing loop.
If I needed to do something like that, I would probably go by these steps:
Create event and calculate deadline
Set up a cronjob to run at the deadline time
cronjob checks the event status and takes actions
I think this will be the most efficient way in doing it.
I am trying to create a site in PHP and MySQL where a person is
assigned to do a task for another person. Time period allotted to
complete the task is 72 hours.
These could simply just be db vales
table_task
.id
.user_assigned
.strart_date
.end_date
.status
.desc
If done, the other person will confirm so. If not, I want to take
certain actions (like blocking his account, sending an email, assign
the task to someone else etc.) What is the best way to do this?
This again, Admin panel of some form with a layout of assigned jobs, the ''worker'' should be required to post a job as ''done'', the ''admin'' can then confirm this. This can all be managed via Values in the db ''table_task.status''
As the count-down runs second-by-second, I guess I will have to run a
script every second.
^-- server suicide
Run the cron ever hour at most. If you have 1000 users. Then this cron will run 1000 a sec
in an hour this is, 60000*60
If you need to display the time use some jQuery of some form. There are lots of clock scripts.
I need to develop a website that runs jobs at certain times (every 15 minutes) I'll use a cron to run the webpage.
What is the best way to store the jobs information in? Some jobs will be daily, others every 8 hours etc. To complicate matters, I also need to take into consideration the timezone differences. This shouldn't be too difficult as PHP has many timezone functions, but how do I integrate it in the programming of the next job to run?
Another question, is how will the user enter the information for the jobs to run? One option, similar to http://www.webcron.org, is to ask the user to select how often the jobs must run, but how do I store that info in a database?
Any assistance will be greatly appreciated!
I would use the crontab syntax, store it in a DB and run a separate crontab invoking the execution script every minute.
There are GPL classes that do that for you already, like : http://www.phpclasses.org/package/4140-PHP-Database-driven-PHP-job-scheduler-like-cron.html
Create a MySQL Event, that will take care also the storing, recurrence, execution.