Schedule jobs at particular date and/or time

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

1 Like

probably i found the solution:

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

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.

2 Likes

@vjFaLk thx for the hint …what u think about using GitHub - rq/rq-scheduler: A lightweight library that adds job scheduling capabilities to RQ (Redis Queue) ? …seems to be a nice solution to be used inside server side script.

@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

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 : erpnext/hooks.py at develop · frappe/erpnext · GitHub

@vjFaLk at what time will run the daily schedule?

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.

@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 …

3 Likes

@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)

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.

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

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

Thanks. its working for me

It doesn’t work well for me; can i ask your help?
my post is here

Thanks

it is not available??

can you send the code for send auto mail at specific time?

@Rehan_Ansari , Have you found the solution?

@Suresh_Thakor:

Auto email report uses “daily_long” basis, but you can change in hooks.py to send it at specific hour.
In this example automail will be generated at 11.00am everyday.

(use bench migrate after change it)

scheduler_events = {
	"cron": {
		"0/15 * * * *": [
			"frappe.oauth.delete_oauth2_data",
			"frappe.website.doctype.web_page.web_page.check_publish_status",
			"frappe.twofactor.delete_all_barcodes_for_users",
		],
		"0/10 * * * *": [
			"frappe.email.doctype.email_account.email_account.pull",
		],
		# Hourly but offset by 30 minutes
		"30 * * * *": [
			"frappe.core.doctype.prepared_report.prepared_report.expire_stalled_report",
		],
		# Daily but offset by 45 minutes
		"45 0 * * *": [
			"frappe.core.doctype.log_settings.log_settings.run_log_clean_up",
		],
        # Daily at 11 am
        "0 11 * * *": [
            "frappe.email.doctype.auto_email_report.auto_email_report.send_daily",
		],
	},

Hope this helps.

1 Like

@avc , Thanks. it works.

1 Like