ERPNext Foundation ERPNext Cloud Chat Blog Discuss Frappé* Donate

Schedule jobs at particular date and/or time


#1

Hi all,

any hint on how to set up a schedule job to be executed at particular date and/or time? Let’s say every MON at 3 am?

Thx in advance


#2

probably i found the solution:

https://github.com/ui/rq-scheduler


When does a Daily Auto Email get sent? How can I tell if it fails?
#3

Here’s a quick and dirty solution. You can run any function you want using the bench execute command, and you can basically add this command to your user’s crontab to run at a particular time.

For example you add this entry to your crontab:

MAILTO=email@domain.com
* 3 * * 1 cd /home/frappe/frappe-bench && bench execute customapp.customapp.doctype.name.name.function

And this will execute a function every monday at 3 AM and send you an email if there is an error.


#4

@vjFaLk thx for the hint …what u think about using https://github.com/ui/rq-scheduler ? …seems to be a nice solution to be used inside server side script.


#5

@vjFaLk @JoEz,
I am also evaluating if there is a standard batch job/scheduler to automatically perform a function, say create sales invoice every night for all delivered sales items.

Is there any standard function on the desk or should this be a custom development?

Thanks


#6

There isn’t a standard way in the UI, but if you want to add a daily scheduled task, it’s pretty easy. Just add the method in the daily section in the hooks.py of your custom app.

Eg : https://github.com/frappe/erpnext/blob/develop/erpnext/hooks.py#L171


#7

@vjFaLk at what time will run the daily schedule?


#8

12 AM everyday. Regarding rq-scheduler, I have no hands on experience with it. What you can do is write code yourself to schedule jobs. We use something called the schedule package that enqueues our methods, you can do something similar if you’d like.


#9

@vjFaLk Thanks, i’ll have a look. rq-schedule sound promising for Periodic & Repeated Jobs:

scheduler.schedule(
    scheduled_time=datetime.utcnow(), # Time for first execution, in UTC timezone
    func=func,                     # Function to be queued
    args=[arg1, arg2],             # Arguments passed into function when executed
    kwargs={'foo': 'bar'},         # Keyword arguments passed into function when executed
    interval=60,                   # Time before the function is called again, in seconds
    repeat=10                      # Repeat this number of times (None means repeat forever)
)

or

scheduler.cron(
    cron_string,                # A cron string (e.g. "0 0 * * 0")
    func=func,                  # Function to be queued
    args=[arg1, arg2],          # Arguments passed into function when executed
    kwargs={'foo': 'bar'},      # Keyword arguments passed into function when executed
    repeat=10                   # Repeat this number of times (None means repeat forever)
    queue_name=queue_name       # In which queue the job should be put in
)

I’ll try to write an app for scheduling …


#10

@vjFaLk

Hi there, i’m done with my very first app …actually the meeting app in the screen cast on which i’ve added a cron task schedule to be able to schedule jobs at particular date/time.

Feel free to download and install from:

https://github.com/joezsweet/meeting

It’s my first attempt so take as it is :slight_smile:

  1. It use rq-scheduler, i’ve added it in requirements; in case it will not be installed, should be added manually using pip install rq-scheduler

  2. I didnt figure out how to have it run at start up :slight_smile:, so i’ve added it to Procfile in frappe-bench folder:

rqscheduler: rqscheduler -H localhost -p 11000 -v

It should work at list on develop branch :slight_smile:

Any hint, critics, help is very very welcome :slight_smile:

edit: added cron string validation and cancel job when task schedule is deleted (on_trash)


#11

Looks interesting! Unfortunately I’m too sleepy to understand what exactly happens. :laughing:

However, you could really functionify your code further. There’s a lot of repetition in the task_schedule.py file. You can condense the multiple else if statements easily.


Customize running time of scheduled jobs
#12

Can you please share the code to send email at 8am morning daily


#13

Hava a look at https://frappe.io/docs/user/en/tutorial/task-runner


#14

Thanks. its working for me