mirror of
https://github.com/mtan93/cachet-url-monitor.git
synced 2026-03-08 05:31:58 +00:00
* #10 - Creating auxiliary client class to generate configuration class based on cachet's component list. * Updating the python version in codacy to reduce false positives * Moving some of the cachet operations to the client class to clean up the configuration class and making better constants * Refactoring status to have proper classes and adding more tests. Refactoring the requests tests to use requests-mock. * Removing unused imports from test_scheduler * Adding more tests and the ability to run the client from command line * Updating README and client arg parsing * Fixing broken unit tests
This commit is contained in:
@@ -40,45 +40,40 @@ class Agent(object):
|
||||
|
||||
|
||||
class Decorator(object):
|
||||
"""Defines the actions a user can configure to be executed when there's an incident."""
|
||||
|
||||
def execute(self, configuration):
|
||||
pass
|
||||
|
||||
|
||||
class UpdateStatusDecorator(Decorator):
|
||||
"""Updates the component status when an incident happens."""
|
||||
|
||||
def execute(self, configuration):
|
||||
configuration.push_status()
|
||||
|
||||
|
||||
class CreateIncidentDecorator(Decorator):
|
||||
"""Creates an incident entry on cachet when an incident happens."""
|
||||
|
||||
def execute(self, configuration):
|
||||
configuration.push_incident()
|
||||
|
||||
|
||||
class PushMetricsDecorator(Decorator):
|
||||
"""Updates the URL latency metric."""
|
||||
|
||||
def execute(self, configuration):
|
||||
configuration.push_metrics()
|
||||
|
||||
|
||||
class Scheduler(object):
|
||||
def __init__(self, config_file, endpoint_index):
|
||||
def __init__(self, configuration, agent):
|
||||
self.logger = logging.getLogger('cachet_url_monitor.scheduler.Scheduler')
|
||||
self.configuration = Configuration(config_file, endpoint_index)
|
||||
self.agent = self.get_agent()
|
||||
|
||||
self.configuration = configuration
|
||||
self.agent = agent
|
||||
self.stop = False
|
||||
|
||||
def get_agent(self):
|
||||
action_names = {
|
||||
'CREATE_INCIDENT': CreateIncidentDecorator,
|
||||
'UPDATE_STATUS': UpdateStatusDecorator,
|
||||
'PUSH_METRICS': PushMetricsDecorator,
|
||||
}
|
||||
actions = []
|
||||
for action in self.configuration.get_action():
|
||||
self.logger.info(f'Registering action {action}')
|
||||
actions.append(action_names[action]())
|
||||
return Agent(self.configuration, decorators=actions)
|
||||
|
||||
def start(self):
|
||||
self.agent.start()
|
||||
self.logger.info('Starting monitor agent...')
|
||||
@@ -96,6 +91,19 @@ class NewThread(threading.Thread):
|
||||
self.scheduler.start()
|
||||
|
||||
|
||||
def build_agent(configuration, logger):
|
||||
action_names = {
|
||||
'CREATE_INCIDENT': CreateIncidentDecorator,
|
||||
'UPDATE_STATUS': UpdateStatusDecorator,
|
||||
'PUSH_METRICS': PushMetricsDecorator,
|
||||
}
|
||||
actions = []
|
||||
for action in configuration.get_action():
|
||||
logger.info(f'Registering action {action}')
|
||||
actions.append(action_names[action]())
|
||||
return Agent(configuration, decorators=actions)
|
||||
|
||||
|
||||
def validate_config():
|
||||
if 'endpoints' not in config_file.keys():
|
||||
fatal_error('Endpoints is a mandatory field')
|
||||
@@ -132,4 +140,6 @@ if __name__ == "__main__":
|
||||
validate_config()
|
||||
|
||||
for endpoint_index in range(len(config_file['endpoints'])):
|
||||
NewThread(Scheduler(config_file, endpoint_index)).start()
|
||||
configuration = Configuration(config_file, endpoint_index)
|
||||
NewThread(Scheduler(configuration,
|
||||
build_agent(configuration, logging.getLogger('cachet_url_monitor.scheduler')))).start()
|
||||
|
||||
Reference in New Issue
Block a user