After some research, I haven't found anwers for what I'm trying to achieve.
I found some information about the php sleep() function, and some inforamtions about CRON jobs, but none of these options seem to solve the problem.
Here is what I try to do : I have a php file which may receive webhooks. I need to wait some time (15 minutes for example) before "reacting" to this webhook. So, basically, my script should :
1 - receive the webhook (Already done with current code)
2 - wait some time
3 - do some actions (Already done)
I've already done what I needed without the wait part, and it works very well, bu now I don't know how to do with it...
If I understood it well, Cron Jobs are executed periodically, while I want to wait some time only when the webhook is received.
I thought about the sleep() function, but I'm afraid it may use to much ressources...
How can I do this ?
Update : It seems I can't use the sleep function more than 30 seconds (max execution time I guess ?)
If I was you, I'd build a queueing system so that when a webhook comes in, a row in a database is inserted. I'd add a column for 'execution_time' and set that to 15 mins from when the webhook came in. You can then setup a cron job that runs every minute but only fires where the execution_time is 'now'.
use cron
“At every 15th minute past every hour.”
Minute Hour Day Month Weekday Command
15 * * * * php /homelink/path/filename.php
Note:
You can set an email address for the cron. Every trigger it will send an email notification. If receive email means cron is working, If not then need to check your code. Also you can debug using the appropriate echo's.
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 am making web application in PHP. I need to send mail to my users after every interval of time (say after every two days). How can i keep track of time intervals in php (assume i know the start time).
You could create a cron job that runs one of your php scripts locally, and have that script check against a database to see whether or not the time is appropriate to send the email. For example,
In your crontab:
0 0 */2 * * php /usr/bin/mailcheck.php
And in mailcheck.php, just have logic to check for entries in a database that are overdue for an email.
Or, if you just want to blindly send an email to everyone every interval, just have a sendmail() entry in mailcheck.php, and that'll do it too.
I have a PHP web page which serves the RSS feed, but it takes about 15-20 seconds to generate a response (which then will be cached for 10 minutes on the server for faster responses).
How could I set a cron job timing for this operation? I am having problem with this. I think if I call the page before 10 minutes it will run cached page so I won't get latest updated page, is this true? And if I call that page after 10 mins then will I have to wait for 15-20 seconds to get a response?
How do I manage to make this process where I will get updated feed with swift response? I haven't tried cron job before, this is my first time, so i find this confusing.
My cron command is : */10 * * * * wget http//www.example.com/multifeed.php
Is it right?
You won't have a perfect cron to bring the fresh data as soon as it's available, That's a limitation you'll have to live with I think.
What I would do is run this cron every 2 minutes and try to get new data, I would check to see if the last update is different than what I already have, and if it is, update the file, if it's not, do nothing and wait for the next cron.
This method will provide at most two minutes of stale data.
Another option is to check the mtime of hte file : http://php.net/manual/en/function.filemtime.php
Basically, I visit your page, We check the mtime of the file, if it's greater than 10 minutes, we fetch fresh data, this, combined with the cron, can provide a way for users to always see fresh data. If the freshness of the information isn't that important(can you live with two minutes of stale data?), if it's not that important, simply run the two minute cron.
Hope it helps.
I have a website in PHP and users can schedule message to be sent. I can sent message with command similar to this one:
php sendMsg.php 249
where number is ID of the message
Many people suggested to use cron jobs, but since I don't want to run this in interval cron is no option(only once - for example after 3 hours).
My idea was as follows:
$seconds = $hours*60*60;
exec('sleep '.$seconds.'; php sendMsg.php 249');
But this wont work because it will block php for further executing. What is the simplest way to achieve this?
You said you don't want to use a cron job because you only want the message sent once, but this is mis-understanding the way that a cron job would be written for this kind of task
Consider a situation where you have many users creating many messages to be sent at various given points in time.
You don't want to have a PHP program sitting running on your server all that time for each of those messages; it would be wasteful of server resources, even if they were all just sleep()ing for the duration.
Instead, one would use a cron job to run a short-lived PHP program once every minute (or whatever interval suits you).
Your message creation program would not be written to acually send the message; instead it would insert it into a database, along with the time it needs to be sent.
Meanwhile, the cronjob PHP program would scan this database every minute to see if there are any messages that are due to send but have not yet been sent. It would then send those messages and mark them as 'sent' on the DB.
This is the standard way to write this kind of thing, so it's not surprising that people are recommending it to you.
Doing it this way means that you never have a program running on your system for longer than necessary. Both PHP programs do their job quickly and exit, meaning that no-one is kept waiting for them.
It also makes it much more robust. Imagine if your server had to be rebooted. If you had a bunch of PHP programs running for hours waiting for their moment to send their message, they'd all be lost. On the other hand, if they had saved their message to a DB, the cron job would find them and send them correctly once the server was restarted.
Put the schedule in a database. Run a cronjob every minute or so, check the database if a message should be sent within this minute, and send it.
Is there a reason you don't want to use a cron job? That would be the simplest and most efficient way of sending the messages.
I would think that a cronjob ist still the right way
Create a table where the to be send messages are stored, with a timestamp when to be send and a flag for isSend
Create a cronjob - start php skript every 1 minute , which sends the messages with timestamp < current time and isSend = false
Ignore suggestions of cron, if you want to simply wait a period of time then use the at scheduler:
$hours = 2;
$command = sprintf('echo "php sendMsg.php 249" | at now + %d hours', $hours);
exec($command);
I have a PHP-based site with bookings/appointments stored in a MySQL database.
I want to set an e-mail notification to be sent to each person who made a booking exactly 24 hours before their booking. Bookings are added through a regular PHP form.
I know I could add a script to, for instance, the index page that checks the database for any bookings that are in the final 24 hours, but that's unreliable since I won't be getting much traffic at first. So the index page could go hours without visits, and the notification would be hours late.
The other solution that came to mind is to set a cron job that runs every minute, calls a PHP script which checks whether any e-mails should be sent and sends them. But I'm not sure if this is overkill in a way; does anyone have a better solution than having something run in the background every minute?
To sum it up - is there a way to do this without cron?
Triggering the job from a web page is a very bad idea, for two reason: (1) if you don't get traffic to your site, the job doesn't run; (2) if you get a lot of notifications, the job will be slowing down the response to the web requests (assuming you invoke the job synchronously).
I would strongly discourage you from running a job every minute either - it definitely will be an overkill. Instead, think whether you really need "exactly 24 hours" as the interval or would "between 22 and 26 hours" be ok.
We have a similar requirements - and went about it by setting a job that runs every 4 hours and checks what notifications need to be sent for events starting between 22 and 26 hours form the time the script runs. This way, the script is only execute 6 times in a day and everything gets sent correctly.
If 4 hours approximation is not good enough, then think to the largest interval that's appropriate. I'm sure 1 hour should be sufficient. Have a script run once an hour (from cron) and check for events starting between 23 and 24 hours from the time of the run.
Remember that once your email is sent, it doesn't end up in the recipient's inbox immediately: sometimes it takes a few seconds, but sometimes it may take an hour or even more - so an extra hour difference in your script won't be a problem.
You don't need to use cron as an interval'd timer. You can set a very specific date and time when you want your job done.
Here's an article on the subject
For instance:
0 0 18 5 * <php command here>
Will run every May 18th at midnight. That's more than enough time to clear it before the next iteration (next year).
there is no way other than setting cron or sending request to server through periodic calls...below is post similar to your question, you may get idea.
Live redirect based on periodic server calls with JSON or AJAX
Thanks
a cron job every minute has no sense ! but you can do a cron job every hour because i think it doesn't meter a hour difference or 2 hours . without cron it isn't any other way . it will take about 2 second (at max) to complete so it is worth