Package Products :: Package ZenHub :: Module WorkerSelection |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenHub.WorkerSelection

     1  ##############################################################################
     2  #
     3  # Copyright (C) Zenoss, Inc. 2012, all rights reserved.
     4  #
     5  # This content is made available according to terms specified in
     6  # License.zenoss under the directory where your Zenoss product is installed.
     7  #
     8  ##############################################################################
     9
    10
    11  from collections import defaultdict
    12  from itertools import ifilter
    13  from zope.component import queryUtility, getUtilitiesFor
    14  from zope.interface import implements
    15  from .interfaces import IWorkerSelectionAlgorithm
    16
    17 -class InOrderSelection(object):
    18      """
    19      Simple selection algorithm that returns workers in the
    20      order in which they are given.
    21      """
    22      implements(IWorkerSelectionAlgorithm)
    23
    24 -    def getCandidateWorkerIds(self, workers, options):
    25          return (i for i, worker in enumerate(workers) if not worker.busy)
    26
    27 -class ReservationAwareSelection(InOrderSelection):
    28      """
    29      Selection algorithm that returns workers in the
    30      order in which they are given, and only returns workers
    31      above the reserved threshold.
    32      """
    33      implements(IWorkerSelectionAlgorithm)
    34
    35 -    def getCandidateWorkerIds(self, workers, options):
    36          return ifilter(lambda i: i >= options.workersReservedForEvents,
    37                         super(ReservationAwareSelection, self)\
    38                         .getCandidateWorkerIds(workers, options))
    39
    40 -class ReversedReservationAwareSelection(ReservationAwareSelection):
    41      """
    42      Selection algorithm that returns workers in the reverse
    43      order in which they are given, and only returns workers
    44      above the reserved threshold.
    45      """
    46      implements(IWorkerSelectionAlgorithm)
    47
    48 -    def getCandidateWorkerIds(self, workers, options):
    49          selection = super(ReversedReservationAwareSelection, self)\
    50                              .getCandidateWorkerIds(workers, options)
    51          return reversed(list(selection))
    52
    53
    54 -class WorkerSelector(object):
    55      """
    56      Singleton worker selector that apportions work to zenhub workers based on the
    57      configured utilities per method name.
    58      """
    59
    60 -    def __init__(self, options):
    61          self.options = options
    62          self.selectors = {}
    63
    64          for name, utility in getUtilitiesFor(IWorkerSelectionAlgorithm):
    65              self.selectors[name] = utility
    66          self.defaultSelector = self.selectors['']
    67
    68 -    def getCandidateWorkerIds(self, methodName, workerlist):
    69          selector = self.selectors.get(methodName, self.defaultSelector)
    70          return selector.getCandidateWorkerIds(workerlist, self.options)
    71