def submit_job(self, serialized_job, job_key, job_id, replace_exist): """ Receive submit_job rpc request from worker. :type serialized_job str or xmlrpclib.Binary :type job_key str :type job_id str :type replace_exist bool """ self.log.debug('client call submit job, id=%s, key=%s' % (job_id, job_key)) if isinstance(serialized_job, Binary): serialized_job = serialized_job.data job_in_dict = Job.deserialize_to_dict(serialized_job) # if job doesn't contains trigger, then enqueue it into job queue immediately if not job_in_dict['trigger']: self._enqueue_job(job_key, serialized_job) # else store job into job store first else: # should I need a lock here? with self.jobstore_lock: try: self.jobstore.add_job(job_id, job_key, job_in_dict['next_run_time'], serialized_job) except JobAlreadyExist: if replace_exist: self.jobstore.update_job(job_id, job_key, job_in_dict['next_run_time'], serialized_job) else: self.log.warning('submit job error. job id%s already exist' % job_id) # wake up when new job has store into job store self.wake_up()
def start(self): """ Start elric master. Select all due jobs from jobstore and enqueue them into redis queue. Then update due jobs' information into jobstore. :return: """ if self.running: raise AlreadyRunningException self._stopped = False self.log.debug('eric master start...') while True: now = datetime.now(self.timezone) wait_seconds = None with self.jobstore_lock: for job_id, job_key, serialized_job in self.jobstore.get_due_jobs(now): # enqueue due job into redis queue self._enqueue_job(job_key, serialized_job) # update job's information, such as next_run_time job_in_dict = Job.deserialize_to_dict(serialized_job) last_run_time = Job.get_serial_run_times(job_in_dict, now) if last_run_time: next_run_time = Job.get_next_trigger_time(job_in_dict, last_run_time[-1]) if next_run_time: job_in_dict['next_run_time'] = next_run_time self.update_job(job_id, job_key, next_run_time, Job.dict_to_serialization(job_in_dict)) else: # if job has no next run time, then remove it from jobstore self.remove_job(job_id=job_id) # get next closet run time job from jobstore and set it to be wake up time closest_run_time = self.jobstore.get_closest_run_time() if closest_run_time is not None: wait_seconds = max(timedelta_seconds(closest_run_time - now), 0) self.log.debug('Next wakeup is due at %s (in %f seconds)' % (closest_run_time, wait_seconds)) self._event.wait(wait_seconds if wait_seconds is not None else self.MAX_WAIT_TIME) self._event.clear()
def submit_job(self, serialized_job, job_key, job_id, replace_exist): def exist(key, value): with self.filter_lock: try: return self.filter_list[key].exist(value) except KeyError: self.filter_list[key] = MemoryFilter() return self.filter_list[key].exist(value) self.log.debug("client call submit job %s" % job_id) if isinstance(serialized_job, Binary): serialized_job = serialized_job.data job_in_dict = Job.deserialize_to_dict(serialized_job) filter_key = job_in_dict['filter_key'] filter_value = job_in_dict['filter_value'] if filter_key and filter_value: if exist(filter_key, filter_value): self.log.debug("%s has been filter..." % filter_value) return False if not job_in_dict['trigger']: self._enqueue_job(job_key, serialized_job) else: with self.jobstore_lock: try: self.jobstore.add_job(job_id, job_key, job_in_dict['next_run_time'], serialized_job) except JobAlreadyExist: if replace_exist: self.jobstore.update_job(job_id, job_key, job_in_dict['next_run_time'], serialized_job) else: self.log.warn('job %s already exist' % job_id) self.wake_up() return True