def _update_item_status( s_item: ScheduledOperation, run_result: List[str], debug: bool, ): """Update the status of the scheduled item. :param s_item: Scheduled item :return: Nothing """ now = datetime.now(pytz.timezone(settings.TIME_ZONE)) if run_result is None: s_item.status = ScheduledOperation.STATUS_DONE_ERROR else: if s_item.execute_until and s_item.execute_until > now: # There is a second date/time and is not passed yet! s_item.status = ScheduledOperation.STATUS_PENDING # Update exclude values s_item.exclude_values.extend(run_result) else: s_item.status = ScheduledOperation.STATUS_DONE # Save the new status in the DB s_item.save() if debug: logger.info('Status set to %s', s_item.status)
def execute_scheduled_actions_task(debug: bool): """Execute the entries in the DB that are due. :return: Nothing. """ # Get the current date/time s_items = _get_pending_items() # If the number of tasks to execute is zero, we are done. if s_items.count() == 0: return for s_item in s_items: if debug: logger.info('Starting execution of task %s', str(s_item.id)) with cache.lock(cache_lock_format.format(s_item.id)): # Item is now locked by the cache mechanism s_item.refresh_from_db() if s_item.status != ScheduledOperation.STATUS_PENDING: continue try: # Set item to running s_item.status = ScheduledOperation.STATUS_EXECUTING s_item.save() run_result = None log_item = None action_info = None # Get action info and log item if s_item.action.action_type in _function_distributor: action_info, log_item = _function_distributor[ s_item.action.action_type](s_item) if log_item: run_result = run_task( s_item.user.id, log_item.id, action_info.get_store()) s_item.last_executed_log = log_item else: logger.error( 'Execution of action type "%s" not implemented', s_item.action.action_type) _update_item_status(s_item, run_result, debug) except Exception as exc: logger.error( 'Error while processing scheduled action: {0}'.format( str(exc)))
def _get_pending_items(): """Fetch the actions pending execution. :return: QuerySet with the pending actions. """ # Get the current date/time now = datetime.now(pytz.timezone(settings.TIME_ZONE)) # Get all the actions that are pending s_items = ScheduledOperation.objects.filter( status=ScheduledOperation.STATUS_PENDING, execute__lt=now) logger.info('%s actions pending execution', s_items.count()) return s_items
def execute_scheduled_actions(debug): """ Function that selects the entries in the DB that are due, and proceed with the execution. :return: """ # Get the current date/time now = datetime.now(pytz.timezone(ontask_settings.TIME_ZONE)) # Get all the actions that are pending s_items = ScheduledAction.objects.filter( status=ScheduledAction.STATUS_PENDING, execute__lt=now + timedelta(minutes=1)) logger.info('{0} actions pending execution'.format(s_items.count())) # If the number of tasks to execute is zero, we are done. if s_items.count() == 0: return for item in s_items: if debug: logger.info('Starting execution of task ' + str(item.id)) # Set item to running item.status = ScheduledAction.STATUS_EXECUTING item.save() result = None # # EMAIL ACTION # if item.action.action_type == Action.personalized_text: subject = item.payload.get('subject', '') cc_email = item.payload.get('cc_email', []) bcc_email = item.payload.get('bcc_email', []) send_confirmation = item.payload.get('send_confirmation', False) track_read = item.payload.get('track_read', False) # Log the event log_item = Log.objects.register( item.user, Log.SCHEDULE_EMAIL_EXECUTE, item.action.workflow, { 'action': item.action.name, 'action_id': item.action.id, 'bcc_email': bcc_email, 'cc_email': cc_email, 'email_column': item.item_column.name, 'execute': item.execute.isoformat(), 'exclude_values': item.exclude_values, 'from_email': item.user.email, 'send_confirmation': send_confirmation, 'status': 'Preparing to execute', 'subject': subject, 'track_read': track_read, }) # Store the log event in the scheduling item item.last_executed_log = log_item item.save() result = send_email_messages(item.user.id, item.action.id, subject, item.item_column.name, item.user.email, cc_email, bcc_email, send_confirmation, track_read, item.exclude_values, log_item.id) # # JSON action # elif item.action.action_type == Action.personalized_json: # Get the information from the payload token = item.payload['token'] key_column = None if item.item_column: key_column = item.item_column.name # Log the event log_item = Log.objects.register( item.user, Log.SCHEDULE_JSON_EXECUTE, item.action.workflow, { 'action': item.action.name, 'action_id': item.action.id, 'exclude_values': item.exclude_values, 'key_column': key_column, 'status': 'Preparing to execute', 'target_url': item.action.target_url }) # Send the objects result = send_json_objects(item.user.id, item.action.id, token, key_column, item.exclude_values, log_item.id) # # Canvas Email Action # elif item.action.action_type == Action.personalized_canvas_email: # Get the information from the payload token = item.payload['token'] subject = item.payload.get('subject', '') # Log the event log_item = Log.objects.register( item.user, Log.SCHEDULE_EMAIL_EXECUTE, item.action.workflow, { 'action': item.action.name, 'action_id': item.action.id, 'email_column': item.item_column.name, 'execute': item.execute.isoformat(), 'exclude_values': item.exclude_values, 'from_email': item.user.email, 'status': 'Preparing to execute', 'subject': subject, }) result = send_canvas_email_messages(item.user_id, item.action_id, subject, item.item_column_name, item.user.email, token, item.exclude_values, log_item.id) if result: item.status = ScheduledAction.STATUS_DONE else: item.status = ScheduledAction.STATUS_DONE_ERROR if debug: logger.info('Status set to {0}'.format(item.status)) # Save the new status in the DB item.save()
def execute_scheduled_actions_task(debug: bool): """Execute the entries in the DB that are due. :return: Nothing. """ # Get the current date/time now = datetime.now(pytz.timezone(settings.TIME_ZONE)) # Get all the actions that are pending s_items = ScheduledAction.objects.filter( status=ScheduledAction.STATUS_PENDING, execute__lt=now + timedelta(minutes=1)) logger.info('%s actions pending execution', s_items.count()) # If the number of tasks to execute is zero, we are done. if s_items.count() == 0: return for s_item in s_items: if debug: logger.info('Starting execution of task %s', str(s_item.id)) # Set item to running s_item.status = ScheduledAction.STATUS_EXECUTING s_item.save() run_result = None log_item = None # # EMAIL ACTION # if s_item.action.action_type == Action.personalized_text: action_info = EmailPayload(s_item.payload) action_info['action_id'] = s_item.action_id action_info['item_column'] = s_item.item_column.name action_info['exclude_values'] = s_item.exclude_values # Log the event log_item = Log.objects.register( s_item.user, Log.SCHEDULE_EMAIL_EXECUTE, s_item.action.workflow, { 'action': s_item.action.name, 'action_id': s_item.action.id, 'bcc_email': s_item.payload.get('bcc_email'), 'cc_email': s_item.payload.get('cc_email'), 'item_column': s_item.item_column.name, 'execute': s_item.execute.isoformat(), 'exclude_values': s_item.exclude_values, 'from_email': s_item.user.email, 'send_confirmation': s_item.payload.get('send_confirmation'), 'status': 'Preparing to execute', 'subject': s_item.payload.get('subject'), 'track_read': s_item.payload.get('track_read') }) run_result = run_task(s_item.user.id, log_item.id, action_info.get_store()) # # SEND LIST ACTION # elif s_item.action.action_type == Action.send_list: action_info = SendListPayload(s_item.payload) action_info['action_id'] = s_item.action_id # Log the event log_item = Log.objects.register( s_item.user, Log.SCHEDULE_SEND_LIST_EXECUTE, s_item.action.workflow, { 'action': s_item.action.name, 'action_id': s_item.action.id, 'from_email': s_item.user.email, 'email_to': s_item.payload.get('email_to'), 'subject': s_item.payload.get('subject'), 'bcc_email': s_item.payload.get('bcc_email'), 'cc_email': s_item.payload.get('cc_email'), 'execute': s_item.execute.isoformat(), 'status': 'Preparing to execute' }) run_result = run_task(s_item.user.id, log_item.id, action_info.get_store()) # # JSON action # elif s_item.action.action_type == Action.personalized_json: # Get the information about the keycolum item_column = None if s_item.item_column: item_column = s_item.item_column.name action_info = JSONPayload(s_item.payload) action_info['action_id'] = s_item.action_id action_info['item_column'] = item_column action_info['exclude_values'] = s_item.exclude_values # Log the event log_item = Log.objects.register( s_item.user, Log.SCHEDULE_JSON_EXECUTE, s_item.action.workflow, { 'action': s_item.action.name, 'action_id': s_item.action.id, 'exclude_values': s_item.exclude_values, 'item_column': item_column, 'status': 'Preparing to execute', 'target_url': s_item.action.target_url }) # Send the objects run_result = run_task(s_item.user.id, log_item.id, action_info.get_store()) # # JSON LIST action # elif s_item.action.action_type == Action.send_list_json: # Get the information about the keycolum item_column = None if s_item.item_column: item_column = s_item.item_column.name action_info = JSONPayload(s_item.payload) action_info['action_id'] = s_item.action_id # Log the event log_item = Log.objects.register( s_item.user, Log.SCHEDULE_JSON_EXECUTE, s_item.action.workflow, { 'action': s_item.action.name, 'action_id': s_item.action.id, 'status': 'Preparing to execute', 'target_url': s_item.action.target_url }) # Send the objects run_result = run_task(s_item.user.id, log_item.id, action_info.get_store()) # # Canvas Email Action # elif s_item.action.action_type == Action.personalized_canvas_email: # Get the information from the payload action_info = CanvasEmailPayload(s_item.payload) action_info['action_id'] = s_item.action_id action_info['item_column'] = s_item.item_column.name action_info['exclude_values'] = s_item.exclude_values # Log the event log_item = Log.objects.register( s_item.user, Log.SCHEDULE_EMAIL_EXECUTE, s_item.action.workflow, { 'action': s_item.action.name, 'action_id': s_item.action.id, 'item_column': s_item.item_column.name, 'execute': s_item.execute.isoformat(), 'exclude_values': s_item.exclude_values, 'from_email': s_item.user.email, 'status': 'Preparing to execute', 'subject': s_item.payload.get('subject', '') }) run_result = run_task(s_item.user.id, log_item.id, action_info.get_store()) else: logger.error('Execution of action type "%s" not implemented', s_item.action.action_type) if run_result: s_item.status = ScheduledAction.STATUS_DONE else: s_item.status = ScheduledAction.STATUS_DONE_ERROR if debug: logger.info('Status set to %s', s_item.status) if log_item: # Store the log event in the scheduling item s_item.last_executed_log = log_item # Save the new status in the DB s_item.save()