Ejemplo n.º 1
0
    def test_serialize_deserialize(self):
        user = User('user-X', {
            audit_utils.AUTH_USERNAME: '******',
            audit_utils.HOSTNAME: 'localhost'
        })
        schedule_config = ScheduleConfig(
            True, start_datetime=datetime.now(tz=timezone.utc))
        schedule_config.repeat_unit = 'weeks'
        schedule_config.repeat_period = 3
        schedule_config.weekdays = ['monday', 'wednesday']
        parameter_values = {'p1': 9, 'p2': ['A', 'C']}

        job = SchedulingJob(123, user, schedule_config, 'my_script',
                            parameter_values)

        serialized = json.dumps(job.as_serializable_dict())
        restored_job = from_dict(json.loads(serialized))

        self.assertEqual(job.id, restored_job.id)
        self.assertEqual(job.script_name, restored_job.script_name)
        self.assertEqual(job.parameter_values, restored_job.parameter_values)

        self.assertEqual(job.user.user_id, restored_job.user.user_id)
        self.assertEqual(job.user.audit_names, restored_job.user.audit_names)

        self.assertEqual(job.schedule.repeatable,
                         restored_job.schedule.repeatable)
        self.assertEqual(job.schedule.start_datetime,
                         restored_job.schedule.start_datetime)
        self.assertEqual(job.schedule.repeat_period,
                         restored_job.schedule.repeat_period)
        self.assertEqual(job.schedule.repeat_unit,
                         restored_job.schedule.repeat_unit)
        self.assertEqual(job.schedule.weekdays, restored_job.schedule.weekdays)
Ejemplo n.º 2
0
    def create_job(self, script_name, parameter_values,
                   incoming_schedule_config, user: User):
        if user is None:
            raise InvalidUserException('User id is missing')

        config_model = self._config_service.load_config_model(
            script_name, user, parameter_values)
        self.validate_script_config(config_model)

        schedule_config = read_schedule_config(incoming_schedule_config)

        if not schedule_config.repeatable and date_utils.is_past(
                schedule_config.start_datetime):
            raise InvalidScheduleException(
                'Start date should be in the future')

        id = self._id_generator.next_id()

        job = SchedulingJob(id, user, schedule_config, script_name,
                            parameter_values)

        self.save_job(job)

        self.schedule_job(job)

        return id
def create_job(id=None,
               user_id='UserX',
               script_name='my_script_A',
               audit_names=None,
               repeatable=True,
               start_datetime=mocked_now + timedelta(seconds=5),
               repeat_unit=None,
               repeat_period=None,
               weekdays=None,
               parameter_values=None):
    if audit_names is None:
        audit_names = {audit_utils.HOSTNAME: 'my-host'}

    if repeatable and repeat_unit is None:
        repeat_unit = 'weeks'
    if repeatable and repeat_period is None:
        repeat_period = 3

    if weekdays is None and repeatable and repeat_unit == 'weeks':
        weekdays = ['monday', 'wednesday']

    if parameter_values is None:
        parameter_values = {'p1': 987, 'param_2': ['hello', 'world']}

    schedule_config = ScheduleConfig(repeatable, start_datetime)
    schedule_config.repeat_unit = repeat_unit
    schedule_config.repeat_period = repeat_period
    schedule_config.weekdays = weekdays

    return SchedulingJob(id, User(user_id, audit_names), schedule_config, script_name, parameter_values)
Ejemplo n.º 4
0
    def save_job(self, job: SchedulingJob):
        user = job.user
        script_name = job.script_name

        filename = file_utils.to_filename(
            '%s_%s_%s.json' % (script_name, user.get_audit_name(), job.id))
        file_utils.write_file(os.path.join(self._schedules_folder, filename),
                              json.dumps(job.as_serializable_dict(), indent=2))
Ejemplo n.º 5
0
    def _execute_job(self, job: SchedulingJob):
        LOGGER.info('Executing ' + job.get_log_name())

        script_name = job.script_name
        parameter_values = job.parameter_values
        user = job.user

        try:
            config = self._config_service.load_config_model(script_name, user, parameter_values)
            self.validate_script_config(config)

            execution_id = self._execution_service.start_script(config, parameter_values, user)
            LOGGER.info('Started script #' + str(execution_id) + ' for ' + job.get_log_name())
        except:
            LOGGER.exception('Failed to execute ' + job.get_log_name())

        self.schedule_job(job)
Ejemplo n.º 6
0
    def schedule_job(self, job: SchedulingJob):
        schedule = job.schedule

        if not schedule.repeatable and date_utils.is_past(schedule.start_datetime):
            return

        next_datetime = schedule.get_next_time()
        LOGGER.info(
            'Scheduling ' + job.get_log_name() + ' at ' + next_datetime.astimezone(tz=None).strftime('%H:%M, %d %B %Y'))

        self.scheduler.enterabs(next_datetime.timestamp(), 1, self._execute_job, (job,))