Exemplo n.º 1
0
    def get_schedueled_jobs_rq(self):
        jobs = self.queue.get_job_ids()
        self.failed = self.queue.failed_job_registry.get_job_ids()
        self.jqueued = []
        self.started = self.queue.started_job_registry.get_job_ids()
        self.finished = self.queue.finished_job_registry.get_job_ids()
        for rjob in jobs:
            try:
                job = Job.fetch(rjob, connection=self.r_connection)
            except:
                log.error("No job with id {}".format(rjob))
                response = {'error': 'no such job'}
                return response
            if job.is_finished:
                self.finished.append(rjob)
            elif job.is_failed:
                self.failed.append(rjob)
            elif job.is_started:
                self.started.append(rjob)
            elif job.is_queued:
                self.jqueued.append(rjob)

        response = {
            'started': self.started,
            'finished': self.finished,
            'failed': self.failed,
            'queued':self.jqueued
        }

        return response
Exemplo n.º 2
0
    def delete(self):
        if not request.json:
            list_workers = get_list_workers()
            for worker in range(len(list_workers)):
                service_name = "chaosrqworker@{}".format(worker)
                subprocess.call(['sudo', 'service', service_name, 'stop'])
                time.sleep(1)
            log.info("Stopped all chaos workers")
            response = jsonify({'status': 'stopped all chaos workers'})
            response.status_code = 200
        elif 'id' not in request.json.keys():
            response = jsonify({'status': 'missing worker id in request'})
            response.status_code = 400
            log.error("No worker id for stopping specified")

        else:
            service_name = "chaosrqworker@{}".format(request.json['id'])
            subprocess.call(['sudo', 'service', service_name, 'stop'])
            time.sleep(1)
            log.info("Worker with id {} stopped".format(request.json['id']))
            response = jsonify({
                'status':
                'stopped worker with id {}'.format(request.json['id'])
            })
            response.status_code = 200
        return response
Exemplo n.º 3
0
 def remove_job(self, job_id):
     try:
         job = Job.fetch(job_id, connection=self.r_connection)
     except Exception as inst:
         log.error("No job with id {}".format(job_id))
         response = {'error': 'no such job'}
         return response
     job.delete()
     log.info("Job with id {} deleted.".format(job_id))
     response = {"status": "deleted",
                 "job": job_id}
     return response
Exemplo n.º 4
0
 def remove_all_jobs(self):
     jobs = self.queue.get_job_ids()
     for rjob in jobs:
         try:
             job = Job.fetch(rjob, connection=self.r_connection)
             job.delete()
             log.info("Deleted job with id {}".format(rjob))
         except:
             log.error("No job with id {}".format(rjob))
             response = {'error': 'no such job'}
             return response
     response = {'deleted_job': jobs}
     return response
Exemplo n.º 5
0
 def return_jobs_status(self, job_id):
     try:
         job = Job.fetch(job_id, connection=self.r_connection)
     except Exception as inst:
         log.error("No job with id {}".format(job_id))
         response = {'error': 'no such job'}
         return response
     job.refresh()
     status = job.get_status()
     finished = job.is_finished
     meta = job.meta
     response = {'status': status,
                         'finished': finished,
                         'meta': meta}
     return response
Exemplo n.º 6
0
    def session_gen(self):
        sgen = self.session
        log.info("Detailed session generation started")
        options = sgen.get('options', None)
        anomalies = sgen.get('anomalies', None)
        if options is not None:
            self.loop = options.get('loop', False)
            self.randomise = options.get('randomise', False)
            self.distribution = options.get('distribution', False)
            self.sample_size = options.get('sample_size', len(anomalies))
            self.stagger_time = options.get('stagger_time', 0)
        # print(anomalies)
        if self.randomise:
            log.info("Session is randomised")
            random.shuffle(anomalies)
            # sgen = anomalies
        if self.distribution == 'uniform':
            log.info("Session uniform distribution")
            sgen = list(np.random.choice(anomalies, self.sample_size))
            # print(len(np.random.choice(anomalies, self.sample_size)))
        elif self.distribution == 'prob':
            log.info("Session probabilistic distribution")
            prob_list = []
            for ano in anomalies:
                prob_list.append(ano['prob'])
            sum_prob = sum(prob_list)
            if round(sum_prob, 2) < 1.0 or round(sum_prob, 2) > 1.0:
                log.error("Probabilities sum to {}, have to be 1.0".format(sum_prob))
                import sys
                sys.exit()
            sgen = list(np.random.choice(anomalies, self.sample_size, p=prob_list))
        else:
            sgen = anomalies

        if self.stagger_time:
            log.info("Stagger set to {}".format(self.stagger_time))
            stagger = {'type': 'dummy', 'options': {'stime': self.stagger_time, 'silent': 1}}
            sgen = self._intersperse(sgen=sgen, item=stagger)

        # print(anomalies)
        # print(len(anomalies))
        # print(options)
        self.schedueled_session = sgen
        log.info("Finished final session generation")
        return sgen
Exemplo n.º 7
0
 def post(self):
     if not chaosgen.schedueled_session:
         log.warning("No user defined session data defined, using default!")
     try:
         chaosgen.job_gen()
     except Exception as inst:
         log.error("Failed to generate jobs with {} and {}".format(
             type(inst), inst.args))
         response = jsonify({
             'error':
             "Failed to generate jobs with {} and {}".format(
                 type(inst), inst.args)
         })
         response.status_code = 400
         return response
     response = jsonify(chaosgen.get_detailed_session())
     response.status_code = 201
     return response
Exemplo n.º 8
0
 def get(self, job_id):
     try:
         job = Job.fetch(job_id, connection=r_connection)
     except Exception as inst:
         log.error("No job with id {}".format(job_id))
         response = jsonify({'error': 'no such job'})
         response.status_code = 404
         return response
     job.refresh()
     status = job.get_status()
     finished = job.is_finished
     meta = job.meta
     response = jsonify({
         'status': status,
         'finished': finished,
         'meta': meta
     })
     response.status_code = 200
     return response