Skip to content
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

util: Add -shutdownnotify and startupnotify options from upstream #2688

Merged
merged 2 commits into from
May 30, 2023
Merged
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
40 changes: 38 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ static fs::path GetPidFile(const ArgsManager& args)
// Shutdown
//

#if HAVE_SYSTEM
static void ShutdownNotify(const ArgsManager& args)
{
std::vector<std::thread> threads;
for (const auto& cmd : args.GetArgs("-shutdownnotify")) {
threads.emplace_back(runCommand, cmd);
}
for (auto& t : threads) {
t.join();
}
}
#endif

bool ShutdownRequested()
{
return fRequestShutdown;
Expand Down Expand Up @@ -126,6 +139,11 @@ void Shutdown(void* parg)
if (fFirstThread)
{
LogPrintf("gridcoinresearch exiting...");

#if HAVE_SYSTEM
ShutdownNotify(gArgs);
#endif

fShutdown = true;

// Signal to the scheduler to stop.
Expand Down Expand Up @@ -384,8 +402,11 @@ void SetupServerArgs()
" prefixed by datadir location. (default: %s)",
GRIDCOIN_CONF_FILENAME, GRIDCOIN_SETTINGS_FILENAME),
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
//TODO: Implement startupnotify option
//argsman.AddArg("-startupnotify=<cmd>", "Execute command on startup.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-startupnotify=<cmd>", "Execute command on startup.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-shutdownnotify=<cmd>", "Execute command immediately before beginning shutdown. The need for shutdown may be urgent,"
" so be careful not to delay it long (if the command doesn't require interaction with the"
" server, consider having it fork into the background).",
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);


// Staking
Expand Down Expand Up @@ -654,6 +675,17 @@ bool InitSanityCheck()
return true;
}

#if HAVE_SYSTEM
static void StartupNotify(const ArgsManager& args)
{
std::string cmd = args.GetArg("-startupnotify", "");
if (!cmd.empty()) {
std::thread t(runCommand, cmd);
t.detach(); // thread runs free
}
}
#endif

/**
* Initialize global loggers.
*
Expand Down Expand Up @@ -1533,6 +1565,10 @@ bool AppInit2(ThreadHandlerPtr threads)

GRC::ScheduleBackgroundJobs(scheduler);

#if HAVE_SYSTEM
StartupNotify(gArgs);
#endif

uiInterface.InitMessage(_("Done loading"));
g_timer.GetTimes("Done loading", "init");

Expand Down