maestral.daemon =============== .. py:module:: maestral.daemon .. autoapi-nested-parse:: This module defines functions to start and stop the sync daemon and retrieve proxy objects for a running daemon. Module Contents --------------- .. py:function:: freeze_support() Call this as early as possible in the main entry point of a frozen executable. This call will start the sync daemon if a matching command line arguments are detected and do nothing otherwise. .. py:class:: Stop Bases: :py:obj:`enum.Enum` Enumeration of daemon exit results .. py:attribute:: Ok :value: 0 .. py:attribute:: Killed :value: 1 .. py:attribute:: NotRunning :value: 2 .. py:attribute:: Failed :value: 3 .. py:class:: Start Bases: :py:obj:`enum.Enum` Enumeration of daemon start results .. py:attribute:: Ok :value: 0 .. py:attribute:: AlreadyRunning :value: 1 .. py:attribute:: Failed :value: 2 .. py:class:: Lock(path) An inter-process and inter-thread lock This internally uses :class:`fasteners.InterProcessLock` but provides non-blocking acquire. It also guarantees thread-safety when using the :meth:`singleton` class method to create / retrieve a lock instance. :param path: Path of the lock file to use / create. .. py:method:: singleton(path) :classmethod: Retrieve an existing lock object with a given 'name' or create a new one. Use this method for thread-safe locks. :param path: Path of the lock file to use / create. .. py:attribute:: path .. py:attribute:: pid .. py:method:: acquire() Attempts to acquire the given lock. :returns: Whether the acquisition succeeded. .. py:method:: release() Release the previously acquired lock. .. py:method:: locked() Checks if the lock is currently held by any thread or process. :returns: Whether the lock is acquired. .. py:method:: locking_pid() Returns the PID of the process which currently holds the lock or ``None``. This should work on macOS, OpenBSD and Linux but may fail on some platforms. Always use :meth:`locked` to check if the lock is held by any process. :returns: The PID of the process which currently holds the lock or ``None``. .. py:function:: maestral_lock(config_name) Returns an inter-process and inter-thread lock for Maestral. This is a wrapper around :class:`Lock` which fills out the appropriate lockfile path for the given config name. :param config_name: The name of the Maestral configuration. :returns: Lock instance for the config name .. py:function:: sockpath_for_config(config_name) Returns the unix socket location to be used for the config. This should default to the apps runtime directory + 'CONFIG_NAME.sock'. :param config_name: The name of the Maestral configuration. :returns: Socket path. .. py:function:: lockpath_for_config(config_name) Returns the lock file location to be used for the config. This will be the apps runtime directory + 'CONFIG_NAME.lock'. :param config_name: The name of the Maestral configuration. :returns: Path of lock file to use. .. py:function:: get_maestral_pid(config_name) Returns the PID of the daemon if it is running, ``None`` otherwise. :param config_name: The name of the Maestral configuration. :returns: The daemon's PID. .. py:function:: is_running(config_name) Checks if a daemon is currently running. :param config_name: The name of the Maestral configuration. :returns: Whether the daemon is running. .. py:function:: wait_for_startup(config_name, timeout = 30) Waits until we can communicate with the maestral daemon for ``config_name``. :param config_name: Configuration to connect to. :param timeout: Timeout it seconds until we raise an error. :raises CommunicationError: if we cannot communicate with the daemon within the given timeout. .. py:function:: start_maestral_daemon(config_name = 'maestral', log_to_stderr = False) Starts the Maestral daemon with event loop in the current thread. Startup is race free: there will never be more than one daemon running with the same config name. The daemon is a :class:`maestral.main.Maestral` instance which is exposed as Pyro daemon object and listens for requests on a unix domain socket. This call starts an asyncio event loop to process client requests and blocks until the event loop shuts down. On macOS, the event loop is integrated with Cocoa's CFRunLoop. This allows processing Cocoa events and callbacks, for instance for desktop notifications. :param config_name: The name of the Maestral configuration to use. :param log_to_stderr: If ``True``, write logs to stderr. :raises RuntimeError: if a daemon for the given ``config_name`` is already running. .. py:function:: start_maestral_daemon_process(config_name = 'maestral', timeout = 30) Starts the Maestral daemon in a new process by calling :func:`start_maestral_daemon`. Startup is race free: there will never be more than one daemon running for the same config name. This function will use :obj:`sys.executable` as a Python executable to start the daemon. Environment variables from the current process will be preserved and updated with the environment variables defined in :const:`constants.ENV`. :param config_name: The name of the Maestral configuration to use. :param timeout: Time in sec to wait for daemon to start. :returns: :attr:`Start.Ok` if successful, :attr:`Start.AlreadyRunning` if the daemon was already running or :attr:`Start.Failed` if startup failed. It is possible that :attr:`Start.Ok` may be returned instead of :attr:`Start.AlreadyRunning` in case of a race but the daemon is nevertheless started only once. .. py:function:: stop_maestral_daemon_process(config_name = 'maestral', timeout = 10) Stops a maestral daemon process by finding its PID and shutting it down. This function first tries to shut down Maestral gracefully. If this fails and we know its PID, it will send SIGTERM. If that fails as well, it will send SIGKILL to the process. :param config_name: The name of the Maestral configuration to use. :param timeout: Number of sec to wait for daemon to shut down before killing it. :returns: :attr:`Stop.Ok` if successful, :attr:`Stop.Killed` if killed, :attr:`Stop.NotRunning` if the daemon was not running and :attr:`Stop.Failed` if killing the process failed because we could not retrieve its PID. .. py:class:: MaestralProxy(config_name = 'maestral', fallback = False) Bases: :py:obj:`ContextManager`\ [\ :py:obj:`MaestralProxy`\ ] A Proxy to the Maestral daemon All methods and properties of Maestral's public API are accessible and calls / access will be forwarded to the corresponding Maestral instance. This class can be used as a context manager to close the connection to the daemon on exit. :Example: Use MaestralProxy as a context manager: >>> import src.maestral.cli.cli_info >>> with MaestralProxy() as m: ... print(src.maestral.cli.cli_info.status) Use MaestralProxy directly: >>> import src.maestral.cli.cli_info >>> m = MaestralProxy() >>> print(src.maestral.cli.cli_info.status) >>> m._disconnect() :ivar _is_fallback: Whether we are using an actual Maestral instance as fallback instead of a Proxy. :param config_name: The name of the Maestral configuration to use. :param fallback: If ``True``, a new instance of Maestral will be created in the current process when the daemon is not running. :raises CommunicationError: if the daemon is running but cannot be reached or if the daemon is not running and ``fallback`` is ``False``.