@@ -116,95 +116,86 @@ module Clockwork
116116 every ( import_interval . seconds , 'import_ethscriptions_blocks' ) do
117117 importer = EthBlockImporter . new
118118
119- # Handle Ctrl+C gracefully
120- Signal . trap ( 'INT' ) do
121- puts "\n [#{ Time . now } ] Received INT signal, shutting down..."
122- importer . shutdown
123- exit 130
124- end
125-
126- Signal . trap ( 'TERM' ) do
127- puts "\n [#{ Time . now } ] Received TERM signal, shutting down..."
128- importer . shutdown
129- exit 143
130- end
131-
132119 # Track statistics
133120 total_blocks_imported = 0
134- total_ethscriptions = 0
135121 start_time = Time . now
136122
137- loop do
138- begin
139- initial_block = importer . current_max_eth_block_number
123+ begin
124+ loop do
125+ begin
126+ initial_block = importer . current_max_eth_block_number
140127
141- # Import blocks
142- importer . import_blocks_until_done
128+ # Import blocks
129+ importer . import_blocks_until_done
143130
144- final_block = importer . current_max_eth_block_number
145- blocks_imported = final_block - initial_block
131+ final_block = importer . current_max_eth_block_number
132+ blocks_imported = final_block - initial_block
146133
147- if blocks_imported > 0
148- total_blocks_imported += blocks_imported
134+ if blocks_imported > 0
135+ total_blocks_imported += blocks_imported
149136
150- puts "[#{ Time . now } ] Imported #{ blocks_imported } blocks (#{ initial_block + 1 } to #{ final_block } )"
137+ puts "[#{ Time . now } ] Imported #{ blocks_imported } blocks (#{ initial_block + 1 } to #{ final_block } )"
151138
152- # Show validation summary if enabled
153- if ENV . fetch ( 'VALIDATION_ENABLED' ) . casecmp? ( 'true' )
154- puts importer . validation_summary
155- end
156- else
157- # We're caught up
158- elapsed = ( Time . now - start_time ) . round ( 2 )
139+ # Show validation summary if enabled
140+ if ENV . fetch ( 'VALIDATION_ENABLED' ) . casecmp? ( 'true' )
141+ puts importer . validation_summary
142+ end
143+ else
144+ # We're caught up
145+ elapsed = ( Time . now - start_time ) . round ( 2 )
146+
147+ if total_blocks_imported > 0
148+ puts "[#{ Time . now } ] Session summary: Imported #{ total_blocks_imported } blocks in #{ elapsed } s"
159149
160- if total_blocks_imported > 0
161- puts "[#{ Time . now } ] Session summary: Imported #{ total_blocks_imported } blocks in #{ elapsed } s"
150+ # Reset counters
151+ total_blocks_imported = 0
152+ start_time = Time . now
153+ end
162154
163- # Reset counters
164- total_blocks_imported = 0
165- start_time = Time . now
155+ puts "[#{ Time . now } ] Caught up at block #{ final_block } . Waiting #{ import_interval } s..."
166156 end
167157
168- puts "[#{ Time . now } ] Caught up at block #{ final_block } . Waiting #{ import_interval } s..."
158+ rescue EthBlockImporter ::BlockNotReadyToImportError => e
159+ # This is normal when caught up
160+ current = importer . current_max_eth_block_number
161+ puts "[#{ Time . now } ] Waiting for new blocks (current: #{ current } )..."
162+
163+ rescue EthBlockImporter ::ReorgDetectedError => e
164+ Rails . logger . warn "[#{ Time . now } ] ⚠️ Reorg detected! Reinitializing importer..."
165+ puts "[#{ Time . now } ] ⚠️ Reorg detected at block #{ importer . current_max_eth_block_number } "
166+
167+ # Reinitialize importer to handle reorg
168+ importer . shutdown
169+ importer = EthBlockImporter . new
170+ puts "[#{ Time . now } ] Importer reinitialized. Continuing from block #{ importer . current_max_eth_block_number } "
171+
172+ rescue EthBlockImporter ::ValidationFailureError => e
173+ Rails . logger . fatal "[#{ Time . now } ] 🛑 VALIDATION FAILURE: #{ e . message } "
174+ puts "[#{ Time . now } ] 🛑 VALIDATION FAILURE - System stopping for investigation"
175+ puts "[#{ Time . now } ] Fix the validation issue and restart manually"
176+ exit 1
177+
178+ rescue EthBlockImporter ::ValidationStalledError => e
179+ # Validation is behind - wait longer and keep trying
180+ Rails . logger . info "[#{ Time . now } ] ⏸️ VALIDATION BEHIND: #{ e . message } "
181+ puts "[#{ Time . now } ] ⏸️ Validation is behind - waiting #{ import_interval * 2 } s for validation to catch up..."
182+ sleep import_interval * 2 # Wait longer when validation is behind
183+ # Don't exit - continue the loop to retry
184+
185+ rescue => e
186+ Rails . logger . error "Import error: #{ e . class } - #{ e . message } "
187+ Rails . logger . error e . backtrace . first ( 20 ) . join ( "\n " )
188+
189+ puts "[#{ Time . now } ] ❌ Error: #{ e . message } "
190+
191+ # For other errors, wait and retry
192+ puts "[#{ Time . now } ] Retrying in #{ import_interval } s..."
169193 end
170194
171- rescue EthBlockImporter ::BlockNotReadyToImportError => e
172- # This is normal when caught up
173- current = importer . current_max_eth_block_number
174- puts "[#{ Time . now } ] Waiting for new blocks (current: #{ current } )..."
175-
176- rescue EthBlockImporter ::ReorgDetectedError => e
177- Rails . logger . warn "[#{ Time . now } ] ⚠️ Reorg detected! Reinitializing importer..."
178- puts "[#{ Time . now } ] ⚠️ Reorg detected at block #{ importer . current_max_eth_block_number } "
179-
180- # Reinitialize importer to handle reorg
181- importer = EthBlockImporter . new
182- puts "[#{ Time . now } ] Importer reinitialized. Continuing from block #{ importer . current_max_eth_block_number } "
183-
184- rescue EthBlockImporter ::ValidationFailureError => e
185- Rails . logger . fatal "[#{ Time . now } ] 🛑 VALIDATION FAILURE: #{ e . message } "
186- puts "[#{ Time . now } ] 🛑 VALIDATION FAILURE - System stopping for investigation"
187- puts "[#{ Time . now } ] Fix the validation issue and restart manually"
188- exit 1
189-
190- rescue EthBlockImporter ::ValidationStalledError => e
191- # Validation is behind - wait longer and keep trying
192- Rails . logger . info "[#{ Time . now } ] ⏸️ VALIDATION BEHIND: #{ e . message } "
193- puts "[#{ Time . now } ] ⏸️ Validation is behind - waiting #{ import_interval * 2 } s for validation to catch up..."
194- sleep import_interval * 2 # Wait longer when validation is behind
195- # Don't exit - continue the loop to retry
196-
197- rescue => e
198- Rails . logger . error "Import error: #{ e . class } - #{ e . message } "
199- Rails . logger . error e . backtrace . first ( 20 ) . join ( "\n " )
200-
201- puts "[#{ Time . now } ] ❌ Error: #{ e . message } "
202-
203- # For other errors, wait and retry
204- puts "[#{ Time . now } ] Retrying in #{ import_interval } s..."
195+ sleep import_interval
205196 end
206-
207- sleep import_interval
197+ ensure
198+ importer &. shutdown
208199 end
209200 end
210201end
0 commit comments