def _delete_mirror_file_at_time_1(name: str, deletion_time: str) -> None: if current_task.maybe_delay_task( DatetimeWithTimezone.fromisoformat(deletion_time)): return found = p.app.mirror_file_storage.get(name) found.if_just(lambda f: f.delete())
def as_task( self, fun: t.Callable[[], t.Optional[TaskResultState]], *, eta: t.Optional[DatetimeWithTimezone] = None, ) -> bool: """Run the given ``fun`` as the task. .. warning:: One of the first things this function do is committing the current session, however after running ``fun`` nothing is committed. :param fun: The function to run as the task, catching the exceptions it produces and storing them in this task result. :param eta: The time the task should run, if the current time is before this ETA the current celery task will be scheduled again and ``fun`` will not be called. :returns: ``True`` if the task ran, otherwise ``False``. """ if not self.state.is_not_started: # pragma: no cover logger.error('Cannot start task that has already started', task_result=self) return False elif eta and current_task.maybe_delay_task(eta): return False self.state = TaskResultState.started db.session.commit() try: result_code = fun() except APIException as exc: self.state = TaskResultState.failed self.result = JSONResponse.dump_to_object(exc) except: # pylint: disable=bare-except logger.warning('The task crashed', exc_info=True) self.state = TaskResultState.crashed self.result = JSONResponse.dump_to_object( APIException( 'The task failed for an unknown reason', f'The task {self.id} failed with a uncaught exception', APICodes.UNKOWN_ERROR, 400)) else: self.result = None self.state = handle_none(result_code, TaskResultState.finished) return True
def _maybe_open_assignment_at_1(assignment_id: int) -> None: assignment = p.models.Assignment.query.filter( p.models.Assignment.id == assignment_id, ).with_for_update( of=p.models.Assignment).one_or_none() if assignment is None or assignment.available_at is None: logger.error('Could not find assignment with available_at', assignment=assignment, assignment_id=assignment_id) return if not assignment.state.is_hidden: logger.info('State already set to not hidden') return if current_task.maybe_delay_task(assignment.available_at): return assignment.state = p.models.AssignmentStateEnum.open p.models.db.session.commit()
def _delete_file_at_time_1( filename: str, in_mirror_dir: bool, deletion_time: str ) -> None: if current_task.maybe_delay_task( DatetimeWithTimezone.fromisoformat(deletion_time) ): return if in_mirror_dir: root = p.app.config['MIRROR_UPLOAD_DIR'] else: # pragma: no cover # The case outside of the mirror_upload_dir is not yet used root = p.app.config['UPLOAD_DIR'] filename = p.files.safe_join(root, filename) if os.path.isfile(filename): # There is a race condition here (file is removed in this small space), # but we don't care as it is removed in that case try: os.unlink(filename) except FileNotFoundError: # pragma: no cover pass
def maybe_delay_current_task(wanted_time: DatetimeWithTimezone) -> bool: return current_task.maybe_delay_task(wanted_time)