44 * - Indexing Status
55 * into the ENSDb.
66 */
7- import config from "@/config" ;
8-
97import { secondsToMilliseconds } from "date-fns" ;
108import pRetry from "p-retry" ;
119
@@ -18,14 +16,11 @@ import {
1816} from "@ensnode/ensnode-sdk" ;
1917
2018import { validateENSIndexerPublicConfigCompatibility } from "@/config/compatibility" ;
21- import { EnsDbConnection , EnsDbMutation , EnsDbQuery } from "@/lib/ensdb" ;
19+ import { EnsDbClient } from "@/lib/ensdb" ;
2220import { ensIndexerClient , waitForEnsIndexerToBecomeHealthy } from "@/lib/ensindexer" ;
23- import { makeLogger } from "@/lib/logger" ;
2421
2522const INDEXING_STATUS_RECORD_UPDATE_INTERVAL : Duration = 1 ;
2623
27- const logger = makeLogger ( "ensdb-writer-worker" ) ;
28-
2924/**
3025 * ENSDb Writer Worker
3126 *
@@ -38,57 +33,45 @@ const logger = makeLogger("ensdb-writer-worker");
3833 * into ENSDb.
3934 */
4035async function ensDbWriterWorker ( ) {
36+ console . log ( "ENSDb Writer Worker: waiting for ENSIndexer to become healthy." ) ;
37+
4138 // 0. Wait for ENSIndexer to become healthy before running the worker's logic
4239 await waitForEnsIndexerToBecomeHealthy ;
4340
44- // 1. Create ENSDb Client
45- const ensDbConnection = new EnsDbConnection ( ) ;
46- const ensDbClient = ensDbConnection . connect ( {
47- schemaName : config . databaseSchemaName ,
48- poolConfig : {
49- connectionString : config . databaseUrl ,
50- } ,
51- } ) ;
52-
53- logger . info ( "ENSDb Client connected" ) ;
41+ console . log ( "ENSDb Writer Worker: ENSIndexer is healthy, starting tasks." ) ;
5442
55- // 2. Create ENSDb Query object for read operations
56- const ensDbQuery = new EnsDbQuery ( ensDbClient ) ;
57- // 3. Create ENSDb Mutation object for write operations
58- const ensDbMutation = new EnsDbMutation ( ensDbClient ) ;
43+ // 1. Create ENSDb Client
44+ const ensDbClient = new EnsDbClient ( ) ;
5945
6046 /**
6147 * Handle ENSIndexerPublicConfig Record
6248 */
6349 const handleEnsIndexerPublicConfigRecord = async ( ) => {
6450 // Read stored config and in-memory config.
65- // Note: we wrap read operations in pRetry to ensure all of them are
51+ // Note: we wrap each operation in pRetry to ensure all of them can be
6652 // completed successfully.
67- const [ storedConfig , inMemoryConfig ] = await pRetry ( ( ) =>
68- Promise . all ( [ ensDbQuery . getEnsIndexerPublicConfig ( ) , ensIndexerClient . config ( ) ] ) ,
69- ) ;
53+ const [ storedConfig , inMemoryConfig ] = await Promise . all ( [
54+ pRetry ( ( ) => ensDbClient . getEnsIndexerPublicConfig ( ) ) ,
55+ pRetry ( ( ) => ensIndexerClient . config ( ) ) ,
56+ ] ) ;
7057
7158 // Validate in-memory config object compatibility with the stored one,
7259 // if the stored one is available
7360 if ( storedConfig ) {
7461 try {
7562 validateENSIndexerPublicConfigCompatibility ( storedConfig , inMemoryConfig ) ;
7663 } catch ( error ) {
77- const errorMessage =
78- "In-memory ENSIndexerPublicConfig object is not compatible with its counterpart stored in ENSDb." ;
79-
80- logger . error ( error , errorMessage ) ;
64+ const errorMessage = `In-memory ENSIndexerPublicConfig object is not compatible with its counterpart stored in ENSDb.` ;
8165
8266 // Throw the error to terminate the ENSIndexer process due to
8367 // found config incompatibility
84- throw new Error ( errorMessage ) ;
68+ throw new Error ( errorMessage , {
69+ cause : error ,
70+ } ) ;
8571 }
8672 } else {
8773 // Upsert ENSIndexerPublicConfig into ENSDb.
88- // Note: we wrap write operation in pRetry to ensure it can complete
89- // successfully, as there will be no other attempt.
90- await pRetry ( ( ) => ensDbMutation . upsertEnsIndexerPublicConfig ( inMemoryConfig ) ) ;
91- logger . info ( "ENSIndexer Public Config successfully stored in ENSDb." ) ;
74+ await ensDbClient . upsertEnsIndexerPublicConfig ( inMemoryConfig ) ;
9275 }
9376 } ;
9477
@@ -110,16 +93,14 @@ async function ensDbWriterWorker() {
11093
11194 // Check if Indexing Status is in expected status.
11295 if ( omnichainSnapshot . omnichainStatus === OmnichainIndexingStatusIds . Unstarted ) {
113- throw new Error ( "Omnichain Status must be different that 'Unstarted'." ) ;
96+ throw new Error ( "Omnichain Status must be different than 'Unstarted'." ) ;
11497 }
11598
11699 // Upsert ENSIndexerPublicConfig into ENSDb.
117- await ensDbMutation . upsertIndexingStatus ( snapshot ) ;
118-
119- logger . info ( "Indexing Status successfully stored in ENSDb." ) ;
100+ await ensDbClient . upsertIndexingStatus ( snapshot ) ;
120101 } catch ( error ) {
121102 // Do nothing about this error, but having it logged.
122- logger . error ( error , "Could not upsert Indexing Status record" ) ;
103+ console . error ( error , "Could not upsert Indexing Status record" ) ;
123104 } finally {
124105 // Regardless of current iteration result,
125106 // schedule the next callback to handle Indexing Status Record.
@@ -131,12 +112,20 @@ async function ensDbWriterWorker() {
131112 } ;
132113
133114 // 4. Handle ENSIndexer Public Config just once.
134- await handleEnsIndexerPublicConfigRecord ( ) ;
115+ console . log ( "Task: store ENSIndexer Public Config in ENSDb." ) ;
116+ await handleEnsIndexerPublicConfigRecord ( ) . then ( ( ) =>
117+ console . log ( "ENSIndexer Public Config successfully stored in ENSDb." ) ,
118+ ) ;
135119
136120 // 5. Handle Indexing Status on recurring basis.
137- await handleIndexingStatusRecordRecursively ( ) ;
121+ console . log ( "Task: store Indexing Status in ENSDb." ) ;
122+ await handleIndexingStatusRecordRecursively ( ) . then ( ( ) =>
123+ console . log ( "Indexing Status successfully stored in ENSDb." ) ,
124+ ) ;
138125}
139126
140127// Run ENSDb Writer Worker in a non-blocking way to
141128// allow database migrations to proceed in the background.
142- setTimeout ( ensDbWriterWorker , 0 ) ;
129+ ensDbWriterWorker ( ) . catch ( ( error ) =>
130+ console . error ( "ENSDb Writer Worker failed to perform its tasks" , error ) ,
131+ ) ;
0 commit comments