|
| 1 | +# mypy: ignore-errors |
| 2 | +import asyncio |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import sys |
| 6 | + |
| 7 | +from microagent import MicroAgent, consumer, load_stuff, on, periodic |
| 8 | +from microagent.tools import amqp, redis |
| 9 | + |
| 10 | +logging.basicConfig(format=( |
| 11 | + '%(levelname)-8s [pid#%(process)d] %(asctime)s %(name)s ' |
| 12 | + '%(filename)s:%(lineno)d %(message)s' |
| 13 | +), stream=sys.stdout, level=logging.INFO) |
| 14 | + |
| 15 | +cur_dir = os.path.dirname(os.path.realpath(__file__)) |
| 16 | +signals, queues = load_stuff('file://' + os.path.join(cur_dir, 'signals.json')) |
| 17 | + |
| 18 | + |
| 19 | +class UserAgent(MicroAgent): |
| 20 | + @on('pre_start') |
| 21 | + def setup(self): |
| 22 | + self.log.info('Run ...\n %s', self.info()) |
| 23 | + |
| 24 | + @periodic(period=15, timeout=10, start_after=3) |
| 25 | + async def example_periodic_task_send_push(self): |
| 26 | + self.log.info('Run periodic task') |
| 27 | + await self.broker.push3.send({'text': 'informer text'}, topic='msg.ios') |
| 28 | + await self.broker.push.send({'text': 'informer 1'}) |
| 29 | + |
| 30 | + @consumer(queues.android) |
| 31 | + async def example_read_queue_android(self, **kwargs): |
| 32 | + self.log.info('Catch android %s', kwargs) |
| 33 | + |
| 34 | + @consumer(queues.ios) |
| 35 | + async def example_read_queue_ios(self, **kwargs): |
| 36 | + self.log.info('Catch ios %s', kwargs) |
| 37 | + |
| 38 | + @consumer(queues.android_a) |
| 39 | + async def example_read_queue_android_a(self, **kwargs): |
| 40 | + self.log.info('Catch android_a %s', kwargs) |
| 41 | + |
| 42 | + @consumer(queues.ios_a) |
| 43 | + async def example_read_queue_ios_a(self, **kwargs): |
| 44 | + self.log.info('Catch ios_a %s', kwargs) |
| 45 | + |
| 46 | + |
| 47 | +async def main(): |
| 48 | + bus = redis.RedisSignalBus('redis://localhost/7') |
| 49 | + broker = amqp.AMQPBroker('amqp://guest:guest@localhost:5672/') |
| 50 | + await broker.push3.send({'q':1}, topic='msg.android') |
| 51 | + |
| 52 | + agent = UserAgent(bus=bus, broker=broker) |
| 53 | + await agent.start() |
| 54 | + |
| 55 | + while True: |
| 56 | + await asyncio.sleep(1) |
| 57 | + |
| 58 | + |
| 59 | +asyncio.run(main()) |
0 commit comments