Ofelia Job Scheduler

repository·master·Indexed 26 days ago

https://github.com/mcuadros/ofelia

A modern, low-footprint job scheduler built in Go for Docker environments. Ofelia serves as a container-native replacement for cron, supporting four job types: job-exec (commands in running containers), job-run (commands in new containers), job-local (commands on the host), and job-service-run (run-once services for Docker Swarm). It supports configuration via INI files or Docker labels, featuring dynamic hot-reloading, Docker event listening, and logging drivers for mail, Slack, and local storage.

Tokens
3.8K
Snippets
7
Records
25
Agent score
86%

What's inside Ofelia

  1. Configure jobs using Docker labels

    master

    You can configure jobs by adding labels to containers. The format is ofelia.<JOB_TYPE>.<JOB_NAME>.<JOB_PARAMETER>=<PARAMETER_VALUE>.

    To use job-exec on a target container, the target container must have the label ofelia.enabled=true.

    Example: Running a local job via Docker CLI

    docker run -it --rm \
        -v /var/run/docker.sock:/var/run/docker.sock:ro \
        --label ofelia.job-local.my-test-job.schedule="@every 5s" \
        --label ofelia.job-local.my-test-job.command="date" \
            mcuadros/ofelia:latest daemon --docker

    Example: Docker Compose configuration

    version: "3"
    services:
      ofelia:
        image: mcuadros/ofelia:latest
        depends_on:
          - nginx
        command: daemon --docker
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock:ro
        labels:
          ofelia.job-local.my-test-job.schedule: "@every 5s"
          ofelia.job-local.my-test-job.command: "date"
    
      nginx:
        image: nginx
        labels:
          ofelia.enabled: "true"
          ofelia.job-exec.datecron.schedule: "@every 5s"
          ofelia.job-exec.datecron.command: "uname -a"
  2. Configure Ofelia jobs

    master

    Ofelia supports four types of jobs using a scheduling format compatible with Go's cron implementation (e.g., @every 10s or 0 1 * * *).

    Job Types:

    • job-exec: Executes a command inside a running container.
    • job-run: Runs a command inside a new container using a specific image.
    • job-local: Runs the command inside the host running Ofelia.
    • job-service-run: Runs the command inside a new "run-once" service (for Docker Swarm).

    Note on version 0.3.x: For the current 0.3.x version, seconds configuration in the cron spec is required. In newer versions (0.4.x+), seconds are optional.

  3. Configure Logging drivers

    master

    Ofelia supports three logging drivers: mail, save, and slack. These are configured in the [global] section of a config.ini file or via labels on the ofelia container.

    Mail options:

    • smtp-host: SMTP server address.
    • smtp-port: SMTP server port.
    • smtp-user: SMTP username.
    • smtp-password: SMTP password.
    • smtp-tls-skip-verify: If true, ignores certificate errors.
    • email-to: Recipient email address.
    • email-from: Sender email address.
    • mail-only-on-error: If true, only sends mail on failure.

    Save options:

    • save-folder: Directory for reports (must exist).
    • save-only-on-error: If true, only saves reports on failure.

    Slack options:

    • slack-webhook: Slack webhook URL.
    • slack-only-on-error: If true, only sends Slack messages on failure.
  4. Configure Ofelia to use Docker labels for job definitions

    master

    Ofelia can dynamically discover and manage jobs by reading Docker container labels. When enabled, Ofelia watches the Docker event stream for container lifecycle changes (create, start, stop, etc.) and hot-reloads its configuration automatically.

    To use this feature, you must enable the --docker flag in your Ofelia command-line invocation. You can also provide specific Docker filters to limit which containers Ofelia inspects.

  5. Configure jobs using INI files

    master

    You can define jobs in an INI-style configuration file and run Ofelia with the --config flag.

    [job-exec "job-executed-on-running-container"]
    schedule = @hourly
    container = my-container
    command = touch /tmp/example
    
    [job-run "job-executed-on-new-container"]
    schedule = @hourly
    image = ubuntu:latest
    command = touch /tmp/example
    
    [job-local "job-executed-on-current-host"]
    schedule = @hourly
    command = touch /tmp/example
    
    [job-service-run "service-executed-on-new-container"]
    schedule = 0,20,40 * * * *
    image = ubuntu
    network = swarm_network
    command =  touch /tmp/example
  6. Configure `job-exec` to run commands in existing containers

    master

    Use job-exec to execute a command inside a currently running container, similar to docker exec.

    Required Parameters:

    • schedule: Cron expression or @every format (e.g., @every 10s or 0 0 1 * * *). Note that the format starts with seconds.
    • command: The command to run.
    • container: The name of the target container.

    Optional Parameters:

    • user: User to execute the command as (default: root).
    • tty: Allocate a pseudo-tty (default: false).
    • environment: Environment variables (e.g., FOO=bar).
      • In INI files, use environment multiple times.
      • In Docker labels, use a JSON array: ["FOO=bar", "BAZ=qux"].
      • Note: environment in job-exec requires Docker API 1.25 or higher.
    [job-exec "flush-nginx-logs"]
    schedule = @hourly
    container = nginx-proxy
    command = /bin/bash /flush-logs.sh
    user = www-data
    tty = false
  7. Configure `job-service-run` for Docker Swarm

    master

    Use job-service-run to run a command inside a new "run-once" service, specifically designed for use within a Docker Swarm environment.

    Parameters:

    • schedule: Cron expression or @every format (Required).
    • image: The image to use (Required).
    • command: Command to run (defaults to container default).
    • network: Connect to a specific network.
    • delete: Delete the container after job finishes (default: true).
    • user: User to execute as (default: root).
    • tty: Allocate a pseudo-tty (default: false).
    [job-service-run "service-executed-on-new-container"]
    schedule = 0,20,40 * * * *
    image = ubuntu
    network = swarm_network
    command =  touch /tmp/example
  8. Configure `job-local` to run commands on the host

    master

    Use job-local to run commands directly on the host machine where Ofelia is running.

    Important Note: If Ofelia is running inside a container, job-local executes the command inside the Ofelia container, not on the Docker host.

    Parameters:

    • schedule: Cron expression or @every format (Required).
    • command: The command to execute (Required).
    • dir: Base directory for execution (default: current directory).
    • environment: Environment variables (e.g., FOO=bar).
      • In INI files, use environment multiple times.
      • In Docker labels, use a JSON array: ["FOO=bar", "BAZ=qux"].
    [job-local "create-file"]
    schedule = @every 15s
    command = touch test.txt
    dir = /tmp/
  9. Configure `job-run` to start new containers or existing ones

    master

    Use job-run for two scenarios:

    1. Run a new container: Specify an image and command (similar to docker run).
    2. Start a stopped container: Specify a container name (similar to docker start).

    Key Parameters:

    • schedule: Cron expression or @every format (Required).
    • image: The image to use (Required for scenario 1).
    • container: The container name to start (Required for scenario 2).
    • command: Command to run (defaults to container default).
    • entrypoint: Override container default entrypoint.
    • user: User to execute as (default: root).
    • network: Connect to a specific network.
    • hostname: Define the container hostname.
    • delete: Delete container after job finishes (default: true).
    • tty: Allocate a pseudo-tty (default: false).
    • volume: Bind mount host directory (e.g., /tmp/test:/tmp/test:rw).
      • In INI files, use volume multiple times.
      • In Docker labels, use a JSON array: ["/test/tmp:/test/tmp:ro", "/test/tmp:/test/tmp:rw"].
    • volumes-from: Use volumes from another container.
      • In INI files, use volumes-from multiple times.
      • In Docker labels, use a JSON array: ["container-foo", "bar-container"].
    • environment: Environment variables (e.g., FOO=bar).
      • In INI files, use environment multiple times.
      • In Docker labels, use a JSON array: ["FOO=bar", "BAZ=qux"].
    [job-run "print-write-date"]
    schedule = @every 5s
    image = alpine:latest
    command = sh -c 'date | tee -a /tmp/test/date'
    volume = /tmp/test:/tmp/test:rw
    environment = FOO=bar
    environment = BAZ=qux
  10. Configure Docker Host connection

    master

    Ofelia connects to the Docker daemon via /var/run/docker.sock by default. You can override this using the following environment variables:

    • DOCKER_HOST: The Docker host to connect to (e.g., tcp://docker-proxy:2375, unix:///custom/docker.sock).
    • DOCKER_TLS_VERIFY: Enable TLS verification (set to 1 to enable).
    • DOCKER_CERT_PATH: Path to TLS certificates directory.
    • DOCKER_API_VERSION: Docker API version to use.