Giter Site home page Giter Site logo

ecto-job-scheduler's Introduction

EctoJobScheduler

CircleCI Hex.pm Hex.pm

Helpers for scheduling Jobs defined in EctoJob

Thanks joaothallis, ramondelemos, victorprs

Installation

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

def deps do
  [
    {:ecto_job_scheduler, "~> 0.6.0"}
  ]
end

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

Usage

Define job

defmodule MyApplication.MyJob do
  @moduledoc false
  use EctoJobScheduler.Job,
    repo: MyApplication.Repo,
    otp_app: :my_application

  def handle_job(%JobInfo{multi: _multi}, %{"input" => input}) do
    case input do
      :a -> :ok
      :b -> {:ok, :gotta_go_fast}
      other -> {:error, :xablau}
    end
  end
end

Define job queue

defmodule MyApplication.MyJobQueue do
  @moduledoc false
  use EctoJobScheduler.JobQueue,
    table_name: "test_jobs",
    jobs: [
      MyApplication.MyJob
    ]
end

Define scheduler module for job queue

defmodule MyApplication.MyJobScheduler do
  @moduledoc false
  use EctoJobScheduler.JobScheduler,
  repo: MyApplication.Repo,
  job_queue: MyApplication.MyJobQueue
end

Configuration

config :my_application, MyApplication.MyJob, max_attempts: "15"

Running and scheduling jobs

Enqueuing right away

{:ok, job_queue} = MyJobScheduler.run(MyJob, %{"input" => "a"})
{:ok, job_queue} = MyJobScheduler.run(MyJob, %{"input" => "a"}, schedule:  ~N[2022-10-03 12:00:00.000000]) # pass additional options

Appending to Ecto.Multi

  multi = Multi.run(:do_my_thing, fn _repo, _changes -> {:ok, :xablau} end)
  multi = MyJobScheduler.schedule(multi, MyJob, %{"input" => "a"})
  result = MyJobScheduler.run(multi)

ecto-job-scheduler's People

Contributors

rai200890 avatar victorprs 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.