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

前端 未结 7 1337
臣服心动
臣服心动 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!

提交回复
热议问题