def test_posting_consume_wait(self): with connect_close(self.board): jb = self.board.post('test', p_utils.temporary_log_book()) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.board.claim(possible_jobs[0], self.board.name) self.board.consume(possible_jobs[0], self.board.name) self.assertTrue(jb.wait())
def test_posting_no_post(self): with connect_close(self.board): with mock.patch.object(self.client, 'create') as create_func: create_func.side_effect = IOError("Unable to post") self.assertRaises(IOError, self.board.post, 'test', p_utils.temporary_log_book()) self.assertEqual(0, self.board.job_count)
def test_posting_no_post(self): with base.connect_close(self.board): with mock.patch.object(self.client, 'create') as create_func: create_func.side_effect = IOError("Unable to post") self.assertRaises(IOError, self.board.post, 'test', p_utils.temporary_log_book()) self.assertEqual(0, self.board.job_count)
def test_trashing_claimed_job(self): with base.connect_close(self.board): with base.flush(self.client): j = self.board.post('test', p_utils.temporary_log_book()) self.assertEqual(states.UNCLAIMED, j.state) with base.flush(self.client): self.board.claim(j, self.board.name) self.assertEqual(states.CLAIMED, j.state) with base.flush(self.client): self.board.trash(j, self.board.name) trashed = [] jobs = [] paths = list(six.iteritems(self.client.storage.paths)) for (path, value) in paths: if path in self.bad_paths: continue if path.find(impl_zookeeper.TRASH_FOLDER) > -1: trashed.append(path) elif (path.find(self.board._job_base) > -1 and not path.endswith(impl_zookeeper.LOCK_POSTFIX)): jobs.append(path) self.assertEqual(len(trashed), 1) self.assertEqual(len(jobs), 0)
def test_posting_received_raw(self): book = p_utils.temporary_log_book() with base.connect_close(self.board): self.assertTrue(self.board.connected) self.assertEqual(0, self.board.job_count) posted_job = self.board.post('test', book) self.assertEqual(self.board, posted_job.board) self.assertEqual(1, self.board.job_count) self.assertIn(posted_job.uuid, [j.uuid for j in self.board.iterjobs()]) # Remove paths that got created due to the running process that we are # not interested in... paths = {} for (path, data) in six.iteritems(self.client.storage.paths): if path in self.bad_paths: continue paths[path] = data # Check the actual data that was posted. self.assertEqual(1, len(paths)) path_key = list(six.iterkeys(paths))[0] self.assertTrue(len(paths[path_key]['data']) > 0) self.assertDictEqual({ 'uuid': posted_job.uuid, 'name': posted_job.name, 'book': { 'name': book.name, 'uuid': book.uuid, }, 'details': {}, }, jsonutils.loads(misc.binary_decode(paths[path_key]['data'])))
def test_posting_dates(self, mock_dt): epoch = misc.millis_to_datetime(0) mock_dt.return_value = epoch with base.connect_close(self.board): j = self.board.post('test', p_utils.temporary_log_book()) self.assertEqual(epoch, j.created_on) self.assertEqual(epoch, j.last_modified) self.assertTrue(mock_dt.called)
def test_posting_dates(self, mock_dt): epoch = misc.millis_to_datetime(0) mock_dt.return_value = epoch with connect_close(self.board): j = self.board.post('test', p_utils.temporary_log_book()) self.assertEqual(epoch, j.created_on) self.assertEqual(epoch, j.last_modified) self.assertTrue(mock_dt.called)
def test_posting_abandon_no_owner(self): with connect_close(self.board): with flush(self.client): self.board.post('test', p_utils.temporary_log_book()) self.assertEqual(1, self.board.job_count) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(1, len(possible_jobs)) j = possible_jobs[0] self.assertRaises(excp.JobFailure, self.board.abandon, j, j.name)
def test_posting_claim_diff_owner(self): with connect_close(self.board): with self.flush(self.client): self.board.post("test", p_utils.temporary_log_book()) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(1, len(possible_jobs)) with self.flush(self.client): self.board.claim(possible_jobs[0], self.board.name) possible_jobs = list(self.board.iterjobs()) self.assertEqual(1, len(possible_jobs)) self.assertRaises(excp.UnclaimableJob, self.board.claim, possible_jobs[0], self.board.name + "-1") possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(0, len(possible_jobs))
def test_posting_claim_diff_owner(self): with connect_close(self.board): self.board.post('test', p_utils.temporary_log_book()) self.client.flush() possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(1, len(possible_jobs)) self.board.claim(possible_jobs[0], self.board.name) self.client.flush() possible_jobs = list(self.board.iterjobs()) self.assertEqual(1, len(possible_jobs)) self.assertRaises(excp.UnclaimableJob, self.board.claim, possible_jobs[0], self.board.name + "-1") possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(0, len(possible_jobs))
def test_posting_claim_abandon(self): with connect_close(self.board): with flush(self.client): self.board.post('test', p_utils.temporary_log_book()) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(1, len(possible_jobs)) j = possible_jobs[0] with flush(self.client): self.board.claim(j, self.board.name) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(0, len(possible_jobs)) with flush(self.client): self.board.abandon(j, self.board.name) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(1, len(possible_jobs))
def test_posting_claim_abandon(self): with connect_close(self.board): with self.flush(self.client): self.board.post('test', p_utils.temporary_log_book()) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(1, len(possible_jobs)) j = possible_jobs[0] with self.flush(self.client): self.board.claim(j, self.board.name) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(0, len(possible_jobs)) with self.flush(self.client): self.board.abandon(j, self.board.name) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(1, len(possible_jobs))
def test_posting_claim_consume(self): with connect_close(self.board): with self.flush(self.client): self.board.post("test", p_utils.temporary_log_book()) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(1, len(possible_jobs)) j = possible_jobs[0] with self.flush(self.client): self.board.claim(j, self.board.name) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(0, len(possible_jobs)) with self.flush(self.client): self.board.consume(j, self.board.name) self.assertEqual(0, len(list(self.board.iterjobs()))) self.assertRaises(excp.NotFound, self.board.consume, j, self.board.name)
def test_posting_owner_lost(self): with connect_close(self.board): j = self.board.post('test', p_utils.temporary_log_book()) self.client.flush() self.assertEqual(states.UNCLAIMED, j.state) self.board.claim(j, self.board.name) self.client.flush() self.assertEqual(states.CLAIMED, j.state) # Forcefully delete the owner from the backend storage to make # sure the job becomes unclaimed (this may happen if some admin # manually deletes the lock). paths = list(six.iteritems(self.client.storage.paths)) for (path, value) in paths: if path in self.bad_paths: continue if path.endswith('lock'): value['data'] = misc.binary_encode(jsonutils.dumps({})) self.assertEqual(states.UNCLAIMED, j.state)
def test_posting_state_lock_lost(self): with base.connect_close(self.board): with base.flush(self.client): j = self.board.post('test', p_utils.temporary_log_book()) self.assertEqual(states.UNCLAIMED, j.state) with base.flush(self.client): self.board.claim(j, self.board.name) self.assertEqual(states.CLAIMED, j.state) # Forcefully delete the lock from the backend storage to make # sure the job becomes unclaimed (this may happen if some admin # manually deletes the lock). paths = list(six.iteritems(self.client.storage.paths)) for (path, value) in paths: if path in self.bad_paths: continue if path.endswith("lock"): self.client.storage.pop(path) self.assertEqual(states.UNCLAIMED, j.state)
def test_posting_claim_consume(self): with connect_close(self.board): self.board.post('test', p_utils.temporary_log_book()) self.client.flush() possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(1, len(possible_jobs)) j = possible_jobs[0] self.board.claim(j, self.board.name) self.client.flush() possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(0, len(possible_jobs)) self.board.consume(j, self.board.name) self.client.flush() self.assertEqual(0, len(list(self.board.iterjobs()))) self.assertRaises(excp.JobNotFound, self.board.consume, j, self.board.name)
def test_posting_claim_same_owner(self): with base.connect_close(self.board): with self.flush(self.client): self.board.post('test', p_utils.temporary_log_book()) self.assertEqual(1, self.board.job_count) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(1, len(possible_jobs)) j = possible_jobs[0] self.assertEqual(states.UNCLAIMED, j.state) with self.flush(self.client): self.board.claim(j, self.board.name) possible_jobs = list(self.board.iterjobs()) self.assertEqual(1, len(possible_jobs)) with self.flush(self.client): self.assertRaises(excp.UnclaimableJob, self.board.claim, possible_jobs[0], self.board.name) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(0, len(possible_jobs))
def test_posting_claim_expiry(self): with base.connect_close(self.board): with self.flush(self.client): self.board.post('test', p_utils.temporary_log_book()) self.assertEqual(1, self.board.job_count) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(1, len(possible_jobs)) j = possible_jobs[0] self.assertEqual(states.UNCLAIMED, j.state) with self.flush(self.client): self.board.claim(j, self.board.name, expiry=0.5) self.assertEqual(self.board.name, self.board.find_owner(j)) self.assertEqual(states.CLAIMED, j.state) time.sleep(0.6) self.assertEqual(states.UNCLAIMED, j.state) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(1, len(possible_jobs))
def test_posting_claim(self): with connect_close(self.board): self.board.post('test', p_utils.temporary_log_book()) self.client.flush() self.assertEqual(1, self.board.job_count) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(1, len(possible_jobs)) j = possible_jobs[0] self.assertEqual(states.UNCLAIMED, j.state) self.board.claim(j, self.board.name) self.client.flush() self.assertEqual(self.board.name, self.board.find_owner(j)) self.assertEqual(states.CLAIMED, j.state) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(0, len(possible_jobs)) self.assertRaisesAttrAccess(excp.JobNotFound, j, 'state') self.assertRaises(excp.JobNotFound, self.board.consume, j, self.board.name)
def test_posting_claim(self): with connect_close(self.board): with self.flush(self.client): self.board.post('test', p_utils.temporary_log_book()) self.assertEqual(1, self.board.job_count) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(1, len(possible_jobs)) j = possible_jobs[0] self.assertEqual(states.UNCLAIMED, j.state) with self.flush(self.client): self.board.claim(j, self.board.name) self.assertEqual(self.board.name, self.board.find_owner(j)) self.assertEqual(states.CLAIMED, j.state) possible_jobs = list(self.board.iterjobs(only_unclaimed=True)) self.assertEqual(0, len(possible_jobs)) self.close_client(self.client) self.assertRaisesAttrAccess(excp.JobFailure, j, 'state')
def poster(wait_post=0.2): if not ev.wait(test_utils.WAIT_TIMEOUT): raise RuntimeError("Waiter did not appear ready" " in %s seconds" % test_utils.WAIT_TIMEOUT) time.sleep(wait_post) self.board.post('test', p_utils.temporary_log_book())
def poster(wait_post=0.2): ev.wait() # wait until the waiter is active time.sleep(wait_post) self.board.post('test', p_utils.temporary_log_book())
def execute(self): print('executing %s' % self) return 'ok' def flow_factory(): return lf.Flow('resume from backend example').add( TestTask(name='first'), InterruptTask(name='boom'), TestTask(name='second')) ### INITIALIZE PERSISTENCE #################################### backend = get_backend() logbook = p_utils.temporary_log_book(backend) ### CREATE AND RUN THE FLOW: FIRST ATTEMPT #################### flow = flow_factory() flowdetail = p_utils.create_flow_detail(flow, logbook, backend) engine = taskflow.engines.load(flow, flow_detail=flowdetail, backend=backend) print_task_states(flowdetail, "At the beginning, there is no state") print_wrapped("Running") engine.run() print_task_states(flowdetail, "After running")
from taskflow.engines.action_engine import engine from taskflow.patterns import linear_flow as lf from taskflow.persistence.backends import impl_memory from taskflow import task from taskflow.utils import persistence_utils # INTRO: This examples shows how to run a engine using the engine iteration # capability, in between iterations other activities occur (in this case a # value is output to stdout); but more complicated actions can occur at the # boundary when a engine yields its current state back to the caller. class EchoNameTask(task.Task): def execute(self): print(self.name) f = lf.Flow("counter") for i in range(0, 10): f.add(EchoNameTask("echo_%s" % (i + 1))) be = impl_memory.MemoryBackend() book = persistence_utils.temporary_log_book(be) fd = persistence_utils.create_flow_detail(f, book, be) e = engine.SingleThreadedActionEngine(f, fd, be, {}) e.compile() e.prepare() for i, st in enumerate(e.run_iter(), 1): print("Transition %s: %s" % (i, st))
def setUp(self): super(EngineTestBase, self).setUp() self.values = [] self.backend = impl_memory.MemoryBackend(conf={}) self.book = p_utils.temporary_log_book(self.backend)
def test_fresh_iter(self): with connect_close(self.board): book = p_utils.temporary_log_book() self.board.post('test', book) jobs = list(self.board.iterjobs(ensure_fresh=True)) self.assertEqual(1, len(jobs))
class TestTask(task.Task): def execute(self): print('executing %s' % self) return 'ok' def flow_factory(): return lf.Flow('resume from backend example').add( TestTask(name='first'), InterruptTask(name='boom'), TestTask(name='second')) ### INITIALIZE PERSISTENCE #################################### backend = get_backend() logbook = p_utils.temporary_log_book(backend) ### CREATE AND RUN THE FLOW: FIRST ATTEMPT #################### flow = flow_factory() flowdetail = p_utils.create_flow_detail(flow, logbook, backend) engine = taskflow.engines.load(flow, flow_detail=flowdetail, backend=backend) print_task_states(flowdetail, "At the beginning, there is no state") print_wrapped("Running") engine.run() print_task_states(flowdetail, "After running") ### RE-CREATE, RESUME, RUN #################################### print_wrapped("Resuming and running again")
def test_posting_no_consume_wait(self): with connect_close(self.board): jb = self.board.post('test', p_utils.temporary_log_book()) self.assertFalse(jb.wait(0.1))
else: f.add( EchoTask(name="echoer_%s" % curr_value, rebind={'value': curr_value})) curr_value = next_value return f # Adjust this number to change how many engines/flows run at once. flow_count = 1 flows = [] for i in range(0, flow_count): f = make_alphabet_flow(i + 1) flows.append(make_alphabet_flow(i + 1)) be = persistence_backends.fetch(conf={'connection': 'memory'}) book = persistence_utils.temporary_log_book(be) engine_iters = [] for f in flows: fd = persistence_utils.create_flow_detail(f, book, be) e = engines.load(f, flow_detail=fd, backend=be, book=book) e.compile() e.storage.inject({'A': 'A'}) e.prepare() engine_iters.append(e.run_iter()) while engine_iters: for it in list(engine_iters): try: print(six.next(it)) except StopIteration: engine_iters.remove(it)