Elixir:计划的作业不是 运行 第一次调用后的混合任务
Elixir: Scheduled jobs not running Mix task after the first call
我正在使用 Quantum 来处理 cron 作业。设置如下:
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}
]
问题是,出于某种原因,混合任务 运行 仅在添加 cron 作业后的第一次。后来,虽然我可以在日志中看到 crons 被启动和结束(根据 Quantum),但 Mix 任务从未被触发。
我没有在此处包括混合任务,因为它们在第一个 运行 以及从控制台调用时都运行良好。所以我认为问题必须出在我在此处包含的设置中。但如果你有充分的理由去那里看看,请告诉我。
Mix.Task.run/1
仅在第一次调用时执行任务,除非重新启用。
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
您可以使用 Mix.Task.rerun/1
而不是 Mix.Task.run/1
来重新启用和调用任务:
...
{"*/5 * * * *", fn -> Mix.Task.rerun "first_mix_task" end},
...
我正在使用 Quantum 来处理 cron 作业。设置如下:
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}
]
问题是,出于某种原因,混合任务 运行 仅在添加 cron 作业后的第一次。后来,虽然我可以在日志中看到 crons 被启动和结束(根据 Quantum),但 Mix 任务从未被触发。
我没有在此处包括混合任务,因为它们在第一个 运行 以及从控制台调用时都运行良好。所以我认为问题必须出在我在此处包含的设置中。但如果你有充分的理由去那里看看,请告诉我。
Mix.Task.run/1
仅在第一次调用时执行任务,除非重新启用。
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
您可以使用 Mix.Task.rerun/1
而不是 Mix.Task.run/1
来重新启用和调用任务:
...
{"*/5 * * * *", fn -> Mix.Task.rerun "first_mix_task" end},
...