flexget.manager module#

class flexget.manager.Manager(args)[source]#

Bases: object

Manager class for FlexGet.

Fires events:

  • manager.initialize

    The first time the manager is initialized, before config is loaded

  • manager.before_config_load

    Before the config file is loaded from disk

  • manager.before_config_validate

    When updating the config, before the validator is run on it

  • manager.config_updated

    After a configuration file has been loaded or changed (and validated) this event is fired

  • manager.startup

    After manager has been initialized. This is when application becomes ready to use, however no database lock is present, so the database must not be modified on this event.

  • manager.lock_acquired

    The manager does not always require a lock on startup, if one is requested, this event will run when it has been acquired successfully

  • manager.upgrade

    If any plugins have declared a newer schema version than exists in the database, this event will be fired to allow plugins to upgrade their tables

  • manager.shutdown_requested

    When shutdown has been requested. Any plugins which might add to execution queue should stop when this is fired.

  • manager.shutdown

    When the manager is exiting

  • manager.execute.completed

    If execution in current process was completed

  • manager.daemon.started

  • manager.daemon.completed

  • manager.db_cleanup

Parameters:

args (list[str])

static parse_initial_options(args)[source]#

Parse what we can from cli args before plugins are loaded.

Parameters:

args (list[str])

Return type:

argparse.Namespace

_add_tray_icon_items(tray_icon)[source]#
Parameters:

tray_icon (TrayIcon)

_handle_sigterm(signum, frame)[source]#
Return type:

None

_init_config(create=False)[source]#

Find and load the configuration file.

Parameters:

create (bool) – If a config file is not found, and create is True, one will be created in the home folder

Raises:

OSError when no config file could be found, and create is False.

Return type:

None

_init_logging()[source]#

Initialize logging variables.

Return type:

None

_read_lock()[source]#

Read the values from the lock file. Returns None if there is no current lock file.

Return type:

dict | None

_shutdown()[source]#

Run when the manager is done processing everything.

Return type:

None

acquire_lock(event=True)[source]#
Parameters:

event (bool) – If True, the ‘manager.lock_acquired’ event will be fired after a lock is obtained

Return type:

Iterator

backup_config()[source]#
Return type:

str

check_ipc_info()[source]#

If a daemon has a lock on the database, return info to connect to IPC.

Return type:

dict | None

check_lock()[source]#

Return True if there is a lock on the database.

Return type:

bool

config_changed()[source]#

Make sure that all tasks will have the config_modified flag come out true on the next run.

Useful when changing the db and all tasks need to be completely reprocessed.

Return type:

None

crash_report()[source]#

Be called when handling an unexpected exception.

Will create a new log file containing the last 50 debug messages as well as the crash traceback.

Return type:

str

daemon_command(options)[source]#

Handle the ‘daemon’ CLI command.

Fires events:

  • manager.daemon.started

  • manager.daemon.completed

Parameters:

options (argparse.Namespace) – argparse options

Return type:

None

daemonize()[source]#

Daemonizes the current process. Returns the new pid.

Return type:

None

db_cleanup(force=False)[source]#

Perform database cleanup if cleanup interval has been met.

Fires events:

  • manager.db_cleanup

    If interval was met. Gives session to do the cleanup as a parameter.

Parameters:

force (bool) – Run the cleanup no matter whether the interval has been met.

Return type:

None

execute(options=None, priority=1, suppress_warnings=None)[source]#

Run all (can be limited with options) tasks from the config.

Parameters:
  • options (dict | argparse.Namespace | None) – Either an argparse.Namespace instance, or a dict, containing options for execution

  • priority (int) – If there are other executions waiting to be run, they will be run in priority order, lowest first.

  • suppress_warnings (Sequence[str] | None) – Allows suppressing log warning about missing plugin in key phases

Returns:

a list of threading.Event instances which will be set when each respective task has finished running

Return type:

list[tuple[str, str, threading.Event]]

execute_command(options)[source]#

Handle the ‘execute’ CLI command.

If there is already a task queue running in this process, adds the execution to the queue. If FlexGet is being invoked with this command, starts up a task queue and runs the execution.

Fires events:

  • manager.execute.started

  • manager.execute.completed

Parameters:

options (argparse.Namespace) – argparse options

Return type:

None

handle_cli(options=None)[source]#

Dispatch a cli command to the appropriate function.

The manager should have a lock and be initialized before calling this method.

Parameters:

options (argparse.Namespace | None) – argparse options for command. Defaults to options that manager was instantiated with.

Return type:

None

hash_config()[source]#
Return type:

str | None

init_sqlalchemy()[source]#

Initialize SQLAlchemy.

Return type:

None

initialize()[source]#

Load plugins, database, and config.

Also initialize (but do not start) the task queue and ipc server. This should only be called after obtaining a lock.

Return type:

None

load_config(output_to_console=True, config_file_hash=None)[source]#

Load the config file from disk, validate and activate it.

Raises:

ValueError if there is a problem loading the config file

Parameters:
  • output_to_console (bool)

  • config_file_hash (str | None)

Return type:

None

matching_tasks(task)[source]#

Create list of tasks to run, preserving order.

Parameters:

task (str)

Return type:

list[str] | None

release_lock()[source]#
Return type:

None

save_config()[source]#

Dump current config to yaml config file.

Return type:

None

setup_yaml()[source]#

Customize the yaml loader/dumper behavior.

Return type:

None

shutdown(finish_queue=True)[source]#

Request manager shutdown.

Parameters:

finish_queue (bool) – Should scheduler finish the task queue

Return type:

None

start()[source]#

Run as the starting point when executing from commandline, dispatch execution to correct destination.

If there is a FlexGet process with an ipc server already running, the command will be sent there for execution and results will be streamed back. If not, this will attempt to obtain a lock, initialize the manager, and run the command here.

Return type:

None

update_config(config)[source]#

Provide a new config for the manager to use.

Raises:

ValueError and rolls back to previous config if the provided config is not valid.

Parameters:

config (dict)

Return type:

None

validate_config(config=None)[source]#

Check all root level keywords are valid.

Config may be modified by before_config_validate hooks. Modified config will be returned.

Parameters:

config (dict | None) – Config to check. If not provided, current manager config will be checked.

Raises:

ValueError when config fails validation. There will be an errors attribute with the schema errors.

Returns:

Final validated config.

Return type:

dict

write_lock(ipc_info=None)[source]#
Parameters:

ipc_info (dict | None)

Return type:

None

property config_base: Path#
property config_name: str#
property config_path: Path#
property has_lock: bool#
options: argparse.Namespace#
property tasks: list[str]#

A list of tasks in the config.

unit_test = False#