def trigger(site, event, now=False): """trigger method in startup.schedule_handler""" from frappe.tasks import scheduler_task for handler in frappe.get_hooks("scheduler_events").get(event, []): if not check_lock(handler): if not now: scheduler_task.delay(site=site, event=event, handler=handler) create_lock(handler) else: create_lock(handler) scheduler_task(site=site, event=event, handler=handler, now=True)
def scheduler_task(site, event, handler, now=False): from frappe.utils.scheduler import log traceback = "" task_logger.info('running {handler} for {site} for event: {event}'.format(handler=handler, site=site, event=event)) try: frappe.init(site=site) if not create_lock(handler): return if not now: frappe.connect(site=site) frappe.get_attr(handler)() except Exception: frappe.db.rollback() traceback = log(handler, "Method: {event}, Handler: {handler}".format(event=event, handler=handler)) task_logger.warn(traceback) raise else: frappe.db.commit() finally: delete_lock(handler) if not now: frappe.destroy() task_logger.info('ran {handler} for {site} for event: {event}'.format(handler=handler, site=site, event=event))
def scheduler_task(site, event, handler, now=False): from frappe.utils.scheduler import log traceback = "" task_logger.info('running {handler} for {site} for event: {event}'.format( handler=handler, site=site, event=event)) try: frappe.init(site=site) if not create_lock(handler): return if not now: frappe.connect(site=site) frappe.get_attr(handler)() except Exception: frappe.db.rollback() traceback = log( handler, "Method: {event}, Handler: {handler}".format(event=event, handler=handler)) task_logger.warn(traceback) raise else: frappe.db.commit() finally: delete_lock(handler) if not now: frappe.destroy() task_logger.info('ran {handler} for {site} for event: {event}'.format( handler=handler, site=site, event=event))
def enqueue_events(site): if is_scheduler_disabled(): return # lock before queuing begins lock = create_lock('scheduler') if not lock: return nowtime = frappe.utils.now_datetime() last = frappe.db.get_value('System Settings', 'System Settings', 'scheduler_last_event') # set scheduler last event frappe.db.begin() frappe.db.set_value('System Settings', 'System Settings', 'scheduler_last_event', nowtime.strftime(DATETIME_FORMAT), update_modified=False) frappe.db.commit() out = [] if last: last = datetime.strptime(last, DATETIME_FORMAT) out = enqueue_applicable_events(site, nowtime, last) delete_lock('scheduler') return '\n'.join(out)
def enqueue_events(site): if is_scheduler_disabled(): return # lock before queuing begins try: lock = create_lock('scheduler') if not lock: return except LockTimeoutError: return nowtime = frappe.utils.now_datetime() last = frappe.db.get_global('scheduler_last_event') # set scheduler last event frappe.db.begin() frappe.db.set_global('scheduler_last_event', nowtime.strftime(DATETIME_FORMAT)) frappe.db.commit() out = [] if last: last = datetime.strptime(last, DATETIME_FORMAT) out = enqueue_applicable_events(site, nowtime, last) delete_lock('scheduler') return '\n'.join(out)
def lock(self, timeout=None): '''Creates a lock file for the given document. If timeout is set, it will retry every 1 second for acquiring the lock again :param timeout: Timeout in seconds, default 0''' signature = self.get_signature() if file_lock.lock_exists(signature): lock_exists = True if timeout: for i in range(timeout): time.sleep(1) if not file_lock.lock_exists(signature): lock_exists = False break if lock_exists: raise frappe.DocumentLockedError file_lock.create_lock(signature)
def run_scheduler(): from frappe.utils.file_lock import create_lock, delete_lock import frappe.utils.scheduler if create_lock('scheduler'): frappe.connect() print frappe.utils.scheduler.execute() delete_lock('scheduler') frappe.destroy()
def scheduler_task(site, event, handler, now=False): from frappe.utils.scheduler import log traceback = "" task_logger.info('running {handler} for {site} for event: {event}'.format( handler=handler, site=site, event=event)) try: if site not in ['tailorpad.com', 'testfirst']: exec_cmd("../env/bin/frappe --use %s" % (site), cwd="/home/erpnext/admin_site/frappe-bench/sites") frappe.init(site=site) if not create_lock(handler): return if not now: frappe.connect(site=site) frappe.get_attr(handler)() except Exception: frappe.db.rollback() traceback = log( handler, "Method: {event}, Handler: {handler}".format(event=event, handler=handler)) task_logger.warn(traceback) raise else: frappe.db.commit() finally: delete_lock(handler) if not now: frappe.destroy() task_logger.info('ran {handler} for {site} for event: {event}'.format( handler=handler, site=site, event=event))