Giter Club home page Giter Club logo

clerk's Introduction

Clerk

Simple manager for global periodic tasks

This library can be used to run periodic tasks on multiple similar Erlang nodes, given that task will be executed only once on one randomly selected node with given interval.

For example, if You need to poll data from 3rd party API or update materialized view every 5 seconds but You application can dynamically scale from one node to tens or hundreds of nodes running simultaneously and back time. Clerk will start a global manager process on one of the nodes that will run periodic task. Other nodes will monitor this global process process and will take control in case a node or process will shut down. Task will run on the randomly selected node to balance the load. This library does not use any centralized or distributed storage to sync execution between nodes, so there is no 100% guarantee that the execution will strictly adhere to the specified interval. The idea of this library is based on simplicity of usage and implementation.

To use this library You will need to define "task" module implementing 3 callbacks required by clerk task behaviour:

  1. execute/1 is required to define task code that will be excuted.

  2. init/1 is predefined callback that can do some setup, get, or define some initial task state using provided args.

  3. list/0 is predefined callback that will return the list of te nodes that can be used to execute task. These nodes should have task module defined and loaded.

(1) and (2) callbacks should return either :ok or {:ok, state} tupples. state returned from the task execution will be passed to the next task call. Just keep in mind that this state is managed by the global process and it can be lost. Task should be able to recreate this state from the init/1 call.

Examples

Defining task module some_periodic_task.ex

defmodule TestApp.PeriodicTask do
  use Clerk.Task

  @impl true
  def execute(%{http_client: http_client} = state) do

    do_some_api_call(http_client)

    {:ok, state}
  end
end

Configure task parameters from the application supervisor application.ex

defmodule TestApp.Application do
  use Application

  def start(_type, _args) do
    # add app version into Logger meta
    :logger.update_primary_config(%{metadata: %{ver: @version}})

    children = [
      # Start the Ecto repository
      TestApp.Repo,
      # Periodic tasks
      {Clerk, api_poll_task_params()}
    ]

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

  defp env(), do: Application.fetch_env!(:test_app, :env)

  defp api_poll_task_params() do
    %{
      enabled: env() != :test,
      task: TestApp.PeriodicTask,
      # do not run task on app start
      instant: false,
      args: %{http_client: TestApp.HttpClient},
      interval: 10_000,
      timeout: 30_000
    }
  end
end

Installation

If available in Hex, the package can be installed by adding clerk to your list of dependencies in mix.exs:

def deps do
  [
    {:clerk, "~> 0.1.0"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/clerk.

Testing

Testing over local distributed nodes cluster requires epmd daemon to be running. You can start epmd via epmd -daemon command and stop with epmd -kill.

clerk's People

Contributors

ybod avatar

Watchers

 avatar  avatar

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.