Exemplo n.º 1
0
    def remove_job(self, job_id, jobstore=None):
        """
        Removes a job, preventing it from being run any more.

        :param str|unicode job_id: the identifier of the job
        :param str|unicode jobstore: alias of the job store that contains the job
        :raises JobLookupError: if the job was not found
        """
        jobstore = jobstore if jobstore else self._jobstore
        with self._jobstore_lock:
            # Check if the job is among the pending jobs
            for i, (job, jobstore_alias,
                    replace_existing) in enumerate(self._pending_jobs):
                if job.id == job_id:
                    del self._pending_jobs[i]
                    break
            else:
                # Otherwise, try to remove it from each store until it succeeds or we run out of store to check
                jobstore.remove_job(job_id)

        # Notify listeners that a job has been removed
        event = JobEvent(EVENT_JOB_REMOVED, job_id, jobstore)
        self._dispatch_event(event)

        logging.info('Removed job %s', job_id)
Exemplo n.º 2
0
    def modify_job(self, job_id, jobstore=None, **changes):
        """
        Modifies the properties of a single job. Modifications are passed to this method as extra keyword arguments.

        :param str|unicode job_id: the identifier of the job
        :param str|unicode jobstore: alias of the job store that contains the job
        """

        self._dispatch_event(
            JobEvent(EVENT_JOB_MODIFIED, job_id, self._jobstore))

        # Wake up the scheduler since the job's next run time may have been changed
        self.wakeup()
Exemplo n.º 3
0
    def _real_add_job(self, job, jobstore_alias, replace_existing, wakeup):
        """
        :param Job job: the job to add
        :param bool replace_existing: ``True`` to use update_job() in case the job already exists in the store
        :param bool wakeup: ``True`` to wake up the scheduler after adding the job
        """
        # Fill in undefined values with defaults
        replacements = {}
        for key, value in six.iteritems(self._job_defaults):
            if not hasattr(job, key):
                replacements[key] = value

        # Calculate the next run time if there is none defined
        if not hasattr(job, 'next_run_time'):
            now = datetime.now(self.timezone)
            replacements['next_run_time'] = job.trigger.get_next_fire_time(
                None, now)

        # Apply any replacements
        job._modify(**replacements)

        # Add the job to the given job store
        try:
            self._jobstore.add_job(job)
        except ConflictingIdError:
            if replace_existing:
                print(
                    "fail to add job into store cause of exists, try to replace it then."
                )
                logging.error(
                    "fail to add job into store cause of exists, try to replace it then."
                )
                self._jobstore.update_job(job)
            else:
                raise

        # Mark the job as no longer pending
        job._jobstore_alias = jobstore_alias

        # Notify listeners that a new job has been added
        event = JobEvent(EVENT_JOB_ADDED, job.id, jobstore_alias)
        self._dispatch_event(event)

        logging.info('Added job "%s" to job store "%s"', job.name,
                     jobstore_alias)

        # Notify the scheduler about the new job
        if wakeup:
            self.wakeup()
Exemplo n.º 4
0
    def remove_job(self, job_id, jobstore=None):
        """
        Removes a job, preventing it from being run any more.

        :param str|unicode job_id: the identifier of the job
        :param str|unicode jobstore: alias of the job store that contains the job
        :raises JobLookupError: if the job was not found

        """
        jobstore_alias = None
        with self._jobstores_lock:
            if self.state == STATE_STOPPED:
                # Check if the job is among the pending jobs
                if self.state == STATE_STOPPED:
                    for i, (job, alias,
                            replace_existing) in enumerate(self._pending_jobs):
                        if job.id == job_id and jobstore in (None, alias):
                            del self._pending_jobs[i]
                            jobstore_alias = alias
                            break
            else:
                # Otherwise, try to remove it from each store until it succeeds or we run out of
                # stores to check
                for alias, store in six.iteritems(self._jobstores):
                    if jobstore in (None, alias):
                        try:
                            store.remove_job(job_id)
                            jobstore_alias = alias
                            break
                        except JobLookupError:
                            continue

        if jobstore_alias is None:
            raise JobLookupError(job_id)

        # Notify listeners that a job has been removed
        event = JobEvent(EVENT_JOB_REMOVED, job_id, jobstore_alias)
        self._dispatch_event(event)

        self._logger.info('Removed job %s', job_id)
Exemplo n.º 5
0
    def modify_job(self, job_id, jobstore=None, **changes):
        """
        Modifies the properties of a single job.

        Modifications are passed to this method as extra keyword arguments.

        :param str|unicode job_id: the identifier of the job
        :param str|unicode jobstore: alias of the job store that contains the job
        :return Job: the relevant job instance

        """
        with self._jobstores_lock:
            job, jobstore = self._lookup_job(job_id, jobstore)
            job._modify(**changes)
            if jobstore:
                self._lookup_jobstore(jobstore).update_job(job)

        self._dispatch_event(JobEvent(EVENT_JOB_MODIFIED, job_id, jobstore))

        # Wake up the scheduler since the job's next run time may have been changed
        self.wakeup()

        return job
Exemplo n.º 6
0
 def test_get_event_str_job_event(self):
     event = JobEvent("code", "id", "jobstore")
     message = ProgramyScheduler.get_event_str(event)
     self.assertIsNotNone(message)
     self.assertEqual("JobEvent [code] [id] [jobstore] [None]", message)
Exemplo n.º 7
0
 def test_listener_event(self):
     event = JobEvent("code1", "id1", "mock")
     ProgramyScheduler.listener_event(event)