1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-04 11:17:02 +02:00
unleash.unleash/src/migrations/20240405174629-jobs.js
Gastón Fournier 0a2d40fb8b
feat: allow schedulers to run in a single node (#6794)
## About the changes
This PR provides a service that allows a scheduled function to run in a
single instance. It's currently not in use but tests show how to wrap a
function to make it single-instance:

65b7080e05/src/lib/features/scheduler/job-service.test.ts (L26-L32)

The key `'test'` is used to identify the group and most likely should
have the same name as the scheduled job.

---------

Co-authored-by: Christopher Kolstad <chriswk@getunleash.io>
2024-04-10 11:47:22 +02:00

38 lines
978 B
JavaScript

exports.up = function (db, cb) {
db.runSql(
`
-- this function rounds a date to the nearest interval
CREATE OR REPLACE FUNCTION date_floor_round(base_date timestamptz, round_interval interval) RETURNS timestamptz AS $BODY$
SELECT to_timestamp(
(EXTRACT(epoch FROM $1)::integer / EXTRACT(epoch FROM $2)::integer)
* EXTRACT(epoch FROM $2)::integer
)
$BODY$ LANGUAGE SQL STABLE;
CREATE TABLE IF NOT EXISTS jobs (
name TEXT NOT NULL,
bucket TIMESTAMPTZ NOT NULL,
stage TEXT NOT NULL,
finished_at TIMESTAMPTZ,
PRIMARY KEY (name, bucket)
);
CREATE INDEX IF NOT EXISTS idx_job_finished ON jobs(finished_at);
CREATE INDEX IF NOT EXISTS idx_job_stage ON jobs(stage);
`,
cb,
);
};
exports.down = function (db, cb) {
db.runSql(
`
DROP INDEX IF EXISTS idx_job_finished;
DROP INDEX IF EXISTS idx_job_stage;
DROP TABLE IF EXISTS jobs;
`,
cb,
);
};