Esempio n. 1
0
    def test_update_recurring_job_failed(self):
        job_id = self.create_job(
            self.function_id,
            first_execution_time=datetime.utcnow() + timedelta(hours=1),
            next_execution_time=datetime.utcnow() + timedelta(hours=1),
            pattern='0 */1 * * *',
            status=status.RUNNING,
            count=10).id
        url = '/v1/jobs/%s' % job_id

        # Try to change job type
        resp = self.app.put_json(url, {'pattern': ''}, expect_errors=True)

        self.assertEqual(400, resp.status_int)
        self.assertIn('Can not change job type.', resp.json['faultstring'])

        # Pause the job and try to resume with an invalid next_execution_time
        auth_context.set_ctx(self.ctx)
        self.addCleanup(auth_context.set_ctx, None)
        db_api.update_job(job_id, {'status': status.PAUSED})
        resp = self.app.put_json(
            url, {
                'status': status.RUNNING,
                'next_execution_time':
                str(datetime.utcnow() - timedelta(hours=1))
            },
            expect_errors=True)

        self.assertEqual(400, resp.status_int)
        self.assertIn('Execution time must be at least 1 minute in the future',
                      resp.json['faultstring'])
Esempio n. 2
0
    def test_update_one_shot_job_failed(self):
        job_id = self.create_job(self.function_id,
                                 prefix='TestJobController',
                                 first_execution_time=datetime.utcnow(),
                                 next_execution_time=datetime.utcnow() +
                                 timedelta(hours=1),
                                 status=status.RUNNING,
                                 count=1).id
        url = '/v1/jobs/%s' % job_id

        # Try to change job type
        resp = self.app.put_json(url, {'pattern': '*/1 * * * *'},
                                 expect_errors=True)

        self.assertEqual(400, resp.status_int)
        self.assertIn('Can not change job type.', resp.json['faultstring'])

        # Try to resume job but the execution time is invalid
        auth_context.set_ctx(self.ctx)
        self.addCleanup(auth_context.set_ctx, None)
        db_api.update_job(
            job_id, {
                'next_execution_time': datetime.utcnow() - timedelta(hours=1),
                'status': status.PAUSED
            })
        resp = self.app.put_json(url, {'status': status.RUNNING},
                                 expect_errors=True)

        self.assertEqual(400, resp.status_int)
        self.assertIn('Execution time must be at least 1 minute in the future',
                      resp.json['faultstring'])
Esempio n. 3
0
    def test_update_recurring_job(self):
        job_id = self.create_job(
            self.function_id,
            first_execution_time=datetime.utcnow() + timedelta(hours=1),
            next_execution_time=datetime.utcnow() + timedelta(hours=1),
            pattern='0 */1 * * *',
            status=status.RUNNING,
            count=10).id

        next_hour_and_half = datetime.utcnow() + timedelta(hours=1.5)
        next_two_hours = datetime.utcnow() + timedelta(hours=2)

        req_body = {
            'next_execution_time':
            str(next_hour_and_half.strftime('%Y-%m-%dT%H:%M:%SZ')),
            'pattern':
            '1 */1 * * *'
        }
        resp = self.app.put_json('/v1/jobs/%s' % job_id, req_body)

        self.assertEqual(200, resp.status_int)
        self._assertDictContainsSubset(resp.json, req_body)

        # Pause the job and resume with a valid next_execution_time
        req_body = {'status': status.PAUSED}
        resp = self.app.put_json('/v1/jobs/%s' % job_id, req_body)

        self.assertEqual(200, resp.status_int)
        self._assertDictContainsSubset(resp.json, req_body)

        req_body = {
            'status':
            status.RUNNING,
            'next_execution_time':
            str(next_two_hours.strftime('%Y-%m-%dT%H:%M:%SZ')),
        }
        resp = self.app.put_json('/v1/jobs/%s' % job_id, req_body)

        self.assertEqual(200, resp.status_int)
        self._assertDictContainsSubset(resp.json, req_body)

        # Pause the job and resume without specifying next_execution_time
        auth_context.set_ctx(self.ctx)
        self.addCleanup(auth_context.set_ctx, None)
        db_api.update_job(
            job_id, {
                'next_execution_time': datetime.utcnow() - timedelta(hours=1),
                'status': status.PAUSED
            })

        req_body = {'status': status.RUNNING}
        resp = self.app.put_json('/v1/jobs/%s' % job_id, req_body)

        self.assertEqual(200, resp.status_int)
        self._assertDictContainsSubset(resp.json, req_body)
Esempio n. 4
0
    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())