scheduler

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2024 License: MIT Imports: 18 Imported by: 0

README

Docker-Compose scheduler

Simple and lightweight service which can execute docker compose run ... services from the same file based on cron expression.

Features:

  • Zero-configuration by-default
  • Designed for docker compose (auto-detect, respects namespace)
  • HTTP notifications with retries

Inspired by ofelia.

services:
  web:
    image: "nginx"
    labels:
      - "net.reddec.scheduler.cron=@daily"
      - "net.reddec.scheduler.exec=nginx -s reload"

  date:
    image: busybox
    restart: "no"
    labels:
      - "net.reddec.scheduler.cron=* * * * *"

  scheduler:
    image: ghcr.io/reddec/compose-scheduler:1.0.0
    restart: unless-stopped
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro

Supports two modes:

  • plain docker compose run
  • exec command inside service (extra label net.reddec.scheduler.exec)

Usage

Application Options:
      --project=              Docker compose project, will be automatically detected if not set [$PROJECT]

HTTP notification:
      --notify.url=           URL to invoke [$NOTIFY_URL]
      --notify.retries=       Number of additional retries (default: 5) [$NOTIFY_RETRIES]
      --notify.interval=      Interval between attempts (default: 12s) [$NOTIFY_INTERVAL]
      --notify.method=        HTTP method (default: POST) [$NOTIFY_METHOD]
      --notify.timeout=       Request timeout (default: 30s) [$NOTIFY_TIMEOUT]
      --notify.authorization= Authorization header value [$NOTIFY_AUTHORIZATION]

Help Options:
  -h, --help                  Show this help message

Notifications

Scheduler will send notifications after each job if NOTIFY_URL env variable or --notify.url flag set. Each notification is a simple HTTP request. HTTP method, attempts number, and interval between attempts can be configured. Authorization via Authorization header also supported.

Scheduler will stop retries if at least one of the following criteria met:

  • reached maximum number of attempts
  • server returned any 2xx code (ex: 200, 201, ...)

Outgoing custom headers:

  • Content-Type: application/json
  • User-Agent: scheduler/<version>, where <version> is build version
  • Authorization: <value> (if set)

Payload:

{
  "project": "compose-project",
  "service": "web",
  "container": "deadbeaf1234",
  "schedule": "@daily",
  "started": "2023-01-20T11:10:39.44006+08:00",
  "finished": "2023-01-20T11:10:39.751879+08:00",
  "failed": true,
  "error": "exit code 1"
}

field error exists only if failed == true

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPNotification

type HTTPNotification struct {
	URL           string        `long:"url" env:"URL" description:"URL to invoke"`
	Retries       int           `long:"retries" env:"RETRIES" description:"Number of additional retries" default:"5"`
	Interval      time.Duration `long:"interval" env:"INTERVAL" description:"Interval between attempts" default:"12s"`
	Method        string        `long:"method" env:"METHOD" description:"HTTP method" default:"POST"`
	Timeout       time.Duration `long:"timeout" env:"TIMEOUT" description:"Request timeout" default:"30s"`
	Authorization string        `long:"authorization" env:"AUTHORIZATION" description:"Authorization header value"`
	UserAgent     string
}

func (*HTTPNotification) Notify

func (ht *HTTPNotification) Notify(ctx context.Context, record *Payload) error

type Option

type Option func(scheduler *Scheduler)

func WithDocker

func WithDocker(dockerClient *client.Client) Option

func WithNotification

func WithNotification(notification *HTTPNotification) Option

func WithProject

func WithProject(composeProject string) Option

type Payload

type Payload struct {
	Project   string    `json:"project"`
	Service   string    `json:"service"`
	Container string    `json:"container"`
	Schedule  string    `json:"schedule"`
	Started   time.Time `json:"started"`
	Finished  time.Time `json:"finished"`
	Failed    bool      `json:"failed"`
	Error     string    `json:"error,omitempty"`
}

type Scheduler

type Scheduler struct {
	// contains filtered or unexported fields
}

func Create

func Create(ctx context.Context, options ...Option) (*Scheduler, error)

func (*Scheduler) Close

func (sc *Scheduler) Close() error

func (*Scheduler) Run

func (sc *Scheduler) Run(ctx context.Context) error

type Task

type Task struct {
	Service   string
	Container string
	Schedule  string
	Command   []string
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL