def _updateStatus(self, job, status, now, query, updates): """Helper for updating job progress information.""" self._validateStatus(status) if status != job['status']: job['status'] = status previous_states = JobStatus.validTransitions(job, status) if previous_states is None: # Get the current state job = self.load(job['_id'], force=True) msg = 'No valid state transition to \'%s\'. Current state is \'%s\'.' % ( status, job['status']) raise ValidationException(msg, field='status') query['status'] = { '$in': previous_states } updates['$set']['status'] = status ts = { 'status': status, 'time': now } job['timestamps'].append(ts) updates['$push']['timestamps'] = ts
def validate(self, job): job['status'] = int(job['status']) if not JobStatus.isValid(job['status']): raise ValidationException('Invalid job status {}.'.format( job.get['status']), field='status') return job
def _updateProgress(self, job, total, current, message, notify, user, updates): """Helper for updating job progress information.""" state = JobStatus.toNotificationStatus(job['status']) if current is not None: current = float(current) if total is not None: total = float(total) if job['progress'] is None: if notify and job['userId']: notification = self._createProgressNotification( job, total, current, state, message) notificationId = notification['_id'] else: notificationId = None job['progress'] = { 'message': message, 'total': total, 'current': current, 'notificationId': notificationId } updates['$set']['progress'] = job['progress'] else: if total is not None: job['progress']['total'] = total updates['$set']['progress.total'] = total if current is not None: job['progress']['current'] = current updates['$set']['progress.current'] = current if message is not None: job['progress']['message'] = message updates['$set']['progress.message'] = message if notify and user: if job['progress']['notificationId'] is None: notification = self._createProgressNotification( job, total, current, state, message, user) nid = notification['_id'] job['progress']['notificationId'] = nid updates['$set']['progress.notificationId'] = nid else: notification = Notification().load( job['progress']['notificationId']) Notification().updateProgress( notification, state=state, message=job['progress']['message'], current=job['progress']['current'], total=job['progress']['total'])
def _updateProgress(self, job, total, current, message, notify, user, updates): """Helper for updating job progress information.""" state = JobStatus.toNotificationStatus(job['status']) if current is not None: current = float(current) if total is not None: total = float(total) if job['progress'] is None: if notify and job['userId']: notification = self._createProgressNotification( job, total, current, state, message) notificationId = notification['_id'] else: notificationId = None job['progress'] = { 'message': message, 'total': total, 'current': current, 'notificationId': notificationId } updates['$set']['progress'] = job['progress'] else: if total is not None: job['progress']['total'] = total updates['$set']['progress.total'] = total if current is not None: job['progress']['current'] = current updates['$set']['progress.current'] = current if message is not None: job['progress']['message'] = message updates['$set']['progress.message'] = message if notify and user: if job['progress']['notificationId'] is None: notification = self._createProgressNotification( job, total, current, state, message, user) nid = notification['_id'] job['progress']['notificationId'] = nid updates['$set']['progress.notificationId'] = nid else: notification = self.model('notification').load( job['progress']['notificationId']) self.model('notification').updateProgress( notification, state=state, message=job['progress']['message'], current=job['progress']['current'], total=job['progress']['total'])
def updateNotification(event): """ Update the Whole Tale task notification for a job, if present. """ job = event.info['job'] if job['progress'] and 'wt_notification_id' in job: state = JobStatus.toNotificationStatus(job['status']) notification = Notification().load(job['wt_notification_id']) state_changed = notification['data']['state'] != state message_changed = notification['data']['message'] != job['progress'][ 'message'] # Ignore duplicate events based on state and message content if not state_changed and not message_changed: return # For multi-job tasks, ignore success for intermediate events is_last = notification['data']['total'] == ( notification['data']['current']) if state == ProgressState.SUCCESS and not is_last: return # Add job IDs to the resource if 'jobs' not in notification['data']['resource']: notification['data']['resource']['jobs'] = [] if job['_id'] not in notification['data']['resource']['jobs']: notification['data']['resource']['jobs'].append(job['_id']) # If the state hasn't changed, increment. Otherwise keep previous current value. # Note, if expires parameter is not provided, updateProgress resets to 1 hour if not state_changed: Notification().updateProgress(notification, state=state, expires=notification['expires'], message=job['progress']['message'], increment=1, total=notification['data']['total']) else: Notification().updateProgress( notification, state=state, expires=notification['expires'], message=job['progress']['message'], current=notification['data']['current'], total=notification['data']['total'])
def _updateJobProgress(self, job, total, current, message, notify): """Helper for updating job progress information.""" state = JobStatus.toNotificationStatus(job['status']) if current is not None: current = float(current) if total is not None: total = float(total) if job['progress'] is None: if notify and job['userId']: notification = self._createProgressNotification( job, total, current, state, message) notificationId = notification['_id'] else: notificationId = None job['progress'] = { 'message': message, 'total': total, 'current': current, 'notificationId': notificationId } else: if total is not None: job['progress']['total'] = total if current is not None: job['progress']['current'] = current if message is not None: job['progress']['message'] = message if notify and job['userId']: if job['progress']['notificationId'] is None: notification = self._createProgressNotification( job, total, current, state, message) job['progress']['notificationId'] = notification['_id'] self.save(job) else: notification = self.model('notification').load( job['progress']['notificationId']) self.model('notification').updateProgress( notification, state=state, message=job['progress']['message'], current=job['progress']['current'], total=job['progress']['total'])
def validate(self, job): if not JobStatus.isValid(job['status']): raise ValidationException('Invalid job status %s.' % job['status'], field='status') return job
def _validateStatus(self, status): if not JobStatus.isValid(status): raise ValidationException( 'Invalid job status %s.' % status, field='status')
def validate(self, job): if not JobStatus.isValid(job['status']): raise ValidationException( 'Invalid job status %s.' % job['status'], field='status') return job