Пример #1
0
    def __init__(self, api_session_manager, api_name, wrapped_name, settings):
        self.api_session_manager = api_session_manager
        self.api_name = api_name
        self.wrapped_name = wrapped_name

        self.name = (
            settings.get('unique_name')
            or '%s:%s.%s' % (self.application_name, self.api_name, wrapped_name))

        self.active = False
        self.next_date = None
        self.updating = False
        self.last_called_date = None

        self.domain_name = settings.get('domain_name', None)
        self.title = settings.get('title', None)

        cron_settings = {}
        for key in DATES_RANGES.keys():
            if key in settings:
                cron_settings[key] = settings[key]
        self.cron = Cron(**cron_settings)

        self.enable()
Пример #2
0
class APIJob(object):
    def __init__(self, api_session_manager, api_name, wrapped_name, settings):
        self.api_session_manager = api_session_manager
        self.api_name = api_name
        self.wrapped_name = wrapped_name

        self.name = (
            settings.get('unique_name')
            or '%s:%s.%s' % (self.application_name, self.api_name, wrapped_name))

        self.active = False
        self.next_date = None
        self.updating = False
        self.last_called_date = None

        self.domain_name = settings.get('domain_name', None)
        self.title = settings.get('title', None)

        cron_settings = {}
        for key in DATES_RANGES.keys():
            if key in settings:
                cron_settings[key] = settings[key]
        self.cron = Cron(**cron_settings)

        self.enable()

    def __eq__(self, other):
        return isinstance(other, APIJob) and self.name == other.name

    def __hash__(self):
        return hash(self.name)

    def __repr__(self):
        return '%s (%s)' % (self.name, self.next_date)

    @property
    def application_name(self):
        return self.api_session_manager.config.application_name

    def disable(self):
        if self.active:
            self.active = False
            self.next_date = None

    def enable(self):
        if not self.domain_name:
            self.active = True
        elif self.domain_name == DOMAIN_NAME:
            self.active = True
        else:
            self.active = False
            self.next_date = None

        if self.active and not self.next_date:
            self.find_next()

    def find_next(self):
        if self.active:
            try:
                self.next_date = self.cron.find_next(NOW())
            except NoMoreDates:
                self.next_date = None
        else:
            self.next_date = None

    def will_run(self):
        return bool(self.active and not self.updating and self.next_date)

    def __call__(self):
        if self.will_run():
            api_session = self.api_session_manager.system_session(self)

            lock_key = JOBS_LOCK_KEY(self.name)
            if lock_key not in self.api_session_manager.config.cache.lockme:
                try:
                    self.api_session_manager.config.cache.lock(lock_key, timeout=1)
                except LockTimeout:
                    api_session.logging.log_error('job_locked', u('Job already running.'))
                else:
                    self.updating = True
                    self.last_called_date = NOW()

                    session = getattr(api_session, self.api_name)
                    try:
                        getattr(session, self.wrapped_name)()
                    except (BaseException, Exception) as error:
                        api_session.logging.log_critical('jobs_error', str(error))
                    else:
                        jobs_session = getattr(api_session, self.api_session_manager.__api_name__)
                        jobs_session.after_job_running()
                finally:
                    self.updating = False
                    self.api_session_manager.config.cache.unlock(lock_key)
                    self.find_next()