Ejemplo n.º 1
0
Archivo: views.py Proyecto: sltk/crmint
        worker_class = getattr(workers, args['worker_class'])
        worker_params = json.loads(args['worker_params'])
        worker = worker_class(worker_params, job.pipeline_id, job.id)
        if retries >= worker_class.MAX_ATTEMPTS:
            worker.log_error('Execution canceled after %i failed attempts',
                             retries)
            job.task_failed(task_name)
        elif job.status == 'stopping':
            worker.log_warn(
                'Execution canceled as parent job is going to stop')
            job.task_failed(task_name)
        else:
            try:
                workers_to_enqueue = worker.execute()
            except workers.WorkerException as e:
                worker.log_error('Execution failed: %s: %s',
                                 e.__class__.__name__, e)
                job.task_failed(task_name)
            except Exception as e:
                worker.log_error('Unexpected error: %s: %s',
                                 e.__class__.__name__, e)
                raise e
            else:
                for worker_class_name, worker_params, delay in workers_to_enqueue:
                    job.enqueue(worker_class_name, worker_params, delay)
                job.task_succeeded(task_name)
        return 'OK', 200


api.add_resource(Task, '/task')
Ejemplo n.º 2
0

class Cron(Resource):
    """Resource to handle GET requests from cron service."""
    def _its_time(self, cron_format):
        """Returns True if current time matches cron time spec."""
        now = int(time.time())
        itr = croniter(cron_format, now - 60)
        nxt = itr.get_next()
        return now / 60 * 60 == nxt

    def get(self):
        """Finds and enqueues pipelines scheduled to be executed now."""
        urlfetch.set_default_fetch_deadline(300)
        for pipeline in Pipeline.where(run_on_schedule=True).all():
            logging.info('Checking schedules for pipeline %s', pipeline.name)
            for schedule in pipeline.schedules:
                logging.info('Checking schedule with cron string %s',
                             schedule.cron)
                if self._its_time(schedule.cron):
                    logging.info('Trying to start pipeline %s', pipeline.name)
                    pipeline.start()
                    tracker = insight.GAProvider()
                    tracker.track_event(category='pipelines',
                                        action='scheduled_run')
                    break
        return 'OK', 200


api.add_resource(Cron, '/cron')