Schedule and run an idempotent SQL TaskΒΆ

On an isolated instance, create a task with an annual schedule and trigger it manually. The WHERE NOT EXISTS guard makes a rerun harmless. Send the CREATE TASK body as one statement; the MySQL CLI needs the delimiter change shown below.

CREATE DATABASE cookbook_task;
CREATE TABLE cookbook_task.events(id INT PRIMARY KEY);
DELIMITER //
CREATE TASK cookbook_task_once SCHEDULE '0 0 0 1 1 *' TIMEZONE 'UTC'
AS BEGIN
  INSERT INTO cookbook_task.events SELECT 1
  WHERE NOT EXISTS (SELECT 1 FROM cookbook_task.events WHERE id=1);
END//
DELIMITER ;
EXECUTE TASK cookbook_task_once;
SELECT SLEEP(2);
SELECT COUNT(*) AS event_count FROM cookbook_task.events;
EXECUTE TASK cookbook_task_once;
SELECT SLEEP(2);
SELECT COUNT(*) AS event_count FROM cookbook_task.events;
SHOW TASK RUNS FOR cookbook_task_once LIMIT 2;
DROP TASK cookbook_task_once;
DROP DATABASE cookbook_task;

Expect event_count=1 after either manual run. Check SHOW TASK RUNS for status; task execution is asynchronous, so wait or poll before checking. Drop the task before its database. See the v4.1.0 source test.