Ejemplo n.º 1
0
 def perform_create(self, serializer):
     ttl = self.request.data.get(RedisTTL.TTL_KEY)
     if ttl:
         try:
             ttl = RedisTTL.validate_ttl(ttl)
         except ValueError:
             raise ValidationError('ttl must be an integer.')
     instance = serializer.save(user=self.request.user,
                                project=self.project)
     if ttl:
         RedisTTL.set_for_job(job_id=instance.id, value=ttl)
Ejemplo n.º 2
0
    def test_redis_ttl_raises_for_wrong_values(self):
        with self.assertRaises(ValueError):
            RedisTTL(experiment=1, job=1)

        with self.assertRaises(ValueError):
            RedisTTL(job=1, build=1)

        with self.assertRaises(ValueError):
            RedisTTL(experiment=1, job=1, build=1)

        with self.assertRaises(ValueError):
            RedisTTL()
Ejemplo n.º 3
0
    def run(cls) -> Dict:
        results = {}
        result = cls.redis_health(RedisEphemeralTokens.connection())
        if not result.is_healthy:
            results['REDIS_EPH_TOKENS'] = result

        result = cls.redis_health(RedisSessions.connection())
        if not result.is_healthy:
            results['REDIS_SESSIONS'] = result

        result = cls.redis_health(RedisTTL.connection())
        if not result.is_healthy:
            results['REDIS_TTL'] = result

        result = cls.redis_health(RedisToStream.connection())
        if not result.is_healthy:
            results['REDIS_TO_STREAM'] = result

        result = cls.redis_health(RedisJobContainers.connection())
        if not result.is_healthy:
            results['REDIS_CONTAINERS'] = result

        if not results:
            results = {'REDIS': Result()}

        return results
Ejemplo n.º 4
0
    def perform_create(self, serializer):
        ttl = self.request.data.get(RedisTTL.TTL_KEY)
        if ttl:
            try:
                ttl = RedisTTL.validate_ttl(ttl)
            except ValueError:
                raise ValidationError('ttl must be an integer.')

        instance = serializer.save(user=self.request.user,
                                   project=self.project)
        if not instance.is_managed:
            return
        if ttl:
            RedisTTL.set_for_build(build_id=instance.id, value=ttl)
        # Trigger build scheduling
        workers.send(SchedulerCeleryTasks.BUILD_JOBS_START,
                     kwargs={'build_job_id': instance.id})
Ejemplo n.º 5
0
    def test_create_ttl(self):
        data = {'content': build_spec_parsed_content.raw_data, 'ttl': 10}
        resp = self.auth_client.post(self.url, data)
        assert resp.status_code == status.HTTP_201_CREATED
        job = BuildJob.objects.last()
        assert RedisTTL.get_for_build(job.id) == 10

        data = {'content': build_spec_parsed_content.raw_data, 'ttl': 'foo'}
        resp = self.auth_client.post(self.url, data)
        assert resp.status_code == status.HTTP_400_BAD_REQUEST
Ejemplo n.º 6
0
    def post(self, request, *args, **kwargs):
        ttl = self.request.data.get(RedisTTL.TTL_KEY)
        if ttl:
            try:
                ttl = RedisTTL.validate_ttl(ttl)
            except ValueError:
                raise ValidationError('ttl must be an integer.')

        obj = self.get_object()
        auditor.record(event_type=self.event_type,
                       instance=obj,
                       actor_id=self.request.user.id,
                       actor_name=self.request.user.username)

        description = None
        content = None
        params = None
        update_code_reference = False
        if 'content' in request.data:
            spec = compiler.compile(
                kind=kinds.EXPERIMENT,
                values=[obj.specification.raw_data, request.data['content']])
            content = spec.raw_data
            params = spec.params
        if 'update_code' in request.data:
            update_code_reference = to_bool(request.data['update_code'],
                                            handle_none=True,
                                            exception=ValidationError)
        if 'description' in request.data:
            description = request.data['description']
        new_obj = self.clone(obj=obj,
                             content=content,
                             params=params,
                             update_code_reference=update_code_reference,
                             description=description)
        if ttl:
            RedisTTL.set_for_experiment(experiment_id=new_obj.id, value=ttl)
        serializer = self.get_serializer(new_obj)
        return Response(status=status.HTTP_201_CREATED, data=serializer.data)
Ejemplo n.º 7
0
    def post(self, request, *args, **kwargs):
        ttl = self.request.data.get(RedisTTL.TTL_KEY)
        if ttl:
            try:
                ttl = RedisTTL.validate_ttl(ttl)
            except ValueError:
                raise ValidationError('ttl must be an integer.')

        auditor.record(event_type=self.event_type,
                       instance=self.job,
                       actor_id=self.request.user.id,
                       actor_name=self.request.user.username)

        description = None
        content = None
        update_code_reference = False
        if 'content' in request.data:
            spec = compiler.compile(kind=kinds.JOB,
                                    values=[
                                        self.job.specification.raw_data,
                                        request.data['content']
                                    ])
            content = spec.raw_data
        if 'update_code' in request.data:
            try:
                update_code_reference = to_bool(request.data['update_code'])
            except TypeError:
                raise ValidationError('update_code should be a boolean')
        if 'description' in request.data:
            description = request.data['description']
        new_obj = self.clone(obj=self.job,
                             content=content,
                             update_code_reference=update_code_reference,
                             description=description)
        if ttl:
            RedisTTL.set_for_job(job_id=new_obj.id, value=ttl)
        serializer = self.get_serializer(new_obj)
        return Response(status=status.HTTP_201_CREATED, data=serializer.data)
