Skip to content

Introduce cron_shutdown function for stopping the main background worker #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

serpent7776
Copy link

Normally the main background worker used to start tasks cannot be stopped, because it always auto restarts.

This change introduces SQL function cron_shutdown that can be used to stop the background worker.

It works by creating a shared memory that holds pid of the main background worker and bool flag specifying whether to restart the worker. By default, this keeps the current behaviour of automatically restarting the worker whenever it's killed. In situations when the user actually wants the main worker to be killed, It gives a way to do it.

This currently doesn't stop any other worker started by the scheduler.

The change that introduced auto-restarts is #286

Fixes #352

@serpent7776
Copy link
Author

@microsoft-github-policy-service agree

Copy link
Collaborator

@marcoslot marcoslot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In principle this makes sense, but would be good if there's also a corresponding start-up function.

@@ -1 +1,7 @@
/* no SQL changes in 1.6 */
DROP FUNCTION IF EXISTS cron.shutdown();
CREATE FUNCTION cron.shutdown()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would require a new SQL script (1.7), otherwise existing 1.6 users will never get the function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

LANGUAGE C STRICT
AS 'MODULE_PATHNAME', $$cron_shutdown$$;
COMMENT ON FUNCTION cron.shutdown()
IS 'shutdown pg_cron';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should also revoke execute from public, to ensure only superuser can call this (or grant privileges)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


LWLockAcquire(scheduler_shared_data->lock, LW_EXCLUSIVE);
pid = scheduler_shared_data->scheduler_pid;
scheduler_shared_data->scheduler_pid = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code style should be adapted to the rest of the project

(except the Vixie cron logic in entry.c/misc.c)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the style. Let me know if it's OK now.

@Splarv
Copy link

Splarv commented Mar 5, 2025

For what need such function, if I can simply stop launch jobs by

cron.launch_active_jobs = 'off'

@serpent7776
Copy link
Author

Setting cron.launch_active_jobs=off doesn't fix #352, because it doesn't stop the pg_cron launcher task.

Normally the main background worker used to start tasks cannot be
stopped, because it always auto restarts.
`cron_shutdown` function can be used to stop the background worker.
`cron_startup` can be then used to start the scheduler again.
@serpent7776
Copy link
Author

I updated the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to stop pg_cron scheduler
3 participants