Add graceful shutdown
This commit is contained in:
parent
d837602394
commit
a4ad3feb2f
@ -2,12 +2,14 @@
|
|||||||
import uuid
|
import uuid
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
import signal
|
||||||
|
|
||||||
# external imports
|
# external imports
|
||||||
from chainlib.eth.block import (
|
from chainlib.eth.block import (
|
||||||
block_by_number,
|
block_by_number,
|
||||||
Block,
|
Block,
|
||||||
)
|
)
|
||||||
|
from chainlib.eth.tx import receipt
|
||||||
|
|
||||||
# local imports
|
# local imports
|
||||||
from chainsyncer.filter import SyncFilter
|
from chainsyncer.filter import SyncFilter
|
||||||
@ -23,6 +25,7 @@ class Syncer:
|
|||||||
|
|
||||||
running_global = True
|
running_global = True
|
||||||
yield_delay=0.005
|
yield_delay=0.005
|
||||||
|
signal_set = False
|
||||||
|
|
||||||
def __init__(self, backend, loop_callback=noop_callback, progress_callback=noop_callback):
|
def __init__(self, backend, loop_callback=noop_callback, progress_callback=noop_callback):
|
||||||
self.cursor = None
|
self.cursor = None
|
||||||
@ -31,6 +34,22 @@ class Syncer:
|
|||||||
self.filter = SyncFilter(backend)
|
self.filter = SyncFilter(backend)
|
||||||
self.progress_callback = progress_callback
|
self.progress_callback = progress_callback
|
||||||
self.loop_callback = loop_callback
|
self.loop_callback = loop_callback
|
||||||
|
if not Syncer.signal_set:
|
||||||
|
signal.signal(signal.SIGINT, Syncer.__sig_terminate)
|
||||||
|
signal.signal(signal.SIGTERM, Syncer.__sig_terminate)
|
||||||
|
Syncer.signal_set = True
|
||||||
|
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def __sig_terminate(sig, frame):
|
||||||
|
logg.warning('got signal {}'.format(sig))
|
||||||
|
Syncer.terminate()
|
||||||
|
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def terminate():
|
||||||
|
logg.info('termination requested!')
|
||||||
|
Syncer.running_global = False
|
||||||
|
|
||||||
|
|
||||||
def chain(self):
|
def chain(self):
|
||||||
@ -61,7 +80,7 @@ class BlockPollSyncer(Syncer):
|
|||||||
while self.running and Syncer.running_global:
|
while self.running and Syncer.running_global:
|
||||||
if self.loop_callback != None:
|
if self.loop_callback != None:
|
||||||
self.loop_callback(last_block, last_tx)
|
self.loop_callback(last_block, last_tx)
|
||||||
while True:
|
while True and Syncer.running_global:
|
||||||
try:
|
try:
|
||||||
block = self.get(conn)
|
block = self.get(conn)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -89,6 +108,8 @@ class HeadSyncer(BlockPollSyncer):
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
tx = block.tx(i)
|
tx = block.tx(i)
|
||||||
|
rcpt = conn.do(receipt(tx.hash))
|
||||||
|
tx.apply_receipt(rcpt)
|
||||||
self.progress_callback(block.number, i, 'processing {}'.format(repr(tx)))
|
self.progress_callback(block.number, i, 'processing {}'.format(repr(tx)))
|
||||||
self.backend.set(block.number, i)
|
self.backend.set(block.number, i)
|
||||||
self.filter.apply(conn, block, tx)
|
self.filter.apply(conn, block, tx)
|
||||||
|
@ -39,6 +39,7 @@ class SyncFilter:
|
|||||||
logg.debug('applying filter {}'.format(str(f)))
|
logg.debug('applying filter {}'.format(str(f)))
|
||||||
f.filter(conn, block, tx, self.backend.db_session)
|
f.filter(conn, block, tx, self.backend.db_session)
|
||||||
self.backend.complete_filter(i)
|
self.backend.complete_filter(i)
|
||||||
|
if session != None:
|
||||||
self.backend.disconnect()
|
self.backend.disconnect()
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user