Ejemplo n.º 8
0
    def perform_create(self, serializer):
        ttl = self.request.data.get(RedisTTL.TTL_KEY)
        if ttl:
            try:
                ttl = RedisTTL.validate_ttl(ttl)
            except ValueError:
                raise ValidationError('ttl must be an integer.')
        group = self.request.data.get('experiment_group')
        if group:
            try:
                group = ExperimentGroup.objects.get(id=group,
                                                    project=self.project)
            except ExperimentGroup.DoesNotExist:
                raise ValidationError('Received an invalid group.')
            if group.is_selection:
                self.request.data.pop('experiment_group')

        instance = serializer.save(user=self.request.user,
                                   project=self.project)
        if group and group.is_selection:  # Add the experiment to the group selection
            group.selection_experiments.add(instance)
        if ttl:
            RedisTTL.set_for_experiment(experiment_id=instance.id, value=ttl)
Ejemplo n.º 9
0
    def _handle_job_post_run(cls, event: 'Event') -> None:
        instance = event.instance
        if not instance or not instance.has_specification:
            return

        workers.send(SchedulerCeleryTasks.JOBS_STOP,
                     kwargs={
                         'project_name': instance.project.unique_name,
                         'project_uuid': instance.project.uuid.hex,
                         'job_name': instance.unique_name,
                         'job_uuid': instance.uuid.hex,
                         'update_status': False,
                         'collect_logs': True,
                         'is_managed': instance.is_managed,
                     },
                     countdown=RedisTTL.get_for_job(job_id=instance.id))
Ejemplo n.º 10
0
    def _handle_experiment_post_run(cls, event: 'Event') -> None:
        instance = event.instance
        if not instance or not instance.has_specification or not instance.jobs.count(
        ) > 0:
            return

        # Schedule stop for this experiment because other jobs may be still running
        group = instance.experiment_group
        workers.send(
            SchedulerCeleryTasks.EXPERIMENTS_STOP,
            kwargs={
                'project_name': instance.project.unique_name,
                'project_uuid': instance.project.uuid.hex,
                'experiment_name': instance.unique_name,
                'experiment_uuid': instance.uuid.hex,
                'experiment_group_name': group.unique_name if group else None,
                'experiment_group_uuid': group.uuid.hex if group else None,
                'specification': instance.content,
                'update_status': False,
                'collect_logs': True,
                'is_managed': instance.is_managed,
            },
            countdown=RedisTTL.get_for_experiment(experiment_id=instance.id))
Ejemplo n.º 11
0
    def test_validate_ttl(self):
        with self.assertRaises(ValueError):
            RedisTTL.validate_ttl(None)

        with self.assertRaises(ValueError):
            RedisTTL.validate_ttl('sdf')
Ejemplo n.º 12
0
 def test_set_for_build(self):
     RedisTTL.set_for_build(build_id=1, value=10)
     assert RedisTTL.get_for_build(build_id=1) == 10
     assert RedisTTL.get_for_build(
         build_id=2) == conf.get(SCHEDULER_GLOBAL_COUNTDOWN)
     assert RedisTTL(build=10).get_value() is None
Ejemplo n.º 13
0
    def test_redis_ttl_build(self):
        ttl = RedisTTL(build=1)
        assert ttl.redis_key == RedisTTL.KEY_BUILD.format(1)

        ttl.set_value(10)
        assert ttl.get_value() == 10

        ttl.set_value(13)
        assert ttl.get_value() == 13

        ttl.clear()

        assert ttl.get_value() is None
Ejemplo n.º 14
0
    def test_redis_ttl_job(self):
        ttl = RedisTTL(job=1)
        assert ttl.redis_key == RedisTTL.KEY_JOB.format(1)

        ttl.set_value(10)
        assert ttl.get_value() == 10

        ttl.set_value(13)
        assert ttl.get_value() == 13

        ttl.clear()

        assert ttl.get_value() is None
Ejemplo n.º 15
0
 def test_set_for_experiment(self):
     RedisTTL.set_for_experiment(experiment_id=1, value=10)
     assert RedisTTL.get_for_experiment(experiment_id=1) == 10
     assert RedisTTL.get_for_experiment(
         experiment_id=2) == conf.get(SCHEDULER_GLOBAL_COUNTDOWN)
     assert RedisTTL(experiment=10).get_value() is None
Ejemplo n.º 16
0
    def test_redis_ttl_experiment(self):
        ttl = RedisTTL(experiment=1)
        assert ttl.redis_key == RedisTTL.KEY_EXPERIMENT.format(1)

        ttl.set_value(10)
        assert ttl.get_value() == 10

        ttl.set_value(13)
        assert ttl.get_value() == 13

        ttl.clear()

        assert ttl.get_value() is None