brndnmtthws / citrine

Elixir library for running cron-based scheduled jobs on your Erlang cluster
https://hexdocs.pm/citrine/readme.html
MIT License
14 stars 3 forks source link
cron cronjob cronjob-scheduler elixir elixir-lang elixir-library

Citrine Build/test Publish Hex pm

💎 Citrine energizes every level of life. It cleanses the chakras and opens the intuition. It's one of the most powerful energy crystals in the world. 💎

Citrine is a library for running scheduled jobs on an Erlang cluster, similar to cron. Citrine was created to satisfy a few simple requirements:

Internally Citrine uses Erlang's excellent mnesia module for state and process management. It uses in-memory tables, which comes with certain caveats.

If you're using mnesia elsewhere in your application, you should be aware that Citrine will attempt to join existing mnesia clusters.

What Citrine can and cannot do

Citrine is not a perfect solution for all scheduling needs. There are certain features Citrine does not implement, or even try to implement, and if you require these features you should either look elsewhere or consider different patterns.

If you need stronger guarantees of execution, it's recommended that you use a combination of a stateful queueing service with a worker library, such as Exq.

If you're okay with the limitations of the library, Citrine is a great choice thanks to its simple interface, minimal dependencies and ease of operations.

Installation

The package can be installed by adding citrine to your list of dependencies in mix.exs:

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

Usage

Create a module for the core scheduler:

defmodule MyApp.Scheduler do
  use Citrine.Scheduler, otp_app: :myapp
end

Add the scheduler to your supervisor tree, for example in application.ex:

defmodule MyApp.Application do
  use Application
  def start(_type, _args) do
    children = [
      # Start your Citrine scheduler
      MyApp.Scheduler
    ]
    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Use the scheduler to start and stop a job:

# Create a job
job = %Citrine.Job{
  id: "my_job_id",
  schedule: "* * * * * *", # Run every second
  task: job_task,
  extended_syntax: true # Use extended cron syntax
}
# Start or update a job
MyApp.Scheduler.put_job(job)
# Terminate and delete a job
MyApp.Scheduler.delete_job(job)

For further details, see HexDocs.