class gdbus_util.ExitOnIdleService(connection: DBusConnection, name: str, timeout: int)

Bases: object

A base class which implements race-free exit-on-idle for a D-Bus service. It is useful for services that are meant to be started on demand and exit automatically when they are no longer needed.

A timer is started with timeout seconds when the run() method is called. When the timer expires, the service will check if it is idle. If it is, the service will shut down gracefully. If it’s not, the timer is reset.

The idle check is implemented by the check_idle() method. By default this method always returns True. Subclasses should override this method to implement the idle check. The DBusObject class has a check_idle() method which checks if there are any ongoing method calls. If the ExitOnIdleService instance is also a DBusObject, it will use that method as the idle check by default [1].

The timer can be reset by calling the reset_idle_timer() method. If the ExitOnIdleService instance is also a DBusObject, it will automatically reset the idle timeout whenever a method call is received (or a property is read or written).

To avoid that requests are lost when the service shuts down, the service will first unregister the bus name and then handle any pending requests before exiting.

If the service is run as a systemd service, systemd will be notified that the service is stopping, so that it will queue any start requests instead of assuming that the service is still running.

See sleeper-service.py for a simple example or accounts-service.py for a more complex example of how to to implement an exit-on-idle service.

Parameters:
  • connection (Gio.DBusConnection) – The D-Bus connection on which to register the name.

  • name (str) – The name to register on the bus.

  • timeout (int) – The idle timeout in seconds.

check_idle() bool

Check if the service is idle. This method should be overridden by the subclass. By default, it always returns True.

graceful_shutdown() None

Stop the service gracefully.

reset_idle_timer() None

Reset the idle timer. This method should be called whenever the service is active.

run(handle_signals: bool = True) None

Run the service. The service registers the name on the provided D-Bus connection and enters the GLib main loop.

This method blocks until one of the following conditions is met:

  • The service is idle for timeout seconds.

  • The graceful_shutdown() method is called.

  • The D-Bus name is lost or the connection is closed.

  • A SIGTERM, SIGINT or SIGHUP signal is received, if handle_signals is True.

In all of these cases, the service will shut down gracefully. This means that the service will notify systemd that it is stopping (if running as a systemd service), release the D-Bus name and process any queued requests before this method returns.

Parameters:

handle_signals (bool) – If True, the service will handle the SIGTERM, SIGINT and SIGHUP signals and shut down gracefully when one of these signals is received.