-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support a mode to drain pulse queues rather than listen
- Loading branch information
Showing
3 changed files
with
80 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from kombu import Connection, Exchange, Queue | ||
|
||
from fxci_etl.config import Config | ||
from fxci_etl.pulse.handlers.base import handlers | ||
|
||
|
||
def get_connection(config: Config): | ||
pulse = config.pulse | ||
return Connection( | ||
hostname=pulse.host, | ||
port=pulse.port, | ||
userid=pulse.user, | ||
password=pulse.password, | ||
ssl=True, | ||
) | ||
|
||
|
||
def get_consumer(config: Config, connection: Connection, name: str): | ||
pulse = config.pulse | ||
qconf = pulse.queues[name] | ||
exchange = Exchange(qconf.exchange, type="topic") | ||
exchange(connection).declare( | ||
passive=True | ||
) # raise an error if exchange doesn't exist | ||
|
||
queue = Queue( | ||
name=f"queue/{pulse.user}/{name}", | ||
exchange=exchange, | ||
routing_key=qconf.routing_key, | ||
durable=qconf.durable, | ||
exclusive=False, | ||
auto_delete=qconf.auto_delete, | ||
) | ||
|
||
callbacks = [ | ||
cls(config) | ||
for name, cls in handlers.items() | ||
if config.etl.handlers is None or name in config.etl.handlers | ||
] | ||
consumer = connection.Consumer(queue, auto_declare=False, callbacks=callbacks) | ||
consumer.queues[0].queue_declare() | ||
consumer.queues[0].queue_bind() | ||
return consumer | ||
|
||
|
||
def listen(config: Config, name: str): | ||
with get_connection(config) as connection: | ||
with get_consumer(config, connection, name): | ||
while True: | ||
try: | ||
connection.drain_events(timeout=None) | ||
except TimeoutError: | ||
pass | ||
|
||
|
||
def drain_events(config: Config, name: str): | ||
with get_connection(config) as connection: | ||
with get_consumer(config, connection, name): | ||
connection.drain_events(timeout=0) |
This file was deleted.
Oops, something went wrong.