-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathprocessing.service.ts
More file actions
208 lines (182 loc) · 7.93 KB
/
processing.service.ts
File metadata and controls
208 lines (182 loc) · 7.93 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { Chain, extractChain } from "viem";
import * as viemChains from "viem/chains";
import { EvmProvider } from "@grants-stack-indexer/chain-providers";
import {
DatabaseEventRegistry,
DatabaseStrategyRegistry,
InMemoryCachedStrategyRegistry,
Orchestrator,
RetroactiveProcessor,
} from "@grants-stack-indexer/data-flow";
import { ChainId, ILogger } from "@grants-stack-indexer/shared";
import { Environment } from "../config/env.js";
import { InvalidChainId } from "../exceptions/index.js";
import { SharedDependencies, SharedDependenciesService } from "./index.js";
/**
* Processor service application
* - Initializes core dependencies (repositories, providers) via SharedDependenciesService
* - Initializes a StrategyRegistry and loads it with strategies from the database
* For each chain:
* - Sets up EVM provider with configured RPC endpoints
* - Instantiates an EventsRegistry and loads it with the last processed event for the chain
* - Creates an Orchestrator instance to coordinate an specific chain:
* - Fetching on-chain events via indexer client
* - Processing events through registered handlers
* - Storing processed data in PostgreSQL via repositories
* - Manages graceful shutdown on termination signals
*/
export class ProcessingService {
private readonly orchestrators: Map<ChainId, [Orchestrator, RetroactiveProcessor]> = new Map();
private readonly logger: ILogger;
private readonly kyselyDatabase: SharedDependencies["kyselyDatabase"];
private constructor(
orchestrators: Map<ChainId, [Orchestrator, RetroactiveProcessor]>,
kyselyDatabase: SharedDependencies["kyselyDatabase"],
logger: ILogger,
) {
this.orchestrators = orchestrators;
this.kyselyDatabase = kyselyDatabase;
this.logger = logger;
}
static async initialize(env: Environment): Promise<ProcessingService> {
const sharedDependencies = await SharedDependenciesService.initialize(env);
const { logger } = sharedDependencies;
logger.debug("Shared dependencies initialized");
const { CHAINS: chains } = env;
const {
core,
registriesRepositories,
indexerClient,
kyselyDatabase,
retryStrategy,
notifier,
} = sharedDependencies;
const {
eventRegistryRepository,
strategyRegistryRepository,
strategyProcessingCheckpointRepository,
} = registriesRepositories;
const orchestrators: Map<ChainId, [Orchestrator, RetroactiveProcessor]> = new Map();
logger.debug("Starting chain initialization", { chainCount: chains.length });
const strategyRegistry = new DatabaseStrategyRegistry(logger, strategyRegistryRepository);
const eventsRegistry = new DatabaseEventRegistry(logger, eventRegistryRepository);
logger.debug("Created base registries");
const viemChainsArray = Object.values(viemChains) as Chain[];
for (const chain of chains) {
logger.debug("Processing chain configuration", { chainId: chain.id });
const viemChain = extractChain({
chains: viemChainsArray,
id: chain.id,
});
if (!viemChain) {
logger.error("Invalid chain configuration", { chainId: chain.id });
throw new InvalidChainId(chain.id);
}
const evmProvider = new EvmProvider(chain.rpcUrls, viemChain, logger);
logger.debug("EVM provider created", { chainId: chain.id });
const cachedStrategyRegistry = await InMemoryCachedStrategyRegistry.initialize(
logger,
strategyRegistry,
chain.id as ChainId,
);
logger.debug("Cached strategy registry initialized", { chainId: chain.id });
const orchestrator = new Orchestrator(
chain.id as ChainId,
{ ...core, evmProvider },
indexerClient,
{
eventsRegistry,
strategyRegistry: cachedStrategyRegistry,
},
chain.fetchLimit,
chain.fetchDelayMs,
retryStrategy,
logger,
notifier,
);
logger.debug("Orchestrator created", { chainId: chain.id });
const retroactiveProcessor = new RetroactiveProcessor(
chain.id as ChainId,
{ ...core, evmProvider },
indexerClient,
{
eventsRegistry,
strategyRegistry: cachedStrategyRegistry,
checkpointRepository: strategyProcessingCheckpointRepository,
},
chain.fetchLimit,
retryStrategy,
logger,
);
logger.debug("Retroactive processor created", { chainId: chain.id });
orchestrators.set(chain.id as ChainId, [orchestrator, retroactiveProcessor]);
logger.debug("Chain setup completed", { chainId: chain.id });
}
logger.debug("All chains initialized", { chainCount: orchestrators.size });
return new ProcessingService(orchestrators, kyselyDatabase, logger);
}
/**
* Start the processor service
*
* The processor runs indefinitely until it is terminated.
*/
async start(): Promise<void> {
this.logger.info("Starting processor service...");
const abortController = new AbortController();
this.logger.debug("Created abort controller");
const orchestratorProcesses: Promise<void>[] = [];
process.on("SIGINT", () => {
this.logger.info("Received SIGINT signal. Shutting down...");
abortController.abort();
});
process.on("SIGTERM", () => {
this.logger.info("Received SIGTERM signal. Shutting down...");
abortController.abort();
});
this.logger.debug("Signal handlers registered");
try {
for (const [orchestrator, _] of this.orchestrators.values()) {
this.logger.info(`Starting orchestrator for chain ${orchestrator.chainId}...`);
orchestratorProcesses.push(orchestrator.run(abortController.signal));
this.logger.debug("Orchestrator process queued", { chainId: orchestrator.chainId });
}
this.logger.debug("Waiting for all orchestrator processes", {
processCount: orchestratorProcesses.length,
});
await Promise.allSettled(orchestratorProcesses);
this.logger.debug("All orchestrator processes completed");
} catch (error) {
this.logger.error(`Processor service failed: ${error}`);
throw error;
}
}
/**
* Process retroactive events for all chains
* - This is a blocking operation that will run until all retroactive events are processed
*/
async processRetroactiveEvents(): Promise<void> {
this.logger.info("Processing retroactive events...");
for (const [_, retroactiveProcessor] of this.orchestrators.values()) {
this.logger.debug("Starting retroactive processing", {
chainId: retroactiveProcessor.chainId,
});
await retroactiveProcessor.processRetroactiveStrategies();
this.logger.debug("Completed retroactive processing", {
chainId: retroactiveProcessor.chainId,
});
}
}
/**
* Call this function when the processor service is terminated
* - Releases database resources
*/
async releaseResources(): Promise<void> {
try {
this.logger.info("Releasing resources...");
await this.kyselyDatabase.destroy();
this.logger.debug("Database resources released");
} catch (error) {
this.logger.error(`Error releasing resources: ${error}`);
}
}
}