quantum-elixir / quantum-core

:watch: Cron-like job scheduler for Elixir
https://hexdocs.pm/quantum/
Apache License 2.0
2.3k stars 147 forks source link

Error when deleting job in runtime #479

Closed thiagohdeplima closed 2 years ago

thiagohdeplima commented 3 years ago

We are using Quantum to write an application that manages scheduling and de-scheduling of tasks in runtime only.

When we are removing a Job by calling Transformer.Scheduler.delete_job/1 (that uses Quantum), the ExecutionBroadcaster are exiting with the following error:

01:41:13.857 [error] GenServer Transformer.Scheduler.ExecutionBroadcaster terminating
** (FunctionClauseError) no function clause matching in Quantum.ExecutionBroadcaster.handle_event/2
    (quantum 3.3.0) lib/quantum/execution_broadcaster.ex:98: Quantum.ExecutionBroadcaster.handle_event({:delete, %Quantum.Job{name: :"741f01e9-890c-4f37-b3a9-e17268376f79", overlap: true, run_strategy: %Quantum.RunStrategy.Random{nodes: :cluster}, schedule: ~e[* * * * * *], state: :active, task: #Function<4.24704503/0 in Transformer.StateManager.Creator.schedule_job/2>, timezone: :utc}}, %Quantum.ExecutionBroadcaster.State{debug_logging: true, execution_timeline: [], scheduler: Transformer.Scheduler, storage: Quantum.Storage.Noop, storage_pid: nil, uninitialized_jobs: [%Quantum.Job{name: :"741f01e9-890c-4f37-b3a9-e17268376f79", overlap: true, run_strategy: %Quantum.RunStrategy.Random{nodes: :cluster}, schedule: ~e[* * * * * *], state: :active, task: #Function<4.24704503/0 in Transformer.StateManager.Creator.schedule_job/2>, timezone: :utc}, %Quantum.Job{name: :"56b83fe4-39d0-4018-a7d3-7673520349aa", overlap: true, run_strategy: %Quantum.RunStrategy.Random{nodes: :cluster}, schedule: ~e[* * * * * *], state: :active, task: #Function<4.24704503/0 in Transformer.StateManager.Creator.schedule_job/2>, timezone: :utc}, %Quantum.Job{name: :"a801eb01-141c-4e87-a938-ae795f5f292c", overlap: true, run_strategy: %Quantum.RunStrategy.Random{nodes: :cluster}, schedule: ~e[* * * * * *], state: :active, task: #Function<4.24704503/0 in Transformer.StateManager.Creator.schedule_job/2>, timezone: :utc}, %Quantum.Job{name: :"925b30ec-6ddd-4f11-9f40-0c316a77f8ec", overlap: true, run_strategy: %Quantum.RunStrategy.Random{nodes: :cluster}, schedule: ~e[* * * * * *], state: :active, task: #Function<0.108233933/0 in Transformer.StateManager.StatusChanger.change_quantum_task/2>, timezone: :utc}]})
    (quantum 3.3.0) lib/quantum/execution_broadcaster.ex:91: anonymous fn/2 in Quantum.ExecutionBroadcaster.handle_events/3
    (elixir 1.12.1) lib/enum.ex:2356: Enum."-reduce/3-lists^foldl/2-0-"/3
    (quantum 3.3.0) lib/quantum/execution_broadcaster.ex:90: Quantum.ExecutionBroadcaster.handle_events/3
    (gen_stage 1.1.0) lib/gen_stage.ex:2462: GenStage.consumer_dispatch/6
    (gen_stage 1.1.0) lib/gen_stage.ex:2641: GenStage.take_pc_events/3
    (stdlib 3.15.1) gen_server.erl:695: :gen_server.try_dispatch/4
    (stdlib 3.15.1) gen_server.erl:771: :gen_server.handle_msg/6
    (stdlib 3.15.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message: {:"$gen_consumer", {#PID<0.466.0>, #Reference<0.3177743090.3438804995.214648>}, [delete: %Quantum.Job{name: :"741f01e9-890c-4f37-b3a9-e17268376f79", overlap: true, run_strategy: %Quantum.RunStrategy.Random{nodes: :cluster}, schedule: ~e[* * * * * *], state: :active, task: #Function<4.24704503/0 in Transformer.StateManager.Creator.schedule_job/2>, timezone: :utc}, add: %Quantum.Job{name: :"741f01e9-890c-4f37-b3a9-e17268376f79", overlap: true, run_strategy: %Quantum.RunStrategy.Random{nodes: :cluster}, schedule: ~e[* * * * * *], state: :active, task: #Function<0.108233933/0 in Transformer.StateManager.StatusChanger.change_quantum_task/2>, timezone: :utc}]}

Reading the source code seems that Quantum.ExecutionBroadcaster.handle_event/2 expects to receive the event :remove and no :delete.

I'll try to send a PR for it, but, if someone used to Quantum code and with the kindest heart can do it before I, I think the community will be very thankful :laughing: