apscheduler
apscheduler copied to clipboard
Feature Request: Manual start of a job
Is it possible to add an API to the scheduler to run an existing job. For example: a periodic task is scheduled every day. I'd like to give the ability of the user of start this job manually whenever it wants.
If it is a long task and max_instances=1
is set, for instance, let if it was started by the normal schedule, at max 1 instance is started and any time.
Example:
scheduler.schedule_job(job_id)
Ref: #241
I'd rather name it start_job()
but otherwise this is fine.
and a stop_job to interrup a running job (probably not the same as pause/unpause, which only impacts the scheduler)
I'm not sure about that, because how do you stop a job that's running in a worker thread?
Kill the thread ? At least document on best practice. I end up always doing the same : putting a stop Boolean, checked during the job. And a timeout (if not stopped after n seconds, kill the job)
There is not way to kill threads in Python. So what would you expect stop_job()
to do in that case? Put up a flag somewhere (where?) which the user code will voluntarily check and stop itself when it's set?
stop flag is ok. So it is a user story, not an Apscheduler stuff. Please forget it
I'm currently looking on how to do that, but I have no idea... Any workarounds before version 4.0 comes out?
My workaround is to call the job directly.
async def start_job(scheduler, job_id: str):
job = scheduler.get_job(job_id)
result = await job.func(*job.args, **job.kwargs)
return result
An other way is to call add_job again with fields of the original except id and trigger. The parameter trigger=None is the same as DateTrigger(), and the job will be procced immediately.
async def start_job(scheduler, job_id: str):
job = scheduler.get_job(job_id)
scheduler.add_job(
func=job.func,
args=job.args,
kwargs=job.kwargs,
name=job.name,
misfire_grace_time=job.misfire_grace_time,
... # other fields except id and trigger
)
In v4.0 alpha, there's add_job()
and run_job()
which queue a job directly w/o scheduling. That should fulfill this feature request.