Scheduled cron job to check for pending activity - php

Using PHP ...
This is for my personal use so I'm thinking maybe 3-4 emails a day.
I'm at a point where I can send an email to a dedicated email address where my script parses the message and stores it into a DB. Now, I need to figure out the best way to check the records in the DB for any upcoming task. I feel like I'm missing something, maybe like a trigger field as to when a reminder should go out. However, that's not a concern to me at the moment since I'll just send an alert 15 mins prior to the due date.
Question is, shoudl I run a cron job that queries the DB every minute? I take it the query will have to say something like "select all tasks that is due within 15 minutes."

A cron job seems reasonable. You just query based on the dueDate:
MySQL: SELECT * from jobs WHERE dueDate > NOW() + INTERVAL 15 MINUTES
SQL Server: SELECT * from jobs WHERE dueDate > DATEADD(GETDATE(),15,minute)

A quick cron job should be fine, for something small. It had better be a quick query though, or you'll get booted if it's a shared server.

Why don't you use those free cron services if on shared server? For something this simple, that is your best bet!

Related

Run laravel jobs one time at specific time only

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

Can we schedule cron job in the php script?

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.

Execute a timed command in a time command

I am working with an php application where i need to check a database of users for whom has birthday today.
This i do through a cron job.
Now, when i get the users with birthday, i need to send them a SMS.
The SMS has an individual time associated which determins when the SMS should be send.
Lets say John and Peter has birthday today. Peter works from 01-09 so he should get the SMS at 01 and Peter works from 08-16 so he should get his SMS at 08.
I was thinking about dynamically creating the same amount of cron jbos which equals the people who has birthday on a given date. Those cron jobs would then send the SMS to the appropiate people this one time and thats it.
My question is, is there a smarter way to do this?
Secondly, if the cron job generation idea is good enough, is there a way to remove individual old cron jobs so i dont clug up the cron job list with old jobs which will never be run again?
Thanks in advance.
If you would like to send messages to each person at a specific time using cronjobs then you would have to create a cronjob for each person. Very bad idea in my eyes as you could end up with over 1000 cronjobs!
You would be much better with a system that runs a cronjob every 5 minutes or so (however often you want), and this cronjob executes a PHP script to determine if there are any messages to send out at that time, or between now and the last cronjob.
Whilst this may mean your users could get their message up to 5 minutes late, it is unlikely because nobody I know starts work at 8:02, and therefore, if the cronjob is at 8:00, 8:05, 8:10 etc, you will almost always get the message sent right on time!
If you're worried about creating too many cron jobs (not sure how many would be a problem on your system), then why not create just one cron job at the OS level? Then you can call your program every 10 minutes or so, and it can check if there are new people that need to be sent an SMS.

Resent mysql values at certain time

I have a database with a table and some columns. One of these columns is flags (similar to SO) where users can flag comments. I would like to give each user 5 flags per day. So if a user uses 2 flags in a 24 hour period, the flags should reset to 5 at the end of the 24 hours. I really have no idea how to do this. Is there a special mysql function?
PHP:
$query=mysql_query("UPDATE users SET flags='5' WHERE userID='$user'");
how would i get this to repeat every 24 hours? (if this is the right solution)
The best way is probably to set up an automated task (possibly using cron) that runs a query to do this.
Make a script and add to cron job. It will automatically update all on specified time of day.
Cron is very simply a Linux module that allows you to run commands at predetermined times or intervals. In Windows, it’s called Scheduled Tasks. The name Cron is in fact derived from the same word from which we get the word chronology, which means order of time.
Using Cron, a developer can automate such tasks as mailing ezines that might be better sent during an off-hour, automatically updating stats, or the regeneration of static pages from dynamic sources. Systems administrators and Web hosts might want to generate quota reports on their clients, complete automatic credit card billing, or similar tasks. Cron has something for everyone!
You can use MySQL Event Scheduler and define an event to let it update every interval you want.

Execute script at variable time

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:

Categories