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
|
# Scheduled Tasks
|
||||||
# ---------------
|
# ---------------
|
||||||
|
|
||||||
# scheduler_events = {
|
scheduler_events = {
|
||||||
# "all": [
|
"daily": [
|
||||||
# "crm.tasks.all"
|
"crm.tasks.sync_leads_from_sources_daily"
|
||||||
# ],
|
],
|
||||||
# "daily": [
|
"hourly": [
|
||||||
# "crm.tasks.daily"
|
"crm.tasks.sync_leads_from_sources_hourly"
|
||||||
# ],
|
],
|
||||||
# "hourly": [
|
"monthly": [
|
||||||
# "crm.tasks.hourly"
|
"crm.tasks.sync_leads_from_sources_monthly"
|
||||||
# ],
|
],
|
||||||
# "weekly": [
|
"cron": {
|
||||||
# "crm.tasks.weekly"
|
"*/5 * * * *": [
|
||||||
# ],
|
"crm.tasks.sync_leads_from_sources_5_minutes"
|
||||||
# "monthly": [
|
],
|
||||||
# "crm.tasks.monthly"
|
"*/10 * * * *": [
|
||||||
# ],
|
"crm.tasks.sync_leads_from_sources_10_minutes"
|
||||||
# }
|
],
|
||||||
|
"*/15 * * * *": [
|
||||||
|
"crm.tasks.sync_leads_from_sources_15_minutes"
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# Testing
|
# Testing
|
||||||
# -------
|
# -------
|
||||||
|
|||||||
@ -11,6 +11,7 @@
|
|||||||
"column_break_lwcw",
|
"column_break_lwcw",
|
||||||
"last_synced_at",
|
"last_synced_at",
|
||||||
"enabled",
|
"enabled",
|
||||||
|
"background_sync_frequency",
|
||||||
"facebook_section",
|
"facebook_section",
|
||||||
"facebook_page",
|
"facebook_page",
|
||||||
"column_break_zukm",
|
"column_break_zukm",
|
||||||
@ -69,12 +70,19 @@
|
|||||||
{
|
{
|
||||||
"fieldname": "column_break_zukm",
|
"fieldname": "column_break_zukm",
|
||||||
"fieldtype": "Column Break"
|
"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,
|
"grid_page_length": 50,
|
||||||
"index_web_pages_for_search": 1,
|
"index_web_pages_for_search": 1,
|
||||||
"links": [],
|
"links": [],
|
||||||
"modified": "2025-10-08 12:09:28.101399",
|
"modified": "2025-10-08 12:23:22.097933",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Lead Syncing",
|
"module": "Lead Syncing",
|
||||||
"name": "Lead Sync Source",
|
"name": "Lead Sync Source",
|
||||||
|
|||||||
@ -17,6 +17,9 @@ class LeadSyncSource(Document):
|
|||||||
from frappe.types import DF
|
from frappe.types import DF
|
||||||
|
|
||||||
access_token: DF.Password | None
|
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
|
enabled: DF.Check
|
||||||
facebook_lead_form: DF.Link | None
|
facebook_lead_form: DF.Link | None
|
||||||
facebook_page: DF.Link | None
|
facebook_page: DF.Link | None
|
||||||
@ -34,7 +37,10 @@ class LeadSyncSource(Document):
|
|||||||
if not self.facebook_lead_form:
|
if not self.facebook_lead_form:
|
||||||
return
|
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:
|
if already_active:
|
||||||
frappe.throw(frappe._("A lead sync source is already enabled for this Facebook Lead Form!"))
|
frappe.throw(frappe._("A lead sync source is already enabled for this Facebook Lead Form!"))
|
||||||
@ -46,6 +52,9 @@ class LeadSyncSource(Document):
|
|||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def sync_leads(self):
|
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 self.type == "Facebook" and self.access_token:
|
||||||
if not self.facebook_lead_form:
|
if not self.facebook_lead_form:
|
||||||
frappe.throw(frappe._("Please select a lead gen form before syncing!"))
|
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