Commit 332604e9 authored by Boxiang Sun's avatar Boxiang Sun

Prototype: try to use promise to report the error in cron services

parent 4f917a2c
......@@ -15,12 +15,16 @@
[template-matomo-instance]
filename = matomo-instance.cfg.in
md5sum = 8f7f7c1ae6395f5c12894eb6ba2e8424
md5sum = 65fe09694eac010a8c58ba3416ccc98f
[template-matomo-backup.sh]
filename = matomo-backup.sh.in
md5sum = fb29ad59813ef62c3f5934d4a0d90e14
md5sum = 2f8b75f742dda2f4be625e673ecd9ae0
[template-xxx.sh]
filename = xxx.sh.in
md5sum = 291a70f3809aa6c7338f34c4bac36577
[custom-promise]
filename = promise/check_cron_service.py
md5sum = 2e2696061faac493908d87a3cce10988
md5sum = f93a23c08c4f6b864e931c7b4e767a0c
......@@ -2,7 +2,7 @@
set -e
set -x
exit 1
#checkout if directory and matomo resources exist
if [ ! -d {{ parameter_dict['document-root'] }}/matomo/config ]; then
......@@ -46,4 +46,4 @@ if [ -d $TMPFILE ]; then
mv -f $TMPFILE {{ parameter_dict['dir-backup'] }}/`date +%m-%d-%Y-%T`
fi
exit 0
exit 1
......@@ -9,8 +9,9 @@ diff-bin = {{ diffutils_location }}
recipe = slapos.cookbook:cron.d
cron-entries = ${cron:cron-entries}
name = matomo-backup
frequency = 0 0 * * *
command = ${matomo-backup.sh:output}
frequency = * * * * *
log-file = ${directory:log}/matomo-backup.log
command = ${matomo-backup.sh:output} 2>&1 | ${xxx.sh:output} > ${:log-file}
[matomo-backup.sh]
recipe = slapos.recipe.template:jinja2
......@@ -20,6 +21,11 @@ context =
section parameter_dict instance-parameter
key php_bin php-bin:wrapper-path
[xxx.sh]
recipe = slapos.recipe.template:jinja2
url = {{ xxx_sh }}
output = ${directory:scripts}/xxx
[promise-cron]
<= monitor-promise-base
promise = check_cron_service
......@@ -33,6 +39,9 @@ eggs = slapos.core
file = {{ custom_promise }}
output = ${directory:plugins}/check-cron-service.py
config-cron_log = ${cron-simplelogger:log}
config-partition_path = ${directory:etc}
config-cron_name = ${matomo-backup-cron:name}
config-log_file = ${matomo-backup-cron:log-file}
[apache-php-service]
environment=
......
import os
import fnmatch
import subprocess
from zope.interface import implementer
from slapos.grid.promise import interface
......@@ -24,12 +26,70 @@ class RunPromise(GenericPromise):
In this case, check whether the file exists.
"""
path = self.getConfig('cron_log')
if os.path.isfile(path):
self.logger.info("cron_log %s exists and is a file", path)
else:
self.logger.error("cron_log %s does not exist or is not a file", path)
cron_log_path = self.getConfig('cron_log')
if not os.path.isfile(cron_log_path):
self.logger.error("cron_log %s does not exist or is not a file", cron_log_path)
target_string = "exit 1"
partition_path = self.getConfig('partition_path')[:-3]
cron_service_name = self.getConfig('cron_name')
cron_log_file = self.getConfig('log_file')
print(cron_log_file)
# Find the crond log like
# .slappart2_crond-94787cb8beef266b357c15b8ee69f958.log
# Walk through the directory and its subdirectories
# for root, _, files in os.walk(partition_path):
# for filename in files:
# if fnmatch.fnmatch(filename, '*slappart*cron*.log'):
# crond_log_file = os.path.join(root, filename)
# break
# print(crond_log_file)
# Check the log file, but which line should be considered as failed one?
print("0000000000000000000000")
# When run the slapos node instance, slapos can not check the cron result immediately
# because the cron service may need to wait the midnight to launch.
print(cron_log_file)
if not os.path.exists(cron_log_file):
return
print("1111111111111111111111111")
try:
with open(cron_log_file, "r") as file:
log_content = file.read()
if "exit 1" in log_content:
self.logger.error(f"Cron service {cron_service_name} failed: {log_content}")
print(log_content)
except FileNotFoundError:
print(f"File not found: {cron_log_file}")
except Exception as e:
print(f"An error occurred: {str(e)}")
error_line = self.find_error_in_file(cron_log_path, target_string)
if error_line:
self.logger.error(f"There is an error in the cron log: {error_line}")
def find_error_in_file(self, file_path, target_string):
try:
found_line = None
with open(file_path, "r") as file:
for line in file:
if target_string in line:
found_line = line.strip() # Remove leading/trailing whitespace
break # Stop searching once found
return found_line
except FileNotFoundError:
print(f"File not found: {file_path}")
return None
except Exception as e:
print(f"An error occurred: {str(e)}")
return None
def test(self):
"""
......
......@@ -47,11 +47,15 @@ context =
key php_location apache-php:location
key matomo_backup_sh template-matomo-backup.sh:target
key custom_promise custom-promise:target
key xxx_sh template-xxx.sh:target
# download matomo-backup.sh.in
[template-matomo-backup.sh]
<= matomo-download
[template-xxx.sh]
<= matomo-download
# Download the custom promise
[custom-promise]
<= matomo-download
......
#!/bin/bash
while read x; do
echo -n `date +%d/%m/%Y\ %H:%M:%S`;
echo -n " ";
echo $x;
done
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment