diff --git a/runbot_builder/tools.py b/runbot_builder/tools.py index 31a8a225..cc0e42dd 100644 --- a/runbot_builder/tools.py +++ b/runbot_builder/tools.py @@ -26,7 +26,6 @@ class RunbotClient(): self.host = None self.count = 0 self.max_count = 60 - self.next_git_gc_date = datetime.now() + timedelta(minutes=random.randint(5, 15)) def on_start(self): pass @@ -38,6 +37,7 @@ class RunbotClient(): signal.signal(signal.SIGTERM, self.signal_handler) signal.signal(signal.SIGQUIT, self.dump_stack) self.host = self.env['runbot.host']._get_current() + self.update_next_git_gc_date() self.host._bootstrap() logging.info( 'Host %s running with %s slots on pid %s%s', @@ -83,13 +83,21 @@ class RunbotClient(): def sleep(self, t): self.ask_interrupt.wait(t) + def update_next_git_gc_date(self): + now = datetime.now() + gc_hour = int(self.env['ir.config_parameter'].sudo().get_param('runbot.git_gc_hour', '23')) + gc_minutes = self.host.id % 60 # deterministic minutes + self.next_git_gc_date = datetime(now.year, now.month, now.day, gc_hour, gc_minutes) + while self.next_git_gc_date <= now: + self.next_git_gc_date += timedelta(days=1) + _logger.info('Next git gc scheduled on %s', self.next_git_gc_date) + def git_gc(self): """ git gc once a day """ - if datetime.now() > self.next_git_gc_date: + if self.next_git_gc_date < datetime.now(): _logger.info('Starting git gc on repositories') self.env['runbot.runbot']._git_gc(self.host) - self.next_git_gc_date = datetime.now() + timedelta(hours=2, minutes=random.randint(0, 59)) - _logger.info('Next git gc scheduled on %s', self.next_git_gc_date) + self.update_next_git_gc_date() def run(client_class): # parse args @@ -135,5 +143,9 @@ def run(client_class): env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {}) client = client_class(env) # run main loop - client.main_loop() + try: + client.main_loop() + except Exception as e: + _logger.exception(str(e)) + raise e _logger.info("Stopping gracefully")