-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventhandler.py
More file actions
executable file
·55 lines (41 loc) · 1.46 KB
/
eventhandler.py
File metadata and controls
executable file
·55 lines (41 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
import time
import os
import django
import asyncio
import functools
import signal
import importlib
import inspect
from django.conf import settings
from django.apps import apps
os.environ['DJANGO_SETTINGS_MODULE'] = 'meanwise_backend.settings'
django.setup()
from meanwise_backend.eventsourced import EventStoreClient, EventBus, Command
import logging
logger = logging.getLogger('meanwise_backend.%s' % __name__)
logger.setLevel(logging.DEBUG)
loop = asyncio.get_event_loop()
eventstore_client = EventStoreClient(settings.EVENTSTORE_HOST)
Command.set_eventstore_client(eventstore_client)
eventbus = EventBus(eventstore_client)
EventBus.singleton = eventbus
def ask_exit(signame):
logger.info("Got signal %s: exit" % signame)
loop.stop()
for signame in ('SIGINT', 'SIGTERM'):
loop.add_signal_handler(getattr(signal, signame),
functools.partial(ask_exit, signame))
for app_conf in apps.get_app_configs():
spec = importlib.util.find_spec('%s.eventhandlers' % app_conf.name)
if spec is not None:
mod = importlib.import_module('%s.eventhandlers' % app_conf.name)
logger.info("Loaded %s.eventhandlers" % app_conf.name)
logger.info("Eventhandler running, press Ctrl+C to interrupt.")
logger.info("pid %s: send SIGINT or SIGTERM to exit." % os.getpid())
logger.info("Will start processing events in 3 seconds")
time.sleep(3)
try:
loop.run_until_complete(eventbus.run(loop))
finally:
loop.close()