Scheduled task doesn't run

2019-08-04 11:00发布

CREATE EVENT `set_trips_finished`
  ON SCHEDULE EVERY 1 DAY STARTS '2015-08-25 01:50:00'
  ON COMPLETION PRESERVE
DO BEGIN

  UPDATE trips
  SET status = 0
  WHERE date(created_at) < curdate();

END;

is the scheduled task. But the fields don't get updated. When I run just the query - the fields get updated fine.

I have another scheduled task, with the very same syntax, just scheduled to run a 5 minutes later, and it runs fine.

I don't understand why wouldn't this task run, or whether the query does not update the table... Any suggestions?

Update

I deleted the other scheduled task (the one that was working), and set them both again, and now neither fires...

1条回答
我命由我不由天
2楼-- · 2019-08-04 11:12

See if event scheduler is even running:

show variables where variable_name='event_scheduler';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| event_scheduler | OFF   |
+-----------------+-------+

nope

Create my test table:

create table trips
(   id int auto_increment primary key,
    status int not null,
    created_at date not null
);
insert trips(status,created_at) values (0,'2014-09-09');

Create my event:

DELIMITER $$
CREATE EVENT `set_trips_finished`
  ON SCHEDULE EVERY 1 MINUTE STARTS '2015-08-23 00:00:00'
  ON COMPLETION PRESERVE
DO BEGIN

  UPDATE trips
  SET status = status+1
  WHERE date(created_at) < curdate();

END;$$
DELIMITER ;

List all events by schema name:

show events from so_gibberish;

or

show events\G; -- <--------- I like this one from mysql> prompt
show events; -- <--------- from workbench / sqlyog


*************************** 1. row ***************************
                  Db: so_gibberish
                Name: set_trips_finished
             Definer: GuySmiley@localhost
           Time zone: SYSTEM
                Type: RECURRING
          Execute at: NULL
      Interval value: 1
      Interval field: MINUTE
              Starts: 2015-08-23 00:00:00
                Ends: NULL
              Status: ENABLED
          Originator: 1
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: utf8_general_ci

Look at data that might get status updated:

select * from trips;
+----+--------+------------+
| id | status | created_at |
+----+--------+------------+
|  1 |      0 | 2014-09-09 |
+----+--------+------------+

Well I can wait all day long, events aren't even turned on

SET GLOBAL event_scheduler = ON;    -- turn her on

show variables where variable_name='event_scheduler';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| event_scheduler | ON    |
+-----------------+-------+

Wait a few minutes (note my event runs every minute)

select * from trips;
+----+--------+------------+
| id | status | created_at |
+----+--------+------------+
|  1 |      3 | 2014-09-09 |
+----+--------+------------+

Event has run 3 times. Ok, looks good.

SET GLOBAL event_scheduler = OFF;   -- turn her off if so desired
查看更多
登录 后发表回答