# Worker object

Worker client: connects to debusine server.

## Overview

- Registration (needed only once per worker): If the worker doesn’t have a token: it will generate it and register with the server (HTTP POST to `/api/1.0/worker/register`)

- The client will use this token to connect to the server (WebSocket to `/api/ws/1.0/worker/connect`)

## Flow

> 1. The worker is executed and chooses `~/.config/debusine/worker` (if it exists) or `/etc/debusine/worker`. It reads the file `config.ini` from the directory and if it already exists the file `token`.

> 2. If there isn’t a token the worker generates one (using `secrets.token_hex()`) and registers it to the Debusine server via HTTP POST to /api/1.0/worker/register sending the generated token and the worker’s FQDN. The token is saved to the `token` file in the chosen config directory.

> 3. The server will create a new Token and Worker in the DB via the models. They wouldn’t be used until manual validation.

> 4. The client can then connect using WebSockets to `/api/ws/1.0/worker/connect` and wait for commands to execute.

## Objects documentation

class debusine.worker.Worker(*, log_level: str | None = None, worker_type: Literal[WorkerType.EXTERNAL, WorkerType.SIGNING] = WorkerType.EXTERNAL, config: ConfigHandler | None = None, working_directory: str | None = None)

  Bases: `object`

  Worker class: waits for commands from the debusine server.

  AFTER_WORK_REQUEST_HOOK_DIR = PosixPath('/etc/debusine/worker/hooks/after-work-request.d')
  DEFAULT_LOG_LEVEL = 20
  __init__(*, log_level: str | None = None, worker_type: Literal[WorkerType.EXTERNAL, WorkerType.SIGNING] = WorkerType.EXTERNAL, config: ConfigHandler | None = None, working_directory: str | None = None) → None

    Initialize Worker.

    Parameters:

      - log_level – minimum level of the logs being saved. If None uses settings from config.ini or DEFAULT_LOG_LEVEL.

      - config – ConfigHandler to use (or creates a default one)

  clean_workdir() → None

    Clean the working directory from interrupted work requests.

  async handle_server_notifications() → None

    Read incoming server notifications and generate worker events.

  static log_forced_exit(signum: Signals) → None

    Log a forced exit.

  async main() → None

    Run the worker.

  async process_notifications() → ShutdownEvent | None

    Process server notifications.

  async report_task_result(task_runner: TaskRunner, result: WorkRequestResults, output_data: OutputData) → None
  async require_token() → None

    Handle registration if needed.