GenWorker
Generic Worker behavior that helps to run task at a specific time with a specified frequency.
Installation and usage
It’s available in Hex, the package can be installed as:
- Add
gen_worker
to your list of dependencies in mix.exs:
def deps do
[
{:gen_worker, ">= 0.0.1"}
]
end
Then run mix deps.get
to get the package.
- Define your business logic:
defmodule MyWorker do
use GenWorker, run_at: [hour: 13, minute: 59], run_each: [days: 1]
def run do
IO.puts "MyWorker run every day at 13:59"
end
end
- Add it 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
Documentation can be found at https://hexdocs.pm/gen_worker.
Supported options
run_at
– keyword list with integers values. Supported keys: :year
, :month
, :day
, :hour
, :minute
, :second
, :microsecond
.
run_each
- keyword list with integers values. Supported keys: :years
, :months
, :weeks
, :days
, :hours
, :minutes
, :seconds
, :milliseconds
. Default: [days: 1]
.
timezone
- valid timezone. Default: :utc
.
License
This software is licensed under the MIT license.