Make currently implemented jobs use @functools.total_ordering

This allows jobs to be fully be comparable using the total
ordering function to provide the complexity around the various
functions needed to achieve/perform comparisons.

Also fixes up the various job __eq__ and __lt__ methods to
correctly return 'NotImplemented' on unknown types and adds
a __hash__ to the redis job (so that it can be used in hashable
collections, just like the zookeeper job class).

Change-Id: I8820d5cc6b2e7f346ac329f011f41b76fa94b777
This commit is contained in:
Joshua Harlow
2015-07-08 18:35:57 -07:00
parent df5fbe469c
commit 14de80d4b1
2 changed files with 28 additions and 3 deletions

View File

@@ -16,6 +16,7 @@
import contextlib
import datetime
import functools
import string
import threading
import time
@@ -59,6 +60,7 @@ def _translate_failures():
" internal error")
@functools.total_ordering
class RedisJob(base.Job):
"""A redis job."""
@@ -127,10 +129,24 @@ class RedisJob(base.Job):
prior_version=self._redis_version)
def __lt__(self, other):
if self.created_on == other.created_on:
return self.sequence < other.sequence
if not isinstance(other, RedisJob):
return NotImplemented
if self.board.listings_key == other.board.listings_key:
if self.created_on == other.created_on:
return self.sequence < other.sequence
else:
return self.created_on < other.created_on
else:
return self.created_on < other.created_on
return self.board.listings_key < other.board.listings_key
def __eq__(self, other):
if not isinstance(other, RedisJob):
return NotImplemented
return ((self.board.listings_key, self.created_on, self.sequence) ==
(other.board.listings_key, other.created_on, other.sequence))
def __hash__(self):
return hash((self.board.listings_key, self.created_on, self.sequence))
@property
def created_on(self):

View File

@@ -41,6 +41,7 @@ from taskflow.utils import misc
LOG = logging.getLogger(__name__)
@functools.total_ordering
class ZookeeperJob(base.Job):
"""A zookeeper job."""
@@ -171,11 +172,19 @@ class ZookeeperJob(base.Job):
return states.CLAIMED
def __lt__(self, other):
if not isinstance(other, ZookeeperJob):
return NotImplemented
if self.root == other.root:
return self.sequence < other.sequence
else:
return self.root < other.root
def __eq__(self, other):
if not isinstance(other, ZookeeperJob):
return NotImplemented
return ((self.root, self.sequence) ==
(other.root, other.sequence))
def __hash__(self):
return hash(self.path)