This refactor was born out of the inter-dependency cycles developing between the "background" module and just about every other module which was caused by the background module becoming a dependency of every module that needed to background work and the fact that the background module was also supposedly responsible for the logic for processing those tasks. Instead the "background" module is now very, very shallow and relies entirely on the Postgres NOTIFY logic for triggering jobs. There's a new table, `job` which holds just a type and single row ID. All told, this means that jobs can be added to the queue as part of the API-level or platform-level transaction, ensuring atomicity, and processing coordination is handled by the platform module, which can depend on anything.
39 lines
878 B
PL/PgSQL
39 lines
878 B
PL/PgSQL
-- +goose Up
|
|
CREATE TYPE JobType AS ENUM (
|
|
'audio-transcode',
|
|
'csv-commit',
|
|
'csv-import',
|
|
'label-studio-audio-create',
|
|
'email-send',
|
|
'text-send'
|
|
);
|
|
|
|
CREATE TABLE job (
|
|
created TIMESTAMP WITHOUT TIME ZONE NOT NULL,
|
|
id SERIAL NOT NULL,
|
|
type_ JobType NOT NULL,
|
|
row_id INTEGER NOT NULL,
|
|
PRIMARY KEY(id)
|
|
);
|
|
COMMENT ON TABLE job IS 'A temporary holding place for jobs that are pushed to backend workers. Once work is completed the job should be deleted';
|
|
|
|
-- +goose StatementBegin
|
|
CREATE OR REPLACE FUNCTION notify_new_job()
|
|
RETURNS TRIGGER AS $$
|
|
BEGIN
|
|
PERFORM pg_notify('new_job', NEW.id::text);
|
|
RETURN NEW;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER job_insert_trigger
|
|
AFTER INSERT ON job
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION notify_new_job();
|
|
|
|
-- +goose Down
|
|
DROP TRIGGER job_insert_trigger ON job;
|
|
DROP TABLE job;
|
|
DROP TYPE JobType;
|
|
|