Adam Litke has uploaded a new change for review.
Change subject: jobs: Add when_finished callback and wire up Job.abort
......................................................................
jobs: Add when_finished callback and wire up Job.abort
Add a callback mechanism that will be triggered when a job is done, failed, or
aborted. This will be used to auto-clear finished SDM jobs from the HostJobs
manager. Since jobs.Job only implements the abort job lifecycle elements, this
patch adds support to jobs.Job for calling the callback after aborting.
Future patches will enable the callback for the SDM Job run lifecycle.
Change-Id: I73d4c167a4cf1f1da4c1e2629b7afa9c01d338fd
Signed-off-by: Adam Litke <alitke(a)redhat.com>
---
M lib/vdsm/jobs.py
M tests/jobsTests.py
2 files changed, 16 insertions(+), 0 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/54/61254/1
diff --git a/lib/vdsm/jobs.py b/lib/vdsm/jobs.py
index 232981c..a4d3948 100644
--- a/lib/vdsm/jobs.py
+++ b/lib/vdsm/jobs.py
@@ -19,6 +19,7 @@
from __future__ import absolute_import
+import functools
import logging
import threading
@@ -70,6 +71,7 @@
self._status = STATUS.PENDING
self._description = description
self._error = None
+ self._finished_cb = lambda: None
@property
def id(self):
@@ -117,6 +119,10 @@
self._status = STATUS.ABORTED
logging.info('Job %r aborting...', self._id)
self._abort()
+ self._finished_cb()
+
+ def when_finished(self, fn, *args, **kwargs):
+ self._finished_cb = functools.partial(fn, *args, **kwargs)
def _abort(self):
"""
diff --git a/tests/jobsTests.py b/tests/jobsTests.py
index cb1cb89..0b2b7a1 100644
--- a/tests/jobsTests.py
+++ b/tests/jobsTests.py
@@ -158,6 +158,16 @@
self.assertEqual(response.error(jobs.AbortNotSupported.name),
jobs.abort(job.id))
+ def test_abort_finished_callback(self):
+ def cb():
+ called[0] = True
+
+ called = [False]
+ job = TestingJob()
+ job.when_finished(cb)
+ job.abort()
+ self.assertTrue(called[0])
+
@permutations([
[jobs.STATUS.PENDING, True],
[jobs.STATUS.RUNNING, True],
--
To view, visit
https://gerrit.ovirt.org/61254
To unsubscribe, visit
https://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I73d4c167a4cf1f1da4c1e2629b7afa9c01d338fd
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Adam Litke <alitke(a)redhat.com>