创建量子灵药工作

问题描述:

我想创建一个量子灵药工作并控制它执行的时间。我开始与此:创建量子灵药工作

Quantum.Job.new(task: fn -> IO.puts "Hello!" end) 

但它抛出这个错误:

** (KeyError) key :run_strategy not found in: [task: #Function<20.99386804/0 in :erl_eval.expr/5>] 
    (elixir) lib/keyword.ex:371: Keyword.fetch!/2 
    (quantum) lib/quantum/job.ex:58: Quantum.Job.new/1 

我真的不明白run_strategy设置,这是可能的值。

有人知道如何使用Quantum.Job.new的结构或类似的东西来创建一个量子作业吗?

注:cron是使用这个已经工作:

config :sopitas, Sopitas.Scheduler, 
    jobs: [ 
    # Every minute 
    {"@minutely", {Sopitas.Admin.FixtureController, :update_cron, []}}, 
    {"@minutely", {Sopitas.Admin.StandingController, :update_cron, []}}, 
    {"@daily", {Sopitas.Admin.LeagueController, :update_cron, []}}, 
    ] 

但有一点是改变执行的cron的时间,这就是为什么我决定使用Quantum.Job.new

创建

Quantum.Job文档的第一句话说:

The struct should never be defined by hand. Use Acme.Scheduler.new_job/0 to create a new job and use the setters mentioned below to mutate the job.

Quantum.Job本身就是一个structthree mandatory keys

@enforce_keys [:run_strategy, :overlap, :timezone] 

如果你仍然为正,你要违反他们的指引,你应该通过这三个关键词,如:

Quantum.Job.new(
    task: fn -> IO.puts "Hello!" end, 
    run_strategy: %Quantum.RunStrategy.All{nodes: [:one, :two]}, 
    timezone: "Europe/Zurich", 
    overlap: false 
) 

我会建议你使用提供here一个例子,虽然。

+0

你”问题是我无法使用Acme.Scheduler模块 'iex(6)> Acme.Scheduler.config |> Quantum.Job.new **(UndefinedFunctionError)函数Acm e.Scheduler.config/0未定义(模块Acme.Scheduler不可用) Acme.Scheduler.config() ' 我找不到与此错误相关的内容。 –

+0

你是否将它作为'iex -S mix'运行? – mudasobwa

+0

只要这些第一次尝试,一旦它工作,我将创建一个函数作为我的项目中的一个模块的一部分。你建议我采取其他策略吗? –

解决方案比我想象的要容易。量子文件说:

config :your_app, YourApp.Scheduler, jobs: [
     # Every minute
     {"* * * * ", {Heartbeat, :send, []}},
     # Every 15 minutes
     {"
/15 * * * *", fn -> System.cmd("rm", ["/tmp/tmp_"]) end},
     # Runs on 18, 20, 22, 0, 2, 4, 6:
     {"0 18-6/2 * * *", fn -> :mnesia.backup('/var/backup/mnesia') end},
     # Runs every midnight:
     {"@daily", {Backup, :backup, []}}
]

我的名单改变了元组,所以我可以说出每个作业。我可以用自己的名字获取和改变他们的日程安排:

config :sopitas, Sopitas.Scheduler, 
    jobs: [ 
    [name: :redis_cleaning, schedule: "@daily", task: {Sopitas.NotificationController, :clear_redis, []}], 
    [name: :fixtures, schedule: "@daily", task: {Sopitas.Admin.FixtureController, :update_cron, []}], 
    [name: :standings, schedule: "@daily", task: {Sopitas.Admin.StandingController, :update_cron, []}], 
    [name: :leagues, schedule: "@daily", task: {Sopitas.Admin.LeagueController, :update_cron, []}], 
    # [name: :example_cron, schedule: "@minutely", task: fn -> IO.puts "Hello!" end], 
    ] 

我创建了处理工作的函数:

def change_cron(name, cron_expression) do 
    job = Sopitas.Scheduler.find_job(name) 
    unless job do 
     Quantum.Job.set_schedule(job, Crontab.CronExpression.Parser.parse! cron_expression) 
    end 
    end 

我调用该函数如下:

Sopitas.SportsController.change_cron :fixtures, "@daily" 
Sopitas.SportsController.change_cron :fixtures, "@minutely"