Skip to content

Commit d5d7214

Browse files
committed
Auto restart db_replicator
1 parent 624c750 commit d5d7214

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

mysql_ch_replicator/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class Settings:
9191
DEFAULT_LOG_LEVEL = 'info'
9292
DEFAULT_OPTIMIZE_INTERVAL = 86400
9393
DEFAULT_CHECK_DB_UPDATED_INTERVAL = 120
94+
DEFAULT_AUTO_RESTART_INTERVAL = 3600
9495

9596
def __init__(self):
9697
self.mysql = MysqlSettings()
@@ -106,6 +107,7 @@ def __init__(self):
106107
self.optimize_interval = 0
107108
self.check_db_updated_interval = 0
108109
self.indexes: list[Index] = []
110+
self.auto_restart_interval = 0
109111

110112
def load(self, settings_file):
111113
data = open(settings_file, 'r').read()
@@ -123,6 +125,9 @@ def load(self, settings_file):
123125
self.check_db_updated_interval = data.pop(
124126
'check_db_updated_interval', Settings.DEFAULT_CHECK_DB_UPDATED_INTERVAL,
125127
)
128+
self.auto_restart_interval = data.pop(
129+
'auto_restart_interval', Settings.DEFAULT_AUTO_RESTART_INTERVAL,
130+
)
126131
indexes = data.pop('indexes', [])
127132
for index in indexes:
128133
self.indexes.append(

mysql_ch_replicator/db_replicator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def __init__(self, config: Settings, database: str, target_database: str = None,
132132
self.records_to_delete = defaultdict(set) # table_name => {record_id, ...}
133133
self.last_records_upload_time = 0
134134
self.last_touch_time = 0
135+
self.start_time = time.time()
135136

136137
def create_state(self):
137138
return State(os.path.join(self.config.binlog_replicator.data_dir, self.database, 'state.pckl'))
@@ -359,6 +360,12 @@ def run_realtime_replication(self):
359360
killer = GracefulKiller()
360361

361362
while not killer.kill_now:
363+
if self.config.auto_restart_interval:
364+
curr_time = time.time()
365+
if curr_time - self.start_time >= self.config.auto_restart_interval:
366+
logger.info('process restart (check auto_restart_interval config option)')
367+
break
368+
362369
event = self.data_reader.read_next_event()
363370
if event is None:
364371
time.sleep(DbReplicator.READ_LOG_INTERVAL)

0 commit comments

Comments
 (0)