GenWorker v0.0.8 GenWorker behaviour View Source

Generic Worker behavior that helps to run task at a specific time with a specified frequency.

Usage

Define you worker module

defmodule MyWorker do
  use GenWorker, run_at: [hour: 13, minute: 59], run_each: [days: 1]

  def run(_prev_args) do
    IO.puts "MyWorker run every day at 13:59"
  end
end

Supported options

run_at – keyword list with integers values. Supported keys: :year, :month, :day, :hour, :minute, :second, :microsecond.

Or you can use map for multiple runs:

  use GenWorker, run_at: %{"some_key" => [hour: 13, minute: 59], "other_key" => [hour: 14, minute: 00]}, run_each: [days: 1]

run_each - keyword list with integers values. Supported keys: :years, :months, :weeks, :days, :hours, :minutes, :seconds, :milliseconds. Default is [days: 1]

timezone - valid timezone. :utc - by default. Receive full list of timezones call Timex.timezones/0

You need to implement callback function: run/1 that defines worker business logic

### Add worker to the application supervision tree:

  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    children = [
      worker(MyWorker, [])
      # ...
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end

Link to this section Summary

Functions

Allows to set the config options.

Callbacks

Callback that should implement task business logic that must be securely processed.

Link to this section Functions

Allows to set the config options.

GenWorker.configure(fn(config) ->
  config.before fn()->
    IO.puts "runs before task hook"
  end

  config.finally fn()->
    IO.puts "runs after task hook"
  end
end)

Link to this section Callbacks

Link to this callback

run(worker_args)

View Source
run(worker_args :: term()) :: worker_args :: term()

Callback that should implement task business logic that must be securely processed.