예제 #1
0
 def get_due_jobs(self, now):
     timestamp = datetime_to_utc_timestamp(now)
     job_ids = self.redis.zrangebyscore(self.run_times_key, 0, timestamp)
     if job_ids:
         job_states = self.redis.hmget(self.jobs_key, *job_ids)
         return self._reconstitute_jobs(six.moves.zip(job_ids, job_states))
     return []
예제 #2
0
    def add_job(self, job):
        if job.id in self._jobs_index:
            raise ConflictingIdError(job.id)

        timestamp = datetime_to_utc_timestamp(job.next_run_time)
        index = self._get_job_index(timestamp, job.id)
        self._jobs.insert(index, (job, timestamp))
        self._jobs_index[job.id] = (job, timestamp)
예제 #3
0
 def update_job(self, job):
     changes = {
         'next_run_time': datetime_to_utc_timestamp(job.next_run_time),
         'job_state': Binary(pickle.dumps(job.__getstate__(), self.pickle_protocol))
     }
     result = self.collection.update({'_id': job.id}, {'$set': changes})
     if result and result['n'] == 0:
         raise JobLookupError(id)
예제 #4
0
 def update_job(self, job):
     update = self.jobs_t.update().values(**{
         'next_run_time': datetime_to_utc_timestamp(job.next_run_time),
         'job_state': pickle.dumps(job.__getstate__(), self.pickle_protocol)
     }).where(self.jobs_t.c.id == job.id)
     result = self.engine.execute(update)
     if result.rowcount == 0:
         raise JobLookupError(id)
예제 #5
0
 def add_job(self, job):
     try:
         self.collection.insert({
             '_id': job.id,
             'next_run_time': datetime_to_utc_timestamp(job.next_run_time),
             'job_state': Binary(pickle.dumps(job.__getstate__(), self.pickle_protocol))
         })
     except DuplicateKeyError:
         raise ConflictingIdError(job.id)
예제 #6
0
    def get_due_jobs(self, now):
        now_timestamp = datetime_to_utc_timestamp(now)
        pending = []
        for job, timestamp in self._jobs:
            if timestamp is None or timestamp > now_timestamp:
                break
            pending.append(job)

        return pending
예제 #7
0
    def add_job(self, job):
        if self.redis.hexists(self.jobs_key, job.id):
            raise ConflictingIdError(job.id)

        with self.redis.pipeline() as pipe:
            pipe.multi()
            pipe.hset(self.jobs_key, job.id, pickle.dumps(job.__getstate__(), self.pickle_protocol))
            if job.next_run_time:
                pipe.zadd(self.run_times_key, datetime_to_utc_timestamp(job.next_run_time), job.id)
            pipe.execute()
예제 #8
0
 def add_job(self, job):
     insert = self.jobs_t.insert().values(**{
         'id': job.id,
         'next_run_time': datetime_to_utc_timestamp(job.next_run_time),
         'job_state': pickle.dumps(job.__getstate__(), self.pickle_protocol)
     })
     try:
         self.engine.execute(insert)
     except IntegrityError:
         raise ConflictingIdError(job.id)
예제 #9
0
    def update_job(self, job):
        if not self.redis.hexists(self.jobs_key, job.id):
            raise JobLookupError(job.id)

        with self.redis.pipeline() as pipe:
            pipe.hset(self.jobs_key, job.id, pickle.dumps(job.__getstate__(), self.pickle_protocol))
            if job.next_run_time:
                pipe.zadd(self.run_times_key, datetime_to_utc_timestamp(job.next_run_time), job.id)
            else:
                pipe.zrem(self.run_times_key, job.id)
            pipe.execute()
예제 #10
0
    def update_job(self, job):
        old_job, old_timestamp = self._jobs_index.get(job.id, (None, None))
        if old_job is None:
            raise JobLookupError(job.id)

        # If the next run time has not changed, simply replace the job in its present index.
        # Otherwise, reinsert the job to the list to preserve the ordering.
        old_index = self._get_job_index(old_timestamp, old_job.id)
        new_timestamp = datetime_to_utc_timestamp(job.next_run_time)
        if old_timestamp == new_timestamp:
            self._jobs[old_index] = (job, new_timestamp)
        else:
            del self._jobs[old_index]
            new_index = self._get_job_index(new_timestamp, job.id)
            self._jobs.insert(new_index, (job, new_timestamp))

        self._jobs_index[old_job.id] = (job, new_timestamp)
예제 #11
0
 def get_due_jobs(self, now):
     timestamp = datetime_to_utc_timestamp(now)
     return self._get_jobs({'next_run_time': {'$lte': timestamp}})
예제 #12
0
 def get_due_jobs(self, now):
     timestamp = datetime_to_utc_timestamp(now)
     return self._get_jobs(self.jobs_t.c.next_run_time <= timestamp)