feat: configure background sync frequency
This commit is contained in:
parent
094869d2e7
commit
24f800c8e2
39
crm/hooks.py
39
crm/hooks.py
@ -169,23 +169,28 @@ doc_events = {
|
||||
# Scheduled Tasks
|
||||
# ---------------
|
||||
|
||||
# scheduler_events = {
|
||||
# "all": [
|
||||
# "crm.tasks.all"
|
||||
# ],
|
||||
# "daily": [
|
||||
# "crm.tasks.daily"
|
||||
# ],
|
||||
# "hourly": [
|
||||
# "crm.tasks.hourly"
|
||||
# ],
|
||||
# "weekly": [
|
||||
# "crm.tasks.weekly"
|
||||
# ],
|
||||
# "monthly": [
|
||||
# "crm.tasks.monthly"
|
||||
# ],
|
||||
# }
|
||||
scheduler_events = {
|
||||
"daily": [
|
||||
"crm.tasks.sync_leads_from_sources_daily"
|
||||
],
|
||||
"hourly": [
|
||||
"crm.tasks.sync_leads_from_sources_hourly"
|
||||
],
|
||||
"monthly": [
|
||||
"crm.tasks.sync_leads_from_sources_monthly"
|
||||
],
|
||||
"cron": {
|
||||
"*/5 * * * *": [
|
||||
"crm.tasks.sync_leads_from_sources_5_minutes"
|
||||
],
|
||||
"*/10 * * * *": [
|
||||
"crm.tasks.sync_leads_from_sources_10_minutes"
|
||||
],
|
||||
"*/15 * * * *": [
|
||||
"crm.tasks.sync_leads_from_sources_15_minutes"
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
# Testing
|
||||
# -------
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
"column_break_lwcw",
|
||||
"last_synced_at",
|
||||
"enabled",
|
||||
"background_sync_frequency",
|
||||
"facebook_section",
|
||||
"facebook_page",
|
||||
"column_break_zukm",
|
||||
@ -69,12 +70,19 @@
|
||||
{
|
||||
"fieldname": "column_break_zukm",
|
||||
"fieldtype": "Column Break"
|
||||
},
|
||||
{
|
||||
"default": "Hourly",
|
||||
"fieldname": "background_sync_frequency",
|
||||
"fieldtype": "Select",
|
||||
"label": "Background Sync Frequency",
|
||||
"options": "Every 5 Minutes\nEvery 10 Minutes\nEvery 15 Minutes\nHourly\nDaily\nMonthly"
|
||||
}
|
||||
],
|
||||
"grid_page_length": 50,
|
||||
"index_web_pages_for_search": 1,
|
||||
"links": [],
|
||||
"modified": "2025-10-08 12:09:28.101399",
|
||||
"modified": "2025-10-08 12:23:22.097933",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Lead Syncing",
|
||||
"name": "Lead Sync Source",
|
||||
|
||||
@ -17,6 +17,9 @@ class LeadSyncSource(Document):
|
||||
from frappe.types import DF
|
||||
|
||||
access_token: DF.Password | None
|
||||
background_sync_frequency: DF.Literal[
|
||||
"Every 5 Minutes", "Every 10 Minutes", "Every 15 Minutes", "Hourly", "Daily", "Monthly"
|
||||
]
|
||||
enabled: DF.Check
|
||||
facebook_lead_form: DF.Link | None
|
||||
facebook_page: DF.Link | None
|
||||
@ -34,7 +37,10 @@ class LeadSyncSource(Document):
|
||||
if not self.facebook_lead_form:
|
||||
return
|
||||
|
||||
already_active = frappe.db.exists("Lead Sync Source", {"enabled": 1, "facebook_lead_form": self.facebook_lead_form})
|
||||
already_active = frappe.db.exists(
|
||||
"Lead Sync Source",
|
||||
{"enabled": 1, "facebook_lead_form": self.facebook_lead_form, "name": ["!=", self.name]},
|
||||
)
|
||||
|
||||
if already_active:
|
||||
frappe.throw(frappe._("A lead sync source is already enabled for this Facebook Lead Form!"))
|
||||
@ -46,6 +52,9 @@ class LeadSyncSource(Document):
|
||||
|
||||
@frappe.whitelist()
|
||||
def sync_leads(self):
|
||||
frappe.enqueue_doc(self.doctype, self.name, "_sync_leads", queue="long")
|
||||
|
||||
def _sync_leads(self):
|
||||
if self.type == "Facebook" and self.access_token:
|
||||
if not self.facebook_lead_form:
|
||||
frappe.throw(frappe._("Please select a lead gen form before syncing!"))
|
||||
|
||||
32
crm/tasks.py
Normal file
32
crm/tasks.py
Normal file
@ -0,0 +1,32 @@
|
||||
import frappe
|
||||
|
||||
|
||||
def sync_leads_from_all_enabled_sources(frequency: str | None = None) -> None:
|
||||
enabled_sources = frappe.get_all(
|
||||
"Lead Sync Source", filters={"enabled": 1, "background_sync_frequency": frequency}, pluck="name"
|
||||
)
|
||||
for source in enabled_sources:
|
||||
lead_sync_source = frappe.get_doc("Lead Sync Source", source)
|
||||
try:
|
||||
lead_sync_source._sync_leads()
|
||||
except Exception as _:
|
||||
frappe.log_error(f"Error syncing leads for source {source}")
|
||||
|
||||
|
||||
def sync_leads_from_sources_5_minutes() -> None:
|
||||
sync_leads_from_all_enabled_sources("Every 5 Minutes")
|
||||
|
||||
def sync_leads_from_sources_10_minutes() -> None:
|
||||
sync_leads_from_all_enabled_sources("Every 10 Minutes")
|
||||
|
||||
def sync_leads_from_sources_15_minutes() -> None:
|
||||
sync_leads_from_all_enabled_sources("Every 15 Minutes")
|
||||
|
||||
def sync_leads_from_sources_hourly() -> None:
|
||||
sync_leads_from_all_enabled_sources("Hourly")
|
||||
|
||||
def sync_leads_from_sources_daily() -> None:
|
||||
sync_leads_from_all_enabled_sources("Daily")
|
||||
|
||||
def sync_leads_from_sources_monthly() -> None:
|
||||
sync_leads_from_all_enabled_sources("Monthly")
|
||||
Loading…
x
Reference in New Issue
Block a user