Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][REF] queue_job: Use set.discard(...) instead of SafeSet #719

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 10 additions & 30 deletions queue_job/jobrunner/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@
def add(self, o):
if o is None:
raise ValueError()
if o in self._removed:
self._removed.remove(o)
self._removed.discard(o)
if o in self._known:
return
self._known.add(o)
Expand All @@ -87,8 +86,7 @@
raise ValueError()
if o not in self._known:
return
if o not in self._removed:
self._removed.add(o)
self._removed.add(o)

def pop(self):
while True:
Expand All @@ -104,24 +102,6 @@
return o


class SafeSet(set):
"""A set that does not raise KeyError when removing non-existent items.

>>> s = SafeSet()
>>> s.remove(1)
>>> len(s)
0
>>> s.remove(1)
"""

def remove(self, o):
# pylint: disable=missing-return,except-pass
try:
super().remove(o)
except KeyError:
pass


@total_ordering
class ChannelJob(object):
"""A channel job is attached to a channel and holds the properties of a
Expand Down Expand Up @@ -408,8 +388,8 @@
self.parent.children[name] = self
self.children = {}
self._queue = ChannelQueue()
self._running = SafeSet()
self._failed = SafeSet()
self._running = set()
self._failed = set()
self._pause_until = 0 # utc seconds since the epoch
self.capacity = capacity
self.throttle = throttle # seconds
Expand Down Expand Up @@ -463,8 +443,8 @@
def remove(self, job):
"""Remove a job from the channel."""
self._queue.remove(job)
self._running.remove(job)
self._failed.remove(job)
self._running.discard(job)
self._failed.discard(job)
if self.parent:
self.parent.remove(job)

Expand All @@ -484,8 +464,8 @@
"""
if job not in self._queue:
self._queue.add(job)
self._running.remove(job)
self._failed.remove(job)
self._running.discard(job)
self._failed.discard(job)
if self.parent:
self.parent.remove(job)
_logger.debug("job %s marked pending in channel %s", job.uuid, self)
Expand All @@ -498,7 +478,7 @@
if job not in self._running:
self._queue.remove(job)
self._running.add(job)
self._failed.remove(job)
self._failed.discard(job)

Check warning on line 481 in queue_job/jobrunner/channels.py

View check run for this annotation

Codecov / codecov/patch

queue_job/jobrunner/channels.py#L481

Added line #L481 was not covered by tests
if self.parent:
self.parent.set_running(job)
_logger.debug("job %s marked running in channel %s", job.uuid, self)
Expand All @@ -507,7 +487,7 @@
"""Mark the job as failed."""
if job not in self._failed:
self._queue.remove(job)
self._running.remove(job)
self._running.discard(job)
self._failed.add(job)
if self.parent:
self.parent.remove(job)
Expand Down
Loading