daemon/config: add new IdleExitTimeout config

Allow users to specify how long the daemon should idle for before
exiting. This was *mostly* an excuse for me to set up a somewhat useful
config file ahead of the auto-update work. Though I think allowing this
config makes sense as well. I like it better than the secret env var for
development purposes. PackageKit also has a similar `ShutdownTimeout`
configuration.

Closes: #1204
Approved by: cgwalters
This commit is contained in:
Jonathan Lebon 2018-01-12 17:34:29 +00:00 committed by Atomic Bot
parent b9bae8fc5b
commit cf1038e804
3 changed files with 39 additions and 9 deletions

View File

@ -63,11 +63,18 @@ Boston, MA 02111-1307, USA.
<title>Options</title>
<para>
All options are configured in the "[Daemon]" section. There are
currently no available options.
All options are configured in the "[Daemon]" section. Available options are:
</para>
<variablelist>
<varlistentry>
<term><varname>IdleExitTimeout=</varname></term>
<listitem>
<para>Controls the time in seconds of inactivity before the daemon exits. Use 0 to
disable auto-exit.</para>
</listitem>
</varlistentry>
<!--
<varlistentry>
<term><varname>OptionName=</varname></term>

View File

@ -3,3 +3,4 @@
# For option meanings, see rpm-ostreed.conf(5).
[Daemon]
#IdleExitTimeout=60

View File

@ -30,10 +30,6 @@
#define RPMOSTREE_MESSAGE_TRANSACTION_STARTED SD_ID128_MAKE(d5,be,a3,7a,8f,c8,4f,f5,9d,bc,fd,79,17,7b,7d,f8)
/* let's not exit super fast; our startup is non-trivial so staying around will ensure
* follow-up requests are more responsive */
#define IDLE_EXIT_TIMEOUT_SECONDS 60
#define RPMOSTREED_CONF SYSCONFDIR "/rpm-ostreed.conf"
#define DAEMON_CONFIG_GROUP "Daemon"
@ -65,6 +61,9 @@ struct _RpmostreedDaemon {
RpmostreedSysroot *sysroot;
gchar *sysroot_path;
/* we only have one setting for now, so let's just keep it in the main struct */
guint idle_exit_timeout;
GDBusConnection *connection;
GDBusObjectManagerServer *object_manager;
};
@ -313,6 +312,23 @@ maybe_load_config_keyfile (GKeyFile **out_keyfile,
return TRUE;
}
static guint64
get_config_uint64 (GKeyFile *keyfile,
const char *key,
guint64 default_val)
{
if (keyfile && g_key_file_has_key (keyfile, DAEMON_CONFIG_GROUP, key, NULL))
{
g_autoptr(GError) local_error = NULL;
guint64 r = g_key_file_get_uint64 (keyfile, DAEMON_CONFIG_GROUP, key, &local_error);
if (!local_error)
return r;
sd_journal_print (LOG_WARNING, "Bad value for key '%s': %s; using compiled defaults",
key, local_error->message);
}
return default_val;
}
gboolean
rpmostreed_daemon_reload_config (RpmostreedDaemon *self,
gboolean *out_changed,
@ -322,7 +338,13 @@ rpmostreed_daemon_reload_config (RpmostreedDaemon *self,
if (!maybe_load_config_keyfile (&config, error))
return FALSE;
/* when we have configs, we'll read them in here */
/* default to 60s by default; our startup is non-trivial so staying around will ensure
* follow-up requests are more responsive */
guint64 idle_exit_timeout = get_config_uint64 (config, "IdleExitTimeout", 60);
/* don't update changed for this; it's contained to RpmostreedDaemon so no other objects
* need to be reloaded if it changes */
self->idle_exit_timeout = idle_exit_timeout;
if (out_changed)
*out_changed = FALSE;
@ -467,7 +489,7 @@ update_status (RpmostreedDaemon *self)
have_active_txn = TRUE;
}
if (!getenv ("RPMOSTREE_DEBUG_DISABLE_DAEMON_IDLE_EXIT"))
if (!getenv ("RPMOSTREE_DEBUG_DISABLE_DAEMON_IDLE_EXIT") && self->idle_exit_timeout > 0)
currently_idle = !have_active_txn && n_clients == 0;
if (currently_idle && !self->idle_exit_source)
@ -475,7 +497,7 @@ update_status (RpmostreedDaemon *self)
/* I think adding some randomness is a good idea, to mitigate
* pathological cases where someone is talking to us at the same
* frequency as our exit timer. */
const guint idle_exit_secs = IDLE_EXIT_TIMEOUT_SECONDS + g_random_int_range (0, 5);
const guint idle_exit_secs = self->idle_exit_timeout + g_random_int_range (0, 5);
self->idle_exit_source = g_timeout_source_new_seconds (idle_exit_secs);
g_source_set_callback (self->idle_exit_source, on_idle_exit, self, NULL);
g_source_attach (self->idle_exit_source, NULL);