简体   繁体   中英

Elixir: Scheduled jobs not running Mix task after the first call

I'm using Quantum to handle cron jobs. The setting is the following:

application.ex

def start
  ...
  children = [ 
    ...
    worker(MyApp.Scheduler, [])
  ]
  opts = [strategy: :one_for_one, name: MyApp.Supervisor]
  Supervisor.start_link(children, opts)
end

config.exs

config :My_app, MyApp.Scheduler,
  jobs: [
    {"*/5 * * * *",   fn -> Mix.Task.run "first_mix_task" end},
    {"*/5 * * * *",   fn -> Mix.Task.run "second_mix_task" end},
    {"*/5 * * * *",   fn -> Mix.Task.run "third_mix_task" end},
    {"*/5 * * * *",   fn -> Mix.Task.run "fourth_mix_task" end}
  ]

The problem is, for some reason, Mix tasks run only the first time after cron jobs are added. Later, although I can see in the logs crons are started and ended (according to Quantum), Mix tasks are never triggered.

I'm not including the mix tasks here because they work fine the first run and also when called from console. So I think the issue has to be in the settings I'm including here. But if you have a good reason to look there just let me know.

Mix.Task.run/1 only executes a task the first time it's called, unless it is re-enabled.

Runs a task with the given args.

If the task was not yet invoked, it runs the task and returns the result.

If there is an alias with the same name, the alias will be invoked instead of the original task.

If the task or alias were already invoked, it does not run them again and simply aborts with :noop .

https://hexdocs.pm/mix/Mix.Task.html#run/2

You can use Mix.Task.rerun/1 instead of Mix.Task.run/1 to re-enable and invoke the task again:

...
{"*/5 * * * *", fn -> Mix.Task.rerun "first_mix_task" end},
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM