How can I schedule code to run every few hours in Elixir or Phoenix framework?

前端 未结 7 1290
臣服心动
臣服心动 2020-11-29 14:21

So let\'s say I want to send a bunch of emails or recreate sitemap or whatever every 4 hours, how would I do that in Phoenix or just with Elixir?

相关标签:
7条回答
  • 2020-11-29 14:59

    You can use erlcron for that. You use it like

    job = {{:weekly, :thu, {2, :am}},
      {:io, :fwrite, ["It's 2 Thursday morning~n"]}}
    
    :erlcron.cron(job)
    

    A job is a 2-element tuple. The first element is a tuple that represents the schedule for the job and the second element is the function or an MFA(Module, Function, Arity). In the above example, we run :io.fwrite("It's 2 Thursday morning") every 2am of Thursday.

    Hope that helps!

    0 讨论(0)
  • 2020-11-29 15:00

    There is a simple alternative that does not require any external dependencies:

    defmodule MyApp.Periodically do
      use GenServer
    
      def start_link do
        GenServer.start_link(__MODULE__, %{})
      end
    
      def init(state) do
        schedule_work() # Schedule work to be performed at some point
        {:ok, state}
      end
    
      def handle_info(:work, state) do
        # Do the work you desire here
        schedule_work() # Reschedule once more
        {:noreply, state}
      end
    
      defp schedule_work() do
        Process.send_after(self(), :work, 2 * 60 * 60 * 1000) # In 2 hours
      end
    end
    

    Now in your supervision tree:

    worker(MyApp.Periodically, [])
    
    0 讨论(0)
  • 2020-11-29 15:00

    Quantum lets you create, find and delete jobs at runtime.

    Furthermore, you can pass arguments to the task function when creating a cronjob, and even modify the timezone if you're not happy with UTC.

    If your app is running as multiple isolated instances (e.g. Heroku), there are job processors backed by PostgreSQL or Redis, that also support task scheduling:

    Oban: https://github.com/sorentwo/oban

    Exq: https://github.com/akira/exq

    Toniq: https://github.com/joakimk/toniq

    Verk: https://github.com/edgurgel/verk

    0 讨论(0)
  • 2020-11-29 15:07

    Quantum is great, we use it at work as a cron replacement with a phoenix front-end and we also add jobs in real-time which is very neat.

    0 讨论(0)
  • 2020-11-29 15:08

    Besides to use Process.send_after, you can also use :timer.apply_interval.

    0 讨论(0)
  • 2020-11-29 15:10

    I used Quantum library Quantum- Elixir.
    Follow below instructions.

    #your_app/mix.exs
    defp deps do
      [{:quantum, ">= 1.9.1"},  
      #rest code
    end
    
    
    
    #your_app/mix.exs
    def application do
      [mod: {AppName, []},
       applications: [:quantum,
       #rest code         
     ]]
    end
    
    #your_app/config/dev.exs
    config :quantum, :your_app, cron: [
      # Every minute
      "* * * * *": fn -> IO.puts("Hello QUANTUM!") end
    ]
    

    All set. Start the server by running below command.

    iex -S mix phoenix.server 
    
    0 讨论(0)
提交回复
热议问题