예제 #1
0
 def instance(cls):
     '''
     Limits application to single instance
     '''
     with LockGuard(cls.__lock):
         if not hasattr(cls, "_instance"):
             cls._instance = cls()
     return cls._instance
예제 #2
0
 def create_task(self, task):
     with LockGuard(self.__lock):
         if task._id in self.task_store:
             return Result(0, TASK_ALREADY_EXIST_ERR,
                           "The task[%s]is exist already." % (task._id))
         self.task_store[task._id] = task
         self.task_queue.put(task)
     return Result(1)
예제 #3
0
    def instance(cls):
        if not os.path.exists('/var/run/docker.sock'):
            Log(1, 'DockerClient init fail,as the sock file not exist.')
            return None

        with LockGuard(cls.__lock):
            if not hasattr(cls, "_instance"):
                cls._instance = cls()
        return cls._instance
예제 #4
0
    def drop_task(self, user_id, task_id):
        with LockGuard(self.__lock):
            if task_id not in self.task_store:
                return True

            if self.task_store[task_id].is_finished(
            ) and user_id == self.task_store[task_id].user_id:
                del self.task_store[task_id]
                return True

        return False
예제 #5
0
    def create_slow_task(self, task):
        with LockGuard(self.__slow_task_lock):
            if task._id in self.task_store:
                return Result(0, TASK_ALREADY_EXIST_ERR,
                              "The task[%s]is exist already." % (task._id))
            self.task_store[task._id] = task

            key = task.get_queue_key(0)
            if key not in self.slow_task_threads:
                processor = Processor(key)
                processor.create_task(task)
                self.slow_task_threads[key] = processor
            else:
                self.slow_task_threads[key].create_task(task)

        return Result(1)
예제 #6
0
    def timeout(self):
        if len(self.task_store) == 0:
            return

        with LockGuard(self.__lock):
            arr = []
            for task_id, expire_time in self.finish_task.iteritems():
                if expire_time <= time.time():
                    arr.append(task_id)
                    if task_id in self.task_store:
                        del self.task_store[task_id]

            for _id in arr:
                self.finish_task.pop(_id, None)

        for task_id, tsk in self.task_store.iteritems():
            if task_id in self.finish_task:
                continue
            if tsk.is_finished():
                self.finish_task[task_id] = time.time(
                ) + self.delay  # 推迟2分钟销毁任务,等待轮询
예제 #7
0
 def instance(cls):
     with LockGuard(cls.__lock):
         if not hasattr(cls, "_instance"):
             cls._instance = cls()
     return cls._instance