-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprocessing.service.ts
More file actions
167 lines (147 loc) · 5.96 KB
/
processing.service.ts
File metadata and controls
167 lines (147 loc) · 5.96 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
import { optimism } 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 { 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 { CHAINS: chains } = env;
const {
core,
registriesRepositories,
indexerClient,
kyselyDatabase,
logger,
retryStrategy,
} = sharedDependencies;
const {
eventRegistryRepository,
strategyRegistryRepository,
strategyProcessingCheckpointRepository,
} = registriesRepositories;
const orchestrators: Map<ChainId, [Orchestrator, RetroactiveProcessor]> = new Map();
const strategyRegistry = new DatabaseStrategyRegistry(logger, strategyRegistryRepository);
const eventsRegistry = new DatabaseEventRegistry(logger, eventRegistryRepository);
for (const chain of chains) {
// Initialize EVM provider
const evmProvider = new EvmProvider(chain.rpcUrls, optimism, logger);
const cachedStrategyRegistry = await InMemoryCachedStrategyRegistry.initialize(
logger,
strategyRegistry,
chain.id as ChainId,
);
const orchestrator = new Orchestrator(
chain.id as ChainId,
{ ...core, evmProvider },
indexerClient,
{
eventsRegistry,
strategyRegistry: cachedStrategyRegistry,
},
chain.fetchLimit,
chain.fetchDelayMs,
retryStrategy,
logger,
);
const retroactiveProcessor = new RetroactiveProcessor(
chain.id as ChainId,
{ ...core, evmProvider },
indexerClient,
{
eventsRegistry,
strategyRegistry: cachedStrategyRegistry,
checkpointRepository: strategyProcessingCheckpointRepository,
},
chain.fetchLimit,
retryStrategy,
logger,
);
orchestrators.set(chain.id as ChainId, [orchestrator, retroactiveProcessor]);
}
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();
const orchestratorProcesses: Promise<void>[] = [];
// Handle graceful shutdown
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();
});
try {
for (const [orchestrator, _] of this.orchestrators.values()) {
this.logger.info(`Starting orchestrator for chain ${orchestrator.chainId}...`);
orchestratorProcesses.push(orchestrator.run(abortController.signal));
}
await Promise.allSettled(orchestratorProcesses);
} 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()) {
await retroactiveProcessor.processRetroactiveStrategies();
}
}
/**
* 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();
} catch (error) {
this.logger.error(`Error releasing resources: ${error}`);
}
}
}