def test_activity_timeouts(monkeypatch, generators): """Test the creation of an activity timeouts. More details: the timeout of a task is 120s, the schedule to start is 1000, 100 activities are going to be scheduled when the generator is set. The schedule_to_start for all activities instance is: 10000 * 100 = 100k. The schedule to close is 100k + duration of an activity (which is 120s * 2). """ timeout = 120 start_timeout = 1000 @task.decorate(timeout=timeout) def local_task(): return monkeypatch.setattr(activity.Activity, '__init__', lambda self: None) current_activity = activity.Activity() current_activity.hydrate(dict(schedule_to_start=start_timeout)) current_activity.generators = generators current_activity.runner = runner.Sync(local_task.fill(), local_task.fill()) total_generators = pow(10, len(current_activity.generators)) schedule_to_start = start_timeout * total_generators for instance in current_activity.instances({}): assert current_activity.pool_size == total_generators assert instance.schedule_to_start == schedule_to_start assert instance.timeout == timeout * 2 assert instance.schedule_to_close == (schedule_to_start + instance.timeout)
def create_enter_coffee_activity(self): """Create the activity when user enters coffee shop.""" return self.create_activity( name='enter', run=runner.Sync( lambda context, activity: print('entering coffee shop')))
def say_hello(self): """Say hello to everyone.""" return self.create( name='say_hello', retry=2, tasks=runner.Sync( tasks.say_hello.fill(namespace='say_hello')))
def hello_world_one_activity(self): return self.create( name='hello_world_one', tasks=runner.Sync( tasks.print_hello_task.fill( namespace='activity_one_task_one', workflow_id='execution.workflow_id', activity_name=param.StaticParam('Activity 1'), task_name=param.StaticParam('Task 1'))))
def bootstrap(self): """Populate stats table with the fresh queries.""" return self.create( name='bootstrap', tasks=runner.Sync( tasks.bootstrap.fill( namespace='bootstrap', context_date='context_date', reload='reload')))
def set_overall_status(self): """Set an overall status for a day.""" return self.create( name='set_overall_status', tasks=runner.Sync( tasks.set_overall_status.fill( namespace='set_overall_status', feed_name='bootstrap.feed_name', date='bootstrap.date', status_to_set='say_hello.status_to_set')))
def create_leave_coffee_shop(self): """Leave the coffee shop. Returns: ActivityWorker: the activity that leaves the coffee shop. """ @task.decorate() def leave(activity): print('Leaving the coffee shop') return self.create_activity(name='leave', run=runner.Sync(leave.fill()))
def create_get_order_activity(self): """Get order. Returns: ActivityWorker: the activity that gets the order. """ @task.decorate() def payment(activity): print('get order') return self.create_activity(name='get_order', run=runner.Sync(payment.fill()))
def create_payment_activity(self): """Pay the bill. Returns: ActivityWorker: the activity that pays the bills. """ @task.decorate() def payment(activity, total=None): print('pay ${}'.format(total)) return self.create_activity(name='payment', run=runner.Sync( payment.fill(total='total')))
def test_execute_activity(monkeypatch): """Test the execution of an activity. """ monkeypatch.setattr(activity.Activity, '__init__', lambda self: None) monkeypatch.setattr(activity.Activity, 'heartbeat', lambda self: None) resp = dict(task_resp='something') custom_task = MagicMock(return_value=resp) current_activity = activity.Activity() current_activity.runner = runner.Sync(custom_task) val = current_activity.execute_activity(dict(foo='bar')) assert custom_task.called assert val == resp
def create_order_activity(self): """Create an order for an item. Returns: ActivityWorker: the activity that create an order item. """ @task.decorate() def order(activity, item=None): print('ordering: {}'.format(item)) price = 0.00 if item == 'coffee': price = 3.99 if item == 'chocolate_chip_cookie': price = 2.99 return {'price': price} return self.create_activity(name='order', run=runner.Sync(order.fill(item='item')))
def test_execute_activity(monkeypatch, boto_client): """Test the execution of an activity. """ monkeypatch.setattr(activity.ActivityExecution, 'heartbeat', lambda self: None) resp = dict(task_resp='something') custom_task = MagicMock(return_value=resp) current_activity = activity.Activity(boto_client) current_activity.runner = runner.Sync(custom_task) val = current_activity.execute_activity( activity.ActivityExecution(boto_client, 'activityId', 'taskToken', '{"context": "value"}')) assert custom_task.called assert val == resp
def test_synchronous_tasks(monkeypatch): """Test synchronous tasks. """ monkeypatch.setattr(activity.Activity, '__init__', lambda self: None) monkeypatch.setattr(activity.Activity, 'heartbeat', lambda self: None) resp = dict(foo='bar') current_runner = runner.Sync(MagicMock(), MagicMock(return_value=resp)) current_activity = activity.Activity() current_activity.hydrate(dict(runner=current_runner)) result = current_runner.execute(current_activity, EMPTY_CONTEXT) assert len(current_runner.tasks) == 2 for current_task in task.flatten(current_runner.tasks, EMPTY_CONTEXT): assert current_task.called assert resp == result
def test_synchronous_tasks(monkeypatch, boto_client): """Test synchronous tasks. """ monkeypatch.setattr(activity.ActivityExecution, 'heartbeat', lambda self: None) resp = dict(foo='bar') current_runner = runner.Sync( MagicMock(), MagicMock(return_value=resp)) current_activity = activity.ActivityExecution( boto_client, 'activityId', 'taskToken', '{"context": "value"}') result = current_runner.execute(current_activity, EMPTY_CONTEXT) assert len(current_runner.tasks) == 2 for current_task in task.flatten(current_runner.tasks, EMPTY_CONTEXT): assert current_task.called assert resp == result
from __future__ import print_function from garcon import activity from garcon import runner domain = 'dev' name = 'workflow_name' create = activity.create(domain, name) activity_1 = create( name='activity_1', tasks=runner.Sync(lambda activity, context: print('activity_1'))) activity_2 = create(name='activity_2', requires=[activity_1], tasks=runner.Async( lambda activity, context: print('activity_2_task_1'), lambda activity, context: print('activity_2_task_2'))) activity_3 = create( name='activity_3', requires=[activity_1], tasks=runner.Sync(lambda activity, context: print('activity_3'))) activity_4 = create( name='activity_4', requires=[activity_3, activity_2], tasks=runner.Sync(lambda activity, context: print('activity_4'))) def on_exception(actor, exception):
name = 'workflow_sample' create = activity.create(domain, name) def activity_failure(context, activity): num = int(random.random() * 4) if num != 3: logger.warn('activity_3: fails') raise Exception('fails') print('activity_3: end') test_activity_1 = create( name='activity_1', run=runner.Sync(lambda context, activity: logger.debug('activity_1'))) test_activity_2 = create( name='activity_2', requires=[test_activity_1], run=runner.Async( lambda context, activity: logger.debug('activity_2_task_1'), lambda context, activity: logger.debug('activity_2_task_2'))) test_activity_3 = create(name='activity_3', retry=10, requires=[test_activity_1], run=runner.Sync(activity_failure)) test_activity_4 = create( name='activity_4',