feat: Allow setting cron as Server Script frequency

This commit is contained in:
Gavin D'souza 2023-05-27 16:49:13 +05:30
parent c0d5e16b3f
commit b00aac92ba
No known key found for this signature in database
GPG key ID: 3A7BF4D4340DE6F7
2 changed files with 15 additions and 4 deletions

View file

@ -9,6 +9,7 @@
"script_type",
"reference_doctype",
"event_frequency",
"cron_format",
"doctype_event",
"api_method",
"allow_guest",
@ -99,7 +100,7 @@
"fieldtype": "Select",
"label": "Event Frequency",
"mandatory_depends_on": "eval:doc.script_type == \"Scheduler Event\"",
"options": "All\nHourly\nDaily\nWeekly\nMonthly\nYearly\nHourly Long\nDaily Long\nWeekly Long\nMonthly Long"
"options": "All\nHourly\nDaily\nWeekly\nMonthly\nYearly\nHourly Long\nDaily Long\nWeekly Long\nMonthly Long\nCron"
},
{
"fieldname": "module",
@ -132,6 +133,12 @@
"fieldname": "rate_limit_seconds",
"fieldtype": "Int",
"label": "Time Window (Seconds)"
},
{
"depends_on": "eval:doc.event_frequency==='Cron'",
"fieldname": "cron_format",
"fieldtype": "Data",
"label": "Cron Format"
}
],
"index_web_pages_for_search": 1,
@ -141,7 +148,7 @@
"link_fieldname": "server_script"
}
],
"modified": "2023-05-16 11:03:58.282680",
"modified": "2023-05-27 16:33:16.595424",
"modified_by": "Administrator",
"module": "Core",
"name": "Server Script",

View file

@ -52,7 +52,9 @@ class ServerScript(Document):
def sync_scheduler_events(self):
"""Create or update Scheduled Job Type documents for Scheduler Event Server Scripts"""
if not self.disabled and self.event_frequency and self.script_type == "Scheduler Event":
setup_scheduler_events(script_name=self.name, frequency=self.event_frequency)
setup_scheduler_events(
script_name=self.name, frequency=self.event_frequency, cron_format=self.cron_format
)
def clear_scheduled_events(self):
"""Deletes existing scheduled jobs by Server Script if self.event_frequency has changed"""
@ -171,7 +173,7 @@ class ServerScript(Document):
return items
def setup_scheduler_events(script_name, frequency):
def setup_scheduler_events(script_name: str, frequency: str, cron_format: str | None = None):
"""Creates or Updates Scheduled Job Type documents based on the specified script name and frequency
Args:
@ -188,6 +190,7 @@ def setup_scheduler_events(script_name, frequency):
"method": method,
"frequency": frequency,
"server_script": script_name,
"cron_format": cron_format,
}
).insert()
@ -200,6 +203,7 @@ def setup_scheduler_events(script_name, frequency):
return
doc.frequency = frequency
doc.cron_format = cron_format
doc.save()
frappe.msgprint(_("Scheduled execution for script {0} has updated").format(script_name))