Browse Source

Add support for healthchecks "log" feature #628

Signed-off-by: Soumik Dutta <shalearkane@gmail.com>
Soumik Dutta 2 years ago
parent
commit
69f6695253

+ 18 - 0
borgmatic/commands/borgmatic.py

@@ -152,6 +152,24 @@ def run_configuration(config_filename, config, arguments):
                 encountered_error = error
                 error_repository = repository_path
 
+    try:
+        # send logs irrespective of error
+        dispatch.call_hooks(
+            'ping_monitor',
+            hooks,
+            config_filename,
+            monitor.MONITOR_HOOK_NAMES,
+            monitor.State.LOG,
+            monitoring_log_level,
+            global_arguments.dry_run,
+        )
+    except (OSError, CalledProcessError) as error:
+        if command.considered_soft_failure(config_filename, error):
+            return
+
+        encountered_error = error
+        yield from log_error_records('{}: Error pinging monitor'.format(config_filename), error)
+
     if not encountered_error:
         try:
             if using_primary_action:

+ 1 - 0
borgmatic/config/schema.yaml

@@ -1199,6 +1199,7 @@ properties:
                                 - start
                                 - finish
                                 - fail
+                                - log
                             uniqueItems: true
                         description: |
                             List of one or more monitoring states to ping for:

+ 1 - 0
borgmatic/hooks/cronhub.py

@@ -10,6 +10,7 @@ MONITOR_STATE_TO_CRONHUB = {
     monitor.State.START: 'start',
     monitor.State.FINISH: 'finish',
     monitor.State.FAIL: 'fail',
+    monitor.State.LOG: 'log',
 }
 
 

+ 1 - 0
borgmatic/hooks/cronitor.py

@@ -10,6 +10,7 @@ MONITOR_STATE_TO_CRONITOR = {
     monitor.State.START: 'run',
     monitor.State.FINISH: 'complete',
     monitor.State.FAIL: 'fail',
+    monitor.State.LOG: 'log',
 }
 
 

+ 2 - 1
borgmatic/hooks/healthchecks.py

@@ -10,6 +10,7 @@ MONITOR_STATE_TO_HEALTHCHECKS = {
     monitor.State.START: 'start',
     monitor.State.FINISH: None,  # Healthchecks doesn't append to the URL for the finished state.
     monitor.State.FAIL: 'fail',
+    monitor.State.LOG: 'log',
 }
 
 PAYLOAD_TRUNCATION_INDICATOR = '...\n'
@@ -117,7 +118,7 @@ def ping_monitor(hook_config, config_filename, state, monitoring_log_level, dry_
     )
     logger.debug('{}: Using Healthchecks ping URL {}'.format(config_filename, ping_url))
 
-    if state in (monitor.State.FINISH, monitor.State.FAIL):
+    if state in (monitor.State.FINISH, monitor.State.FAIL, monitor.State.LOG):
         payload = format_buffered_logs_for_payload()
     else:
         payload = ''

+ 1 - 0
borgmatic/hooks/monitor.py

@@ -7,3 +7,4 @@ class State(Enum):
     START = 1
     FINISH = 2
     FAIL = 3
+    LOG = 4

+ 1 - 0
borgmatic/hooks/ntfy.py

@@ -10,6 +10,7 @@ MONITOR_STATE_TO_NTFY = {
     monitor.State.START: None,
     monitor.State.FINISH: None,
     monitor.State.FAIL: None,
+    monitor.State.LOG: None,
 }