diff --git a/requirements.txt b/requirements.txt index 0f08daa..e3caf39 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ +celery python-dateutil diff --git a/swh/core/scheduling.py b/swh/core/scheduling.py new file mode 100644 index 0000000..8c4f52b --- /dev/null +++ b/swh/core/scheduling.py @@ -0,0 +1,20 @@ +# Copyright (C) 2015 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import celery + + +class Task(celery.Task): + """a schedulable task (abstract class) + + Sub-classes must implement the run() method + + Current implementation is based on Celery. See + http://docs.celeryproject.org/en/latest/reference/celery.app.task.html for + how to use tasks once instantiated + + """ + def run(self, *args, **kwargs): + raise NotImplementedError('tasks must implement the run() method') diff --git a/swh/core/tests/test_scheduling.py b/swh/core/tests/test_scheduling.py new file mode 100644 index 0000000..2527f33 --- /dev/null +++ b/swh/core/tests/test_scheduling.py @@ -0,0 +1,31 @@ +# Copyright (C) 2015 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import unittest + +from nose.tools import istest + +from swh.core import scheduling + + +class Scheduling(unittest.TestCase): + + @istest + def not_implemented_task(self): + class NotImplementedTask(scheduling.Task): + pass + + with self.assertRaises(NotImplementedError): + NotImplementedTask().run() + + @istest + def add_task(self): + class AddTask(scheduling.Task): + def run(self, x, y): + return x + y + + r = AddTask().apply([2, 3]) + self.assertTrue(r.successful()) + self.assertEqual(r.result, 5)