mysql event scheduler to update - php

I am quite new to web developing and am a little lost as to where to start with the following
problem.
Basically i want a mysql event scheduler to run every 30 mins whereby it copies a hundred elements in one column of a table in my database to another column in the same table (so updating the later table to these new values) and then finding the least value among the hundred copied entries (integers) in the updated column. And then so i can set the value as a local variable in a php script.
How on earth would i go about this? I assume it is possible. I would greatly appreciate it if somebody could provide a general or abstract method. Cheers.

To create an event, you would just do something like this:
CREATE EVENT database.eventname
ON SCHEDULE EVERY 30 MINUTE
DO
BEGIN
# Your SQL here
END
You'll need to make sure the event scheduler is running as well. If it is running, you should see it's thread if you run the SHOW PROCESSLIST command. If not, you can turn it on like so:
SET GLOBAL event_scheduler = ON;
You'll also want to set the event_scheduler=ON setting in your my.cnf file to make sure it runs at startup (I believe it is OFF on most systems by default)
See here for more details on the MySQL event scheduler

Related

Sheduled event disappearing after another has finished

Does anybody know how WordPress stores it's CronJob events? I'm developing a plugin with multiple concurrent CronJobs, which behaves really strange. When configuring the plugin the first Event will generate some page data over a period of roughly 10-15 mins and is split into multiple packages. These packages will reschedule themselves to get the maximum running time, without hitting the script execution limit. However when the first CronJob is executed, the user can start a second one (not the same one, it's from another section), which will always result in the second one being scheduled, staying in standby and getting removed after the first one has finished an execution.
We had problems with long running CronJobs and the database cache before: Some of our data is bundled into an option and inserting data into this package will overwrite changes made outside of the CronJob. Maybe something similar is happening here. For reference: The reshedule of the first CronJob happens inside said CronJob. Could that be a problem too?
This is how the error is behaving:
Init
Cron 1 is sheduled to a past timestamp.
Cron 1 is starting.
Cron 2 is sheduled to a past timestamp.
Cron 1 is working.
Cron 1 is finished.
Cron 1 is resheduled to a new timestamp.
Cron 2 gets removed from the event queue.
Cron 1 is starting...
I have checked everything that correlates to the scripts themselves: The events are properly registered, have a unique argument (just in case) and even pull a new version of the database options they change, before doing so. Limits are set beforehand and every related function is wrapped in a try-catch-block.
My questions so far: Does anybody know what can cause a CronJob do get deleted (besides "wp_clear_sheduled_hook")? Does WordPress store the events as an option? Can a CronJob overwrite these settings, when it is running for a long time?
Thanks for your help and greetings
SOLUTION: Thanks #kyon147 for pointing out that WordPress is using the wp-options table to store information about the sheduled events. In case anyone has similar problems: Wordpress will load ALL options into it's cache, when it is called. Meaning when starting Cron1 the "cron"-array with your events might look like this:
array('cron1' => 'time')
When something is changing this option while the script is still runing, this change will not be reflected to the script. Meaning the array will still be as above, even when an event is added from another script/session. So when resheduling the event INSIDE Cron1 WordPress took the array above, not the new one. This resulted in the changes being reset to the state, when Cron1 was started and thus the event appearing missing.

Update database table cyclically every 5 minutes

I have to populate and update one of my MySql database table using a complex and expensive query, based on selection from other table's data. This table doesn't need to be always fully updated when i make a query on it, but i'd like to have a cyclic update every 5 minutes.
This automatic update should be infinite and i need to be sure that it never stops.
After some research, i've found some solution, but i don't know which is better for security and performance.
One of these could be my goal:
Don't create table and make complex query from php every time to get the desired result
Create a php script that repeats cyclically and update table db, maybe using Cron Job.
Update table using a sql event
I think that first solution could be to expensive since query is complex and there could be many request every second, but the result is always updated. I don't have experience about Cron Job, so i can't know if it could be a good idea or not. For the third solution, i still don't have database privileges to run events, but i'd like to know if it could be a valid solution.
All other solutions are welcome, thanks.
Do not use cron. Think about what will happen if one instance goes beyond 5 minutes and the next starts up. Eventually you will have hundreds of copies bogged down stumbling over each other.
Instead have a single job in a loop doing the update. (OK, you could have a cron job to perform a "keep-alive" task of restarting the query if it dies.)
The job would
CREATE TABLE new ...
INSERT INTO new SELECT complex-stuff...
RENAME TABLE real TO old, new TO real;
DROP TABLE old;
loop.
I would opt for Cron Job.
It doesn't clog any request, since it's executed from the operating system.
You can define which user executes the script (cron -u apache -e).
Easy to define interval. (i.e. every 5 minutes */5 * * * * php /path/to/script.php).
It's loggable.
Additional Notes
I had a cron job running under root and it worked just fine. My problem was that the project had a private logging mechanism that each log file would be created by apache user. By running it from root, sometimes the file would be created by root and after that, the scripts being executed by apache would not be able to APPEND to the log.
I also had an emailing script that would run once every 2 minutes that got stuck for 1h. Turns out, because of a bug in the application, an invalid email address (somethingwithoutatsign.com) was inserted into the database, which made the PHPMailer library throws errors. After that, I added a catch block that would send an email to me whenever an exception was thrown. Now, if the script stops running because of bad execution, I get to know right away.

Truncate database automatically without event scheduler?

My host (blueangelhost.com) claims that I can't use the event scheduler because it takes up too many resources. I have access to cron jobs in cPanel, but I've tried and they don't seem to work.
So, my question: Is there any kind of efficient PHP code that will automatically truncate a MySQL table in a database?
Well, if it needs to be automatic, or at a specific time, not really. But you could have your website trigger the script when someone gets on it, here's the approach you could use:
On a script that is run on every page (header, menu,footer, layout):
Check in DB or file, the date of the last truncate;
If the date is yesterday, run the truncate;
Change DB or file and put current date;
This way, it will execute once a day. But never at the same time, and not if no one walks on your website for a whole day.

Scheduled Database Updates on Given Date - MySQL & PHP

I have a requirement where I need to Update some fields in database on monthly basis, I know I can use Cron Jobs, But I don't have access to use Cron, So is there any other way doing this in PHP, MySQL, JQuery or any other ??
I just need to update & Insert some rows in Database Tables on the scheduled time Automatically.
If you have site or something else with regular visits you can check with each visit is it time to run you job or not. If I not mistake wordpress's jobs are working in this way.
Have you ever heard of MySQL Event Scheduler?
Info
Creating event
Do you have access to it? If you want to check whether event_scheduler is running you can type show processlist - there should be a process run by User 'event_scheduler'. If not you can always run it.
Of course the best way is to add event_scheduler = ON in mysql config file (in mysqld section).

Making timers to run code on expired records

I am developing a web application (PHP/MySQL), in which I need to implement timers (record id expires at expiration_date), where expiration involves the record's state being updated and arbitrary code being executed as required.
I basically need a BPMN Timer event.
Options I have considered, none of which I am thrilled with:
Cronjob calling a function in the application that just queries for and updates expired records.
At a commonly called point in the code, call this function at a fixed interval.
How is this commonly done in PHP applications?
The following is an option which you have not considered:
You could use MySQL Event Scheduler to run a stored expiration routine within the database periodically.
Some good things about this approach:
Platform independent, works the same way on UNIX and Windows.
Easy to set-up. No need to explain to the user how to setup cron jobs. You just create the event together with your database schema.
Downsides:
Not enabled by default in MySQL. Need to put event_scheduler=on in my.cnf or otherwise enable it.
Requires MySQL 5.1 or newer if I remember correctly.
Example:
DROP EVENT IF EXISTS expire_event;
DELIMITER //
CREATE EVENT expire_event
ON SCHEDULE EVERY 1 MINUTE
DO BEGIN
DELETE FROM data WHERE time < UNIX_TIMESTAMP(NOW() - INTERVAL 1 HOUR);
END //
DELIMITER ;
The above will every minute delete any rows from data table which have a UNIX time stamp older than 1 hour.
I would also choose the cronjob option.
I would also, however, have logic in my app that knows how to deal (or ignore) expired records. Services like cron can break, so it's nice to NOT have to rely upon it.
Unfortunately, DBMSs don't give us quite this sort of flexibility, and you won't want to go off and do database maintenance in the event you come across one of these "expired" records.
You're best excluding them through queries, the use of views, or simple application logic.
I would choose the cronjob option.

Categories