Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 14 additions & 53 deletions src/pg_cron.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
#include "mb/pg_wchar.h"
#include "parser/analyze.h"
#include "pgstat.h"
#if PG_VERSION_NUM >= 130000
#include "postmaster/interrupt.h"
#else
#define SignalHandlerForConfigReload PostgresSigHupHandler
#endif
#include "postmaster/postmaster.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
Expand Down Expand Up @@ -120,8 +125,6 @@ typedef enum
/* forward declarations */
void _PG_init(void);
void _PG_fini(void);
static void pg_cron_sigterm(SIGNAL_ARGS);
static void pg_cron_sighup(SIGNAL_ARGS);
PGDLLEXPORT void PgCronLauncherMain(Datum arg);
PGDLLEXPORT void CronBackgroundWorker(Datum arg);

Expand Down Expand Up @@ -154,10 +157,6 @@ static void bgw_generate_returned_message(StringInfoData *display_msg, ErrorData
char *CronTableDatabaseName = "postgres";
static bool CronLogStatement = true;
static bool CronLogRun = true;
static bool CronReloadConfig = false;

/* flags set by signal handlers */
static volatile sig_atomic_t got_sigterm = false;

/* global variables */
static int CronTaskStartTimeout = 10000; /* maximum connection time */
Expand Down Expand Up @@ -348,39 +347,6 @@ _PG_init(void)
}


/*
* Signal handler for SIGTERM
* Set a flag to let the main loop to terminate, and set our latch to wake
* it up.
*/
static void
pg_cron_sigterm(SIGNAL_ARGS)
{
got_sigterm = true;

if (MyProc != NULL)
{
SetLatch(&MyProc->procLatch);
}
}


/*
* Signal handler for SIGHUP
* Set a flag to tell the main loop to reload the cron jobs.
*/
static void
pg_cron_sighup(SIGNAL_ARGS)
{
CronJobCacheValid = false;
CronReloadConfig = true;

if (MyProc != NULL)
{
SetLatch(&MyProc->procLatch);
}
}

/*
* pg_cron_cmdTuples -
* mainly copy/pasted from PQcmdTuples
Expand Down Expand Up @@ -563,9 +529,9 @@ PgCronLauncherMain(Datum arg)
struct rlimit limit;

/* Establish signal handlers before unblocking signals. */
pqsignal(SIGHUP, pg_cron_sighup);
pqsignal(SIGHUP, SignalHandlerForConfigReload);
pqsignal(SIGINT, SIG_IGN);
pqsignal(SIGTERM, pg_cron_sigterm);
pqsignal(SIGTERM, die);

/* We're now ready to receive signals */
BackgroundWorkerUnblockSignals();
Expand Down Expand Up @@ -630,7 +596,7 @@ PgCronLauncherMain(Datum arg)

MemoryContextSwitchTo(CronLoopContext);

while (!got_sigterm)
for (;;)
{
List *taskList = NIL;
TimestampTz currentTime = 0;
Expand All @@ -639,20 +605,18 @@ PgCronLauncherMain(Datum arg)

AcceptInvalidationMessages();

if (CronReloadConfig)
if (ConfigReloadPending)
{
/* set the desired log_min_messages */
ProcessConfigFile(PGC_SIGHUP);
SetConfigOption("log_min_messages", cron_error_severity(CronLogMinMessages),
PGC_POSTMASTER, PGC_S_OVERRIDE);
CronReloadConfig = false;
ConfigReloadPending = false;

/* Some settings might have changed, force RefreshTaskHash() */
CronJobCacheValid = false;
}

/*
* Both CronReloadConfig and CronJobCacheValid are triggered by SIGHUP.
* ProcessConfigFile should come first, because RefreshTaskHash depends
* on settings that might have changed.
*/
if (!CronJobCacheValid)
{
RefreshTaskHash();
Expand All @@ -669,10 +633,7 @@ PgCronLauncherMain(Datum arg)
MemoryContextReset(CronLoopContext);
}

ereport(LOG, (errmsg("pg_cron scheduler shutting down")));

/* return error code to trigger restart */
proc_exit(1);
/* Not reachable */
}


Expand Down