def test_oneforone_handle_failure_restart_directive_can_restart(supervisor_data): supervisor_data['local_process'].send_system_message = Mock() supervisor_data['supervisor'].restart_children = Mock() exc = Exception() decider = lambda pid, cause: SupervisorDirective.Restart one_for_one = OneForOneStrategy(decider, 10, timedelta(seconds=20)) one_for_one.handle_failure(supervisor_data['supervisor'], supervisor_data['pid_child'], supervisor_data['restart_statistic'], exc) supervisor_data['supervisor'].restart_children\ .assert_called_once_with(exc, supervisor_data['pid_child'])
def test_oneforone_handle_failure_restart_directive_cant_restart(supervisor_data): supervisor_data['supervisor'].stop_children = Mock() supervisor_data['restart_statistic'].number_of_failures = Mock(return_value=15) exc = Exception() decider = lambda pid, cause: SupervisorDirective.Restart one_for_one = OneForOneStrategy(decider, 10, timedelta(seconds=20)) one_for_one.request_restart_permission = Mock(return_value=False) one_for_one.handle_failure(supervisor_data['supervisor'], supervisor_data['pid_child'], supervisor_data['restart_statistic'], exc) supervisor_data['supervisor'].stop_children\ .assert_called_once_with(supervisor_data['pid_child'])
def create_transfer(self, actor_name: str, from_account: PID, to_account: PID, amount: float, persistence_id: str) -> PID: transfer_props = Props.from_producer(lambda: TransferProcess( from_account, to_account, amount, self._provider, persistence_id, self._availability)).with_child_supervisor_strategy( OneForOneStrategy( lambda pid, reason: SupervisorDirective.Restart, self._retry_attempts, None)) transfer = self._context.spawn_named(transfer_props, actor_name) return transfer
async def test_oneforone_handle_failure_escalate_directive(supervisor_data): supervisor_data['local_process'].send_system_message = AsyncMock() supervisor_data['supervisor'].escalate_failure = AsyncMock() exc = Exception() decider = lambda pid, cause: SupervisorDirective.Escalate one_for_one = OneForOneStrategy(decider, 10, timedelta(seconds=20)) await one_for_one.handle_failure(supervisor_data['supervisor'], supervisor_data['pid_child'], supervisor_data['restart_statistic'], exc, None) supervisor_data['supervisor'].escalate_failure \ .assert_called_once_with(exc, None)
async def main(): context = RootContext() logging.basicConfig( format='%(name)s - %(levelname)s - %(message)s %(stack_info)s', level=logging.DEBUG, handlers=[logging.StreamHandler(sys.stdout)]) props = Props.from_producer( lambda: ParentActor()).with_child_supervisor_strategy( OneForOneStrategy(Decider.decide, 1, None)) actor = context.spawn(props) await context.send(actor, Hello('Alex')) await context.send(actor, Recoverable()) await context.send(actor, Fatal()) await asyncio.sleep(1) await context.stop(actor) input()
async def main(): context = RootContext() number_of_transfers = 5 interval_between_console_updates = 1 uptime = 99.99 retry_attempts = 0 refusal_probability = 0.01 busy_probability = 0.01 verbose = False props = Props.from_producer(lambda: Runner( number_of_transfers, interval_between_console_updates, uptime, refusal_probability, busy_probability, retry_attempts, verbose )).with_child_supervisor_strategy( OneForOneStrategy(lambda pid, reason: SupervisorDirective.Restart, retry_attempts, None)) print('Spawning runner') context.spawn_named(props, 'runner') input()
async def main(): async def child_fn(context: AbstractContext): print(f'{context.my_self.id}: MSG: {type(context.message)}') if isinstance(context.message, Started): raise Exception('child failure') child_props = Props.from_func(child_fn) async def root_fn(context: AbstractContext): print(f'{context.my_self.id}: MSG: {type(context.message)}') if isinstance(context.message, Started): context.spawn_named(child_props, 'child') elif isinstance(context.message, Terminated): print(f'Terminated {context.message.who}') root_props = Props.from_func(root_fn).with_child_supervisor_strategy( OneForOneStrategy(lambda pid, reason: SupervisorDirective.Escalate, 0, None)) root_context = RootContext() root_context.spawn_named(root_props, 'root') input()