def timer_tasks(): import asyncio import uvloop from mode import Worker from app.services.tasks_scheduler.timer_tasks.app.base import app asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) loop = asyncio.get_event_loop() base_config = BaseConfig().config log_level = base_config['LOG_LEVEL'] worker = Worker(app, loglevel=log_level, loop=loop) worker.execute_from_commandline()
def test_json_sensor(): from mode import Worker supervisor = OneForOneSupervisor() connection = JsonSensor(\ device = '/dev/ttyACM0', baudrate = 9600, timeout = 10 ) class DataReader(Service): @Service.timer(5.0) async def interfere(self): await connection.crash( RuntimeError('Something went wrong... Hehehe...')) self.log.info('poll') async def handle_data(self, sender, data={}, **kwds): # self.log.info('Got data: ' + repr(data)) self.log.info('Got data: sender:' + repr(sender) + ' data: ' + repr(data)) data_reader = DataReader() connection.on_data.connect(data_reader.handle_data) supervisor.add(connection) supervisor.add(data_reader) # Worker(supervisor, loglevel="debug").execute_from_commandline() Worker(supervisor, loglevel="info").execute_from_commandline()
def test_json_sensor_server(): from mode import Worker supervisor = OneForOneSupervisor() json_sensor_server = JsonSensorServer(\ port_greps = ['Leonardo'] ) class DataReader(Service): # @Service.timer(5.0) # async def interfere(self): # await json_sensor_server.crash(RuntimeError('Something went wrong... Hehehe...')) # self.log.info('poll') async def handle_data(self, sender, data={}, aggregated_data={}, **kwds): self.log.info( f'DataReader: Aggregated data is now: {repr(aggregated_data)}') data_reader = DataReader() json_sensor_server.on_data_update.connect(data_reader.handle_data) supervisor.add(json_sensor_server) supervisor.add(data_reader) # Worker(supervisor, loglevel="debug").execute_from_commandline() Worker(supervisor, loglevel="debug").execute_from_commandline()
def run(ctx, debug): loglevel = "info" if debug: loglevel = "debug" from mode import Worker logging.getLogger("aio_pika").setLevel(logging.WARNING) logging.getLogger("asyncio").setLevel(logging.INFO) worker = Worker( PingAgent(identity="PingAgent"), loglevel=loglevel, logfile=None, daemon=True, redirect_stdouts=False, ) worker.execute_from_commandline()
def test_worker_for_service(self, *, command): with patch('faust.cli.base.Worker') as Worker: service = Mock(name='service') loop = Mock(name='loop') res = command.worker_for_service(service, loop) assert res is Worker.return_value Worker.assert_called_once_with( service, debug=command.debug, quiet=command.quiet, stdout=command.stdout, stderr=command.stderr, loglevel=command.loglevel, logfile=command.logfile, blocking_timeout=command.blocking_timeout, console_port=command.console_port, redirect_stdouts=command.redirect_stdouts, redirect_stdouts_level=command.redirect_stdouts_level, loop=loop, daemon=command.daemon, )
def test_usb_serial_server(): from mode import Worker supervisor = OneForOneSupervisor() serial_server = USBSerialServer(\ port_greps = ['Leonardo'] ) supervisor.add(serial_server) # Worker(supervisor, loglevel="debug").execute_from_commandline() Worker(supervisor, loglevel="debug").execute_from_commandline()
def test_robust_serial(): from mode import Worker supervisor = OneForOneSupervisor() connection = RobustSerialService(\ device = '/dev/ttyACM0', baudrate = 9600, timeout = 10, protocol_factory = JanusLineReader ) supervisor.add(connection) Worker(supervisor, loglevel="debug").execute_from_commandline()
def test_json_sensor_server(): from mode import Worker supervisor = OneForOneSupervisor() json_sensor_server = JsonSensorServer(\ port_greps = ['Arduino', 'Trinket'] ) class DataReader(Service): async def handle_data(self, sender, data={}, aggregated_data={}, **kwds): self.log.info( f'DataReader: Aggregated data is now: {repr(aggregated_data)}') @Service.task async def read_adc1(self): while not self.should_stop: data = await self.adc_queue1.get() self.log.info(f'DataReader: New ADC1 value: {data}') @Service.task async def read_adc2(self): while not self.should_stop: data = await self.adc_queue2.get() self.log.info(f'DataReader: New ADC2 value: {data}') data_reader = DataReader() data_reader.adc_queue1 = json_sensor_server.create_subscriber_queue( 'btrn-adc-sensor-0002', 'adc_ch01') data_reader.adc_queue2 = json_sensor_server.create_subscriber_queue( 'btrn-adc-sensor-0002', 'adc_ch23') json_sensor_server.on_data_update.connect(data_reader.handle_data) supervisor.add(json_sensor_server) supervisor.add(data_reader) # Worker(supervisor, loglevel="debug").execute_from_commandline() Worker(supervisor, loglevel="info").execute_from_commandline()
if self.web and self.web.ws: self.log.debug(f"Publishing ws message: {msg}") try: await self.web.ws.send_json(msg) except RuntimeError as e: self.log.exception(e) def __repr__(self): return "{}".format(self.__class__.__name__) @property def status(self): behav_stati = list() for behav in self.behaviours: behav_status = ServiceStatus(name=str(behav), state=behav.state) behav_stati.append(behav_status) return CoreStatus(name=self.identity, state=self.state, behaviours=behav_stati) if __name__ == "__main__": logging.getLogger("aio_pika").setLevel(logging.INFO) logging.getLogger("asyncio").setLevel(logging.INFO) setup_logging(logging.DEBUG) from mode import Worker config = dict(UPDATE_PEER_INTERVAL=1.0) app = Core(identity="core", config=config) Worker(app, loglevel="info").execute_from_commandline()
# -- ] def iterate_over_scheduled_time(self): for period in cycle(self.schedule): for _ in range(period.length): yield period @Service.task async def _runs_in_background(self): self.log.info("I'm your local friendly chaos monkey tester") self.log.info("Starting signal dispatcher.") for current_span in self.iterate_over_scheduled_time(): if self.should_stop: return secs = current_span.seconds_to_sleep() self.log.info("Signal dispatcher sleeping for %r seconds...", secs) await self.sleep(secs) sig = random.choice(current_span.signals) self.log.warning("Signalling all workers on this box with %r", sig) r = envoy.run(f"pkill -{int(sig)} Faust:Worker") if r.status_code: if r.std_err.strip(): self.log.error("ERROR from pkill: %r", r.std_err) else: self.log.info("No processes running, nothing to signal!") if __name__ == "__main__": Worker(Chaos(), loglevel="INFO").execute_from_commandline()
def _init_worker(app_name): loop = asyncio.get_event_loop() app = symbol_by_name(app_name, imp=import_from_cwd) worker = Worker(app, loglevel="info", loop=loop) worker.execute_from_commandline()
sys.path.insert(0, str(Path(__file__).parent / "munggoggo")) from behaviour import Behaviour from core import Core logging.getLogger("aio_pika").setLevel(logging.WARNING) logging.getLogger("asyncio").setLevel(logging.INFO) class Agent(Core): class PingBehav(Behaviour): async def setup(self): self.counter = 0 async def run(self): self.counter += 1 msg = await self.receive() if msg: print(f"{self.name}: Message received: {msg.body.decode()}") await self.publish(str(self.counter), "ping") await asyncio.sleep(0.9) async def setup(self) -> None: """ Register behaviour and subscribe to 'ping' topic """ await self.add_runtime_dependency( self.PingBehav(self, binding_keys=["ping"])) if __name__ == "__main__": Worker(Agent(identity="Agent"), loglevel="info").execute_from_commandline()
print(f"{self.name}: Message: {msg.body.decode()}") async def teardown(self): print(f"Finished {self.name} . . .") class Agent2(Core): @property def behaviour(self) -> Behaviour: return SubscribeBehav(self, binding_keys=["ping"]) async def setup(self) -> None: await self.add_runtime_dependency(self.behaviour) if __name__ == "__main__": from mode import Worker logging.getLogger("aio_pika").setLevel(logging.WARNING) logging.getLogger("asyncio").setLevel(logging.INFO) worker = Worker( Agent2(identity="agent2"), loglevel="info", logfile=None, daemon=True, redirect_stdouts=False, ) worker.execute_from_commandline()
async def on_first_start(self) -> None: self.log.info('Starting! Making a bernd!') self.sad_bernd = self.add_dependency(SadPoller('bernd')) self.supervisor = self.Supervisor() self.supervisor.add(self.sad_bernd) async def on_start(self) -> None: self.log.info('Starting Poller!!!') @Service.timer(8.0) async def no_key(self) -> None: await self.crash(KeyError(f'I cant find the key!!!')) # async def on_init_dependencies(self): # return [SadPoller('bernd')] class SimplePollerSupervisor(OneForOneSupervisor): ... if __name__ == '__main__': from mode import Worker supervisor = OneForOneSupervisor() poller = SimplePoller() supervisor.add(poller) Worker(supervisor, loglevel="info").execute_from_commandline()
def main(): configure_logging() logger.info("Micro API version %s", micro_api.__version__) Worker(MicroService(), loglevel="info").execute_from_commandline()
import sys sys.path.append('.') sys.path.append('..') from json_sensor import * from mode import Worker async def handle_data(sender, data={}, aggregated_data={}, **kwds): print(f'Aggregated data is now: {repr(aggregated_data)}') json_sensor_server = JsonSensorServer( port_greps=['Arduino', 'Adafruit_Industries_Trinket', 'Trinket']) json_sensor_server.on_data_update.connect(handle_data) Worker(json_sensor_server, loglevel="debug").execute_from_commandline() #Worker(json_sensor_server, loglevel="info").execute_from_commandline()