How to set stop date and time?
ai-ml-with-kapil opened this issue · 5 comments
How to pass parameter for a job ? My job is recurring I mean a Task must run every 3 minutes. Please guide me.
It is a nice question.
Here is an example of doing it, and you can change Second(3)
to Minute(3)
using JobSchedulers
scheduler_start()
# define your function
function recurring_function()
println(now(), " - call recurring function")
# call the function itself every 3 second
job = Job(recurring_function, schedule_time = Second(3), name = "Recurring")
submit!(job)
end
recurring_function()
# to cancel it
cancel!(queue("Recurring")[1])
# or to cancel it at a specific time
function cancel_recurring()
try
recurring_job = queue(r"^Recurring$", :queuing)[1] # use regex ^ and $ to exact match the job name
println("Cancel recurring job")
state = cancel!(recurring_job)
if state !== :cancelled
# recurring_function successfully ran (state is :done), and new recurring function is produced.
job_cancel = Job(cancel_recurring, name = "Cancel recurring")
submit!(job_cancel)
end
catch
# redo if recurring_job is running
job_cancel = Job(cancel_recurring, name = "Cancel recurring")
submit!(job_cancel)
end
end
# wrap cancel_recurring into Job, and set up a time to cancel it.
job_cancel = Job(cancel_recurring, schedule_time = now() + Second(10), name = "Cancel recurring")
submit!(job_cancel)
Though it is a little bit complicated, I am also considering adding native support for recurring jobs into JobSchedulers. Hope it won't take long.
If you have other jobs to run, you can change the job priority: Job(xxx, priority = 0)
.
Hi, This was speedy support from your side. I just wanted to let you know that this works for me. Can we also have some method to run the service at a fixed time? coz now()+ Seconds(10) always depends on starting time of script.
I want to run every 5 minutes but start from a specific time of day.
now() + Second(5) is a DateTime object. schedule_time accepts DateTime and Period types. You can create a DateTime on your own. See the julia document https://docs.julialang.org/en/v1/stdlib/Dates/
Hi @ai-ml-with-kapil ,
Recurring jobs are supported in JobSchedulers v0.8. Thank you for the suggestion.
It is inspired by Linux-based crontab, allowing you to run a recurring job at specific date and time.
# JobSchedulers >= v0.8
using JobSchedulers
# define your function
job = Job(() -> println(now(), " - call recurring function"),
cron = Cron(0, "*/3", *, *, *, *), # run every 3 minutes
schedule_time = DateTime(2023,5,21,11,03), # run the first job at this time.
until = Month(1) # job will stop repeat until the DateTime
)
submit!(job)