'Processes one set of messages from Task Scheduler.
The worker processes new jobs from the task master. For each job
we retrieve the session from the Task Scheduler.
Returns:
Total number of messages processed by this call.'

def RunOnce(self):

    start_time = time.time()
    processed = 0
    queue_manager = queue_manager_lib.QueueManager(token=self.token)
    for queue in self.queues:
        queue_manager.FreezeTimestamp()
        fetch_messages_start = time.time()
        notifications_by_priority = queue_manager.GetNotificationsByPriority(queue)
        stats.STATS.RecordEvent('worker_time_to_retrieve_notifications', (time.time() - fetch_messages_start))
        stuck_flows = notifications_by_priority.pop(queue_manager.STUCK_PRIORITY, [])
        if stuck_flows:
            self.ProcessStuckFlows(stuck_flows, queue_manager)
        notifications_available = []
        for priority in sorted(notifications_by_priority, reverse=True):
            for notification in notifications_by_priority[priority]:
                if (notification.session_id not in self.queued_flows):
                    notifications_available.append(notification)
        try:
            processed += self.ProcessMessages(notifications_available, queue_manager, (self.RUN_ONCE_MAX_SECONDS - (time.time() - start_time)))
        except Exception as e:
            logging.error('Error    processing    message    %s.    %s.', e, traceback.format_exc())
            stats.STATS.IncrementCounter('grr_worker_exceptions')
            if flags.FLAGS.debug:
                pdb.post_mortem()
        queue_manager.UnfreezeTimestamp()
        if ((time.time() - start_time) > self.RUN_ONCE_MAX_SECONDS):
            return processed
    return processed
