Skip to content

Commit

Permalink
feat(helper): Rework graceful terminator
Browse files Browse the repository at this point in the history
* Rename module
* Silence pylint error
* Add method for cicd mode termination
  • Loading branch information
TobiWo committed Apr 7, 2023
1 parent e589b0d commit cfd5e40
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
19 changes: 0 additions & 19 deletions duties/helper/kill.py

This file was deleted.

51 changes: 51 additions & 0 deletions duties/helper/terminate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Helper module for graceful shutdown
"""

import signal
from sys import exit as sys_exit
from typing import Any, List

from cli.arguments import Mode
from fetcher.data_types import ValidatorDuty


# pylint: disable=too-few-public-methods
class GracefulTerminator:
"""Helper class for graceful termination"""

def __init__(self, max_number_of_cicd_cycles: int) -> None:
self.kill_now = False
self.__cicd_cycle_counter = 0
self.__max_number_of_cicd_cycles = max_number_of_cicd_cycles
signal.signal(signal.SIGINT, self.__terminate_gracefully)
signal.signal(signal.SIGTERM, self.__terminate_gracefully)

def __terminate_gracefully(self, *_: Any) -> None:
"""Main method to terminate program gracefully"""
self.kill_now = True

def terminate_in_cicd_mode(
self, running_mode: Mode, duties: List[ValidatorDuty]
) -> None:
"""Terminates the running application in dependence of the mode and
the duties which could be found for the provided validators
Args:
running_mode (Mode): Running mode of eth-duties
duties (List[ValidatorDuty]): List of fetched validator duties
"""
match running_mode:
case Mode.CICD_EXIT:
if len(duties) > 0:
sys_exit(1)
sys_exit(0)
case Mode.CICD_WAIT:
if len(duties) == 0:
sys_exit(0)
if self.__cicd_cycle_counter >= self.__max_number_of_cicd_cycles:
sys_exit(1)
case Mode.EXIT_GRACEFULLY:
sys_exit(0)
case _:
pass
self.__cicd_cycle_counter += 1

0 comments on commit cfd5e40

Please sign in to comment.