예제 #1
0
def wait_for_work(queue: beanstalk.Connection) -> str:
    """wait for work, return json"""
    while True:
        job = queue.reserve(timeout=0)
        if job:
            job_json = job.body
            job.delete()  # remove from the queue
            return json.loads(job_json)

        time.sleep(0.01)  # sleep for 10 ms to share the computer
예제 #2
0
파일: rig_runner.py 프로젝트: bp100a/RpiPG
def clear_all_queues(queue: beanstalk.Connection) -> None:
    """clear out all the currently known tubes"""
    for tube in [
            CANCEL_QUEUE, STATUS_QUEUE, TASK_QUEUE, google_drive.GDRIVE_QUEUE
    ]:
        queue.use(tube)
        while True:
            dummy = queue.reserve(timeout=0)
            if dummy:
                dummy.delete()
            else:
                break
예제 #3
0
def clear_tube(queue: beanstalk.Connection, tube: str):
    """ flush all messages tubes to the rig"""
    queue.use(tube)
    while True:
        try:
            job = queue.reserve(timeout=0)
            if job is None:
                return
            job.delete()
        except beanstalk.CommandFailed:
            pass
        except beanstalk.DeadlineSoon:
            pass
예제 #4
0
def get_status(queue: beanstalk.Connection) -> str:
    """return the status of the rig"""
    queue.watch(STATUS_QUEUE)
    try:
        job = queue.reserve(timeout=0) # don't wait
        if job is None:
            return None
        # we have status, let's get it and pull the
        # job out of the queue
        status_json = jsonify(job.body)
        job.delete()
        return status_json
    except beanstalk.DeadlineSoon:
        return None
    except beanstalk.CommandFailed:
        return None
예제 #5
0
def test_write_file(queue: beanstalk.Connection) -> None:
    """simple program to test out google drive file writing"""
    queue.use(TASK_QUEUE)
    queue.watch(TASK_QUEUE)
    job = queue.reserve(timeout=2)
    if job is None:
        return None

    job_json = json.loads(job.body)
    if job_json['task'] != 'token':
        return None

    # we have a token, read it out
    access_info = json.loads(job_json['value'])
    drive_obj = google_drive.GoogleDrive(access_info)
    drive_obj.find_root_folder('rpipg')

    return None
예제 #6
0
class JobService(object):
    """
    beanstalkd job service
    watching the appointed tube.
    """
    def __init__(self, host, port, tube):
        self.__host = host
        self.__port = port
        self.__tube = tube

    def connect(self):
        while True:
            try:
                self.__bs = Connection(self.__host, self.__port)
                print 'connected to %s:%s' % (self.__host, self.__port)
                self.__bs.watch(self.__tube)
                print 'watching %s' % self.__bs.watching()
                if not self.__bs is None:
                    return
            except:
                time.sleep(1)

    def process(self, job):
        """overwrite
        Realize your functions
        """
        pass

    def finish(self, job):
        job.delete()

    def start(self):
        self.connect()
        while True:
            try:
                job = self.__bs.reserve()
                print '>>[new job]', job, job.body
                self.process(job)
                self.finish(job)
            except Exception as ex:
                logging.error('job process exception', exc_info=ex)
                self.connect()
예제 #7
0
class JobService(object):
    """
    beanstalkd job service
    watching the appointed tube.
    """
    def __init__(self, host, port, tube):
        self.__host = host
        self.__port = port
        self.__tube = tube
    
    def connect(self):
        while True:
            try:
                self.__bs = Connection(self.__host, self.__port)
                print 'connected to %s:%s' % (self.__host, self.__port)
                self.__bs.watch(self.__tube)
                print 'watching %s' % self.__bs.watching()
                if not self.__bs is None:
                    return
            except:
                time.sleep(1)

    def process(self, job):
        """overwrite
        Realize your functions
        """
        pass

    def finish(self, job):
        job.delete()
    
    def start(self):
        self.connect()
        while True:
            try:
                job = self.__bs.reserve()
                print '>>[new job]', job, job.body
                self.process(job)
                self.finish(job)
            except Exception as ex:
                logging.error('job process exception', exc_info=ex)
                self.connect()
예제 #8
0
파일: rig_runner.py 프로젝트: bp100a/RpiPG
def wait_for_work(queue: beanstalk.Connection,
                  motor_controller: Raspi_MotorHAT) -> dict:
    """wait for work, return json"""
    idle_start = time.time()
    while True:
        queue.watch(TASK_QUEUE)
        job = queue.reserve(timeout=0)
        if job:
            job_json = job.body
            job.delete()  # remove from the queue
            return json.loads(job_json)

        time.sleep(0.01)  # sleep for 10 ms to share the computer

        # if we have been idle for too long
        # release the stepper motors so they
        # don't overheat
        if (time.time() -
                idle_start) > 600 and motor_controller.is_active:  # 10 minutes
            post_status(queue, "long idle, motors disabled")
            motor_controller.release_motors()
예제 #9
0
class Worker(object):
    log = logging.getLogger()

    def __init__(self, tube='default', host='localhost', port=11300):
        self.tube = tube
        self.host, self.port = host, port

        try:
            self._connect()

        except SocketError:
            self.log.error('start failed. unable to connect to queue.')
            self.reconnect()

    def _connect(self):
       self.conn = Connection(self.host, self.port)
       self._watch_only(self.tube)

    def _watch_only(self, tube):
        for t in self.conn.watching():
            if t != tube: self.conn.ignore(t)
        self.conn.watch(tube)

    def reconnect(self):
        while True:
            self.log.info('trying to reconnect to work queue.') 
            try:
                self._connect()

                self.log.info('reconnected to queue host=%s port=%d.' % (self.host, self.port))
                break

            except SocketError:
                self.log.error('reconnect failed. waiting 10 seconds before retrying.')
                time.sleep(10)

    def run(self):
        """ Worker main loop. Reserve job and execute. """
        try:
            while True:
                self.cycle()
        except KeyboardInterrupt:
            self.log.info('got exit request. bye bye!')
            pass

    def cycle(self):
        try:
            self.log.info('waiting for jobs.')

            job = self.conn.reserve()

            if job is not None:
                self.log.info('got job with id #%d' % job.jid)
                self.on_job(Job(job))
 
        except SocketError:
            self.reconnect()

        except Exception, e:
            self.log.exception('got unexpected exception when running job')
            pass # nothing else to do. the worker should keep running