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

Access to job definition while running #472

Closed dantswain closed 2 years ago

dantswain commented 3 years ago

Hi! I was wondering if there’s any way for a job to have access to its own definition when it is running. I scanned the docs and didn’t find anything.

My use case is that I have a few jobs where the function called is the same but the schedule is different (e.g., same job runs at 10 AM and 3 PM). In those jobs I’d like to do something that is parameterized by when the job is run (could be as simple as logging “the 3PM foo job is running”). Obviously I could pass this information in as an argument in the configuration but that seems error prone (suppose I change the 3PM job to run at 2 PM and forget to change one of the settings).

I had a couple ideas about how this could be implemented. One is to have a dummy parameter for the arguments - e.g. [“arg1”, :quantum_job_definition] where :quantum_job_definition is replaced on-the-fly by the actual Quantum.Job. Another idea is to have a registry mapping job pid to job definition. This way the job could simply call something like Quantum.get_running_job(self()) and have that return the Quantum.Job.

If this is already possible or there’s a reasonable workaround then that’s awesome. If not I would be willing to help with a PR. Thanks!

maennchen commented 2 years ago

@dantswain Sorry for the very long wait for the answer.

Since this is possible easily via the arguments of the job function to call, I will not consider a PR for this.