Giter Site home page Giter Site logo

gen_worker's Introduction

GenWorker

Elixir CI Hex pm Coverage Status

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:

  1. 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.

  1. 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
  1. 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.

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: [days: 1].

timezone - valid timezone. Default: :utc.

Configuration

You can define callbacks to all workers:

  • init -> calls on initializing worker
  • before -> calls each time before worker task starts
  • finally -> calls each time on the end worker task

For exampLe it could be used in the tests test_helper.exs

  GenWorker.configure(fn c ->
    c.init(fn _module, _args ->
      :ok = Ecto.Adapters.SQL.Sandbox.checkout(App.Repo)
      Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()})
      :ok
    end)
  end)

License

This software is licensed under the MIT license.

gen_worker's People

Contributors

posthawk avatar slavenin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

gen_worker's Issues

Server timezone not using

Hi!
My server timezone is Europe/Moscow. But i can not set this for Timex
Timex.now() return date without timezone

After update cannot run if time in past

Hi!
I have a module:

defmodule Instruments.CacheUpdater do
  @moduledoc false
  alias Helpers.RedmineApi
  use GenWorker,
      run_at: [minute: 0],
      run_each: [
        minutes: 30
      ],
      timezone: "Europe/Moscow"

  def run(_params) do
    #do actions
  end
end

on run:

[debug] GenWorker: Init worker with state: %GenWorker.State{caller: Instruments.CacheUpdater, last_called_at: nil, run_at: [minute: 0], run_each: [minutes: 30], timezone: "Europe/Moscow", worker_args: {:restart, :permanent}}
[debug] GenWorker run worker after -960000 msec
** (Mix) Could not start application telegrm: Telegrm.Application.start(:normal, []) returned an error: shutdown: failed to start child: Instruments.CacheUpdater
    ** (EXIT) an exception was raised:
        ** (ArgumentError) argument error
            :erlang.send_after(-960000, #PID<0.615.0>, :run_work)
            lib/gen_worker/server.ex:46: GenWorker.Server.schedule_work/1
            lib/gen_worker/server.ex:11: GenWorker.Server.init/1
            (stdlib) gen_server.erl:365: :gen_server.init_it/2
            (stdlib) gen_server.erl:333: :gen_server.init_it/6
            (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3

before update i simply not use run_at parameter run_at: [],, but now i cant do this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.