def test_job_handler_with_version(self, mock_next_time): db_func = self.create_function() function_id = db_func.id new_version = db_api.increase_function_version(function_id, 0) self.assertEqual(0, new_version.count) now = datetime.utcnow() db_job = self.create_job( function_id, function_version=1, status=status.RUNNING, next_execution_time=now, count=2 ) job_id = db_job.id e_client = mock.Mock() # It doesn't matter what's the returned value, but need to be in # datetime type. mock_next_time.return_value = now + timedelta(seconds=1) periodics.handle_job(e_client) context.set_ctx(self.ctx) db_job = db_api.get_job(job_id) self.assertEqual(1, db_job.count) db_func = db_api.get_function(function_id) self.assertEqual(0, db_func.count) db_version = db_api.get_function_version(function_id, 1) self.assertEqual(1, db_version.count) db_execs = db_api.get_executions(function_id=function_id, function_version=1) self.assertEqual(1, len(db_execs)) periodics.handle_job(e_client) context.set_ctx(self.ctx) db_job = db_api.get_job(job_id) self.assertEqual(0, db_job.count) self.assertEqual(status.DONE, db_job.status) db_func = db_api.get_function(function_id) self.assertEqual(0, db_func.count) db_version = db_api.get_function_version(function_id, 1) self.assertEqual(2, db_version.count) db_execs = db_api.get_executions(function_id=function_id, function_version=1) self.assertEqual(2, len(db_execs))
def put(self, id, job): """Update job definition. 1. Can not update a finished job. 2. Can not change job type. 3. Allow to pause a one-shot job and resume before its first execution time. """ values = {} for key in UPDATE_ALLOWED: if job.to_dict().get(key) is not None: values.update({key: job.to_dict()[key]}) LOG.info('Update resource, params: %s', values, resource={ 'type': self.type, 'id': id }) new_status = values.get('status') pattern = values.get('pattern') next_execution_time = values.get('next_execution_time') job_db = db_api.get_job(id) if job_db.status in [status.DONE, status.CANCELLED]: raise exc.InputException('Can not update a finished job.') if pattern: if not job_db.pattern: raise exc.InputException('Can not change job type.') jobs.validate_pattern(pattern) elif pattern == '' and job_db.pattern: raise exc.InputException('Can not change job type.') valid_states = [status.RUNNING, status.CANCELLED, status.PAUSED] if new_status and new_status not in valid_states: raise exc.InputException('Invalid status.') if next_execution_time: values['next_execution_time'] = jobs.validate_next_time( next_execution_time) elif (job_db.status == status.PAUSED and new_status == status.RUNNING): p = job_db.pattern or pattern if not p: # Check if the next execution time for one-shot job is still # valid. jobs.validate_next_time(job_db.next_execution_time) else: # Update next_execution_time for recurring job. values['next_execution_time'] = croniter.croniter( p, timeutils.utcnow()).get_next(datetime.datetime) updated_job = db_api.update_job(id, values) return resources.Job.from_dict(updated_job.to_dict())
def delete_job(id, trust_id=None): if not trust_id: trust_id = db_api.get_job(id).trust_id modified_count = db_api.delete_job(id) if modified_count: # Delete trust only together with deleting trigger. keystone_utils.delete_trust(trust_id) return 0 != modified_count
def test_job_handler(self, mock_get_next): db_func = self.create_function() function_id = db_func.id self.assertEqual(0, db_func.count) now = datetime.utcnow() db_job = self.create_job( function_id, status=status.RUNNING, next_execution_time=now, count=2 ) job_id = db_job.id e_client = mock.Mock() mock_get_next.return_value = now + timedelta(seconds=1) periodics.handle_job(e_client) context.set_ctx(self.ctx) db_job = db_api.get_job(job_id) self.assertEqual(1, db_job.count) db_func = db_api.get_function(function_id) self.assertEqual(1, db_func.count) db_execs = db_api.get_executions(function_id=function_id) self.assertEqual(1, len(db_execs)) periodics.handle_job(e_client) context.set_ctx(self.ctx) db_job = db_api.get_job(job_id) self.assertEqual(0, db_job.count) self.assertEqual(status.DONE, db_job.status) db_func = db_api.get_function(function_id) self.assertEqual(2, db_func.count) db_execs = db_api.get_executions(function_id=function_id) self.assertEqual(2, len(db_execs))
def get(self, id): LOG.info("Fetch resource.", resource={'type': self.type, 'id': id}) job_db = db_api.get_job(id) return resources.Job.from_dict(job_db.to_dict())
def get(self, id): LOG.info("Get resource.", resource={'type': self.type, 'id': id}) job_db = db_api.get_job(id) return resources.Job.from_db_obj(job_db)