Ejemplo n.º 1
0
    def run_command(self, id=-1, command=None, d=None, caller=None):
        """
        Sends a command to the proper MessagingQueue with the proper data object and command string.
        After a successful send if the object is not processing a QUIT command, the HitParadeBotCommandProcessor then listens to an incoming queue
        where the WebScraper sends a reply as to the success or failure of the command.

        The QUIT command is simply sent and all queues associated with the id are destroyed.
        :param id: int ID of the webscraper.
        :param command: str Command to perform.
        :param d: dict data dictionary of the command request
        :param caller: Thread calling this action usually  the main thread.  Used for logging purposes.
        :return:
        """
        MessagingQueue.send_msg(id=id,
                                direction='in',
                                cmd=command,
                                d=d,
                                caller=caller)
        command = command
        val = None
        if not command == 'QUIT':
            command, val = MessagingQueue.wait_for_msg(direction='out',
                                                       id=id,
                                                       caller=caller)
        print('command %s  ' % command)
        return command, val
Ejemplo n.º 2
0
 def respond(self, obj=None, command=None):
     """
     Sends a message to the main thread.
     :param obj: dict to send to the main thread
     :param command: str command being used.
     :return:
     """
     MessagingQueue.send_msg(id=self.get_id(),
                             direction='out',
                             cmd=command,
                             d=obj,
                             caller=str(self.get_id()))
Ejemplo n.º 3
0
 def run(self):
     exception_count = 0
     while not self.stopped() and (
             not self.state_storage_get_prop('exit_on_exception') or
         (self.state_storage_get_prop('exit_on_exception')
          and exception_count == 0)):
         #acquire lock
         print('<<messaging lock acquire [%s] producer [%s]>>' %
               (self.id, self.state_storage_get_prop('producer_id')))
         self.message_lock.acquire()
         #read message from cache
         message = self.next_msg()
         #check the message
         if message and not isinstance(
                 message['data'],
                 int) and not message['data'].decode('utf-8').isdigit():
             #send message to producer thread
             MessagingQueue.send_msg(
                 id=self.state_storage_get_prop('producer_id'),
                 direction='in',
                 cmd='SEND',
                 d=message,
                 caller=str(self.id))
             #listen to completion message from thread
             command, obj = MessagingQueue.wait_for_msg(id=self.id,
                                                        direction='in',
                                                        caller='Messaging')
             #release lock
         print('<<messaging lock release [%s] producer [%s]>>' %
               (self.id, self.state_storage_get_prop('producer_id')))
         self.message_lock.release()
         print('setting available from id %s for producer %s ' %
               (self.id, self.state_storage_get_prop('producer_id')))
         self.set_available()
         #sleep
         time.sleep(self.sleep_time)