generated from TogetherCrew/python-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord_utils.py
114 lines (85 loc) · 3.33 KB
/
discord_utils.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import logging
from analyzer_init import AnalyzerInit
from automation.automation_workflow import AutomationWorkflow
from tc_messageBroker.rabbit_mq.saga.saga_base import get_saga
from utils.credentials import get_mongo_credentials
from utils.get_guild_utils import get_guild_community_ids
from utils.rabbitmq import RabbitMQSingleton
from utils.transactions_ordering import sort_transactions
def analyzer_recompute(sagaId: str):
saga = get_saga_instance(sagaId=sagaId)
if saga is None:
logging.warn(
f"Warn: Saga not found!, stopping the recompute for sagaId: {sagaId}"
)
else:
platform_id = saga.data["platformId"]
guildId = get_guild_community_ids(platform_id)
logging.info("Initializing the analyzer")
analyzer_init = AnalyzerInit(guildId)
analyzer = analyzer_init.get_analyzer()
logging.info("Analyzer initialized")
def recompute_wrapper(**kwargs):
logging.info("recompute wrapper")
analyzer.recompute_analytics()
def publish_wrapper(**kwargs):
pass
logging.info("Calling the saga.next()")
saga.next(
publish_method=publish_wrapper,
call_function=recompute_wrapper,
)
return sagaId
def analyzer_run_once(sagaId: str):
saga = get_saga_instance(sagaId=sagaId)
if saga is None:
logging.warn(f"Saga not found!, stopping the run_once for sagaId: {sagaId}")
else:
platform_id = saga.data["platformId"]
guildId = get_guild_community_ids(platform_id)
analyzer_init = AnalyzerInit(guildId)
analyzer = analyzer_init.get_analyzer()
def run_once_wrapper(**kwargs):
analyzer.run_once()
def publish_wrapper(**kwargs):
pass
saga.next(
publish_method=publish_wrapper,
call_function=run_once_wrapper,
)
return sagaId
def get_saga_instance(sagaId: str):
mongo_creds = get_mongo_credentials()
saga = get_saga(
sagaId=sagaId,
connection_url=mongo_creds["connection_str"],
db_name="Saga",
collection="sagas",
)
if saga is None:
raise ValueError(f"Saga with sagaId: {sagaId} not found!")
return saga
def publish_on_success(connection, result, *args, **kwargs):
try:
sagaId = args[0]
logging.info(f"SAGAID: {sagaId}: ON_SUCCESS callback!")
saga = get_saga_instance(sagaId=sagaId)
rabbitmq = RabbitMQSingleton.get_instance().get_client()
transactions = saga.choreography.transactions
(transactions_ordered, tx_not_started_count) = sort_transactions(transactions)
platform_id = saga.data["platformId"]
guildId = get_guild_community_ids(platform_id)
msg = f"GUILDID: {guildId}: "
if tx_not_started_count != 0:
tx = transactions_ordered[0]
logging.info(f"{msg}Publishing for {tx.queue}")
rabbitmq.connect(tx.queue)
rabbitmq.publish(
queue_name=tx.queue,
event=tx.event,
content={"uuid": sagaId, "data": saga.data},
)
automation_workflow = AutomationWorkflow()
automation_workflow.start(guild_id=guildId)
except Exception as exp:
logging.info(f"Exception occured in job on_success callback: {exp}")