Best way to create scheduled background task?

Hello everyone :grin:

Previously I had a doctype
Study Schedule:
Date
Start Time
End Time

My current case is, I want to create a background task to send a push notification one day before the study schedule starts, but I’m confused about how to do it. I’ve tried:

  1. Reading the documentation about enqueue and schedular event (Background Jobs), but what I’m confused about is, in enqueue there is no scheduling, there is only short, default, and long, and schedular event, will run in a period of time such as per day, week, month, etc.
  2. I tried asking Ai but the answer uses enqueue_at, but I didn’t find any documentation about enqueue_at

So what should I use? Thank you very much

You can use a scheduler event to run tasks at specific times using cron expressions. For your case, where you want to send a push notification one day before the study schedule starts, you can schedule a task like this:

  1. In the scheduler_events section of your app’s hooks.py file, add a cron job.
  2. The cron expression allows you to set when the task should run. For example, if you want the task to run at midnight (12:00 AM) on the 27th of every month, you can use the following setup:
scheduler_events = {
    "cron": {
        # This runs at midnight (12:00 AM) on the 27th of every month
        "0 0 27 * *": [
            "my_app.my_app.doctype.study_schedule.study_schedule.send_push_notification"
        ]
    }
}

  • The cron expression "0 0 27 * *" means “run at 12:00 AM on the 27th of each month”.
  • Replace send_push_notification with your actual method to send the push notification, and you can adjust the cron expression based on your desired schedule (for example, a day before the study schedule starts).

To know the exact time and day to run your cron job, you can use a tool like CronGuru. It’s a great online tool where you can input the schedule (e.g., “Run at 12:00 AM every day” or “Run on the 27th of each month”), and it will generate the correct cron expression for you. This way, you can configure your cron job accurately without worrying about getting the expression wrong!

This means that the function that will be run by the cron job, for example every day, is…

Thank you so much, but how do i check if my scheduler event is active or not? or check scheduler event list?

For Testing on your Local,
Go in Scheduled Job Type DocType there you can see all the jobs. you can execute from there if you want test and can also check logs in Scheduled Job Log DocType.