Skip to content

Commit 9e6f02b

Browse files
authored
Merge pull request #2669 from mecab/ignore-old-cronjob-option
Add -t option to ignore too old cronjob active flag
2 parents 3cb167a + 495f48d commit 9e6f02b

File tree

6 files changed

+97
-10
lines changed

6 files changed

+97
-10
lines changed

Diff for: cronjobs/run-maintenance.sh

+21-2
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,25 @@ fi
4747
ME=$( basename $0 )
4848

4949
# Overwrite some settings via command line arguments
50-
while getopts "hfvp:d:" opt; do
50+
while getopts "hfvt:p:d:" opt; do
5151
case "$opt" in
5252
h|\?)
53-
echo "Usage: $0 [-v] [-p PHP_BINARY] [-d SUBFOLDER]";
53+
echo "Usage: $0 [-v] [-f] [-t TIME_IN_SEC] [-p PHP_BINARY] [-d SUBFOLDER]";
5454
exit 0
5555
;;
5656
v) VERBOSE=1 ;;
5757
f) PHP_OPTS="$PHP_OPTS -f";;
5858
p) PHP_BIN=$OPTARG ;;
5959
d) SUBFOLDER=$OPTARG ;;
60+
t)
61+
if [[ $OPTARG =~ ^[0-9]+$ ]]; then
62+
TIMEOUT=$OPTARG
63+
PHP_OPTS="$PHP_OPTS -t $OPTARG"
64+
else
65+
echo "Option -t requires an integer" >&2
66+
exit 1
67+
fi
68+
;;
6069
:)
6170
echo "Option -$OPTARG requires an argument." >&2
6271
exit 1
@@ -102,6 +111,16 @@ fi
102111
# Our PID of this shell
103112
PID=$$
104113

114+
# If $PIDFILE exists and older than the time specified by -t, remove it.
115+
if [[ -e $PIDFILE ]]; then
116+
if [[ -n $TIMEOUT ]] && \
117+
[[ $(( $(date +%s) - $(stat -c %Y $PIDFILE) )) -gt $TIMEOUT ]]; then
118+
echo "$PIDFILE exists but older than the time you specified in -t option ($TIMEOUT sec)."
119+
echo "Removing PID file."
120+
rm $PIDFILE
121+
fi
122+
fi
123+
105124
if [[ -e $PIDFILE ]]; then
106125
echo "Cron seems to be running already"
107126
RUNPID=$( cat $PIDFILE )

Diff for: cronjobs/run-payout.sh

+21-3
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,25 @@ fi
4646
# My own name
4747
ME=$( basename $0 )
4848

49-
# Overwrite some settings via command line arguments
50-
while getopts "hfvp:d:" opt; do
49+
while getopts "hfvt:p:d:" opt; do
5150
case "$opt" in
5251
h|\?)
53-
echo "Usage: $0 [-v] [-p PHP_BINARY] [-d SUBFOLDER]";
52+
echo "Usage: $0 [-v] [-f] [-t TIME_IN_SEC] [-p PHP_BINARY] [-d SUBFOLDER]";
5453
exit 0
5554
;;
5655
v) VERBOSE=1 ;;
5756
f) PHP_OPTS="$PHP_OPTS -f";;
5857
p) PHP_BIN=$OPTARG ;;
5958
d) SUBFOLDER=$OPTARG ;;
59+
t)
60+
if [[ $OPTARG =~ ^[0-9]+$ ]]; then
61+
TIMEOUT=$OPTARG
62+
PHP_OPTS="$PHP_OPTS -t $OPTARG"
63+
else
64+
echo "Option -t requires an integer" >&2
65+
exit 1
66+
fi
67+
;;
6068
:)
6169
echo "Option -$OPTARG requires an argument." >&2
6270
exit 1
@@ -102,6 +110,16 @@ fi
102110
# Our PID of this shell
103111
PID=$$
104112

113+
# If $PIDFILE exists and older than the time specified by -t, remove it.
114+
if [[ -e $PIDFILE ]]; then
115+
if [[ -n $TIMEOUT ]] && \
116+
[[ $(( $(date +%s) - $(stat -c %Y $PIDFILE) )) -gt $TIMEOUT ]]; then
117+
echo "$PIDFILE exists but older than the time you specified in -t option ($TIMEOUT sec)."
118+
echo "Removing PID file."
119+
rm $PIDFILE
120+
fi
121+
fi
122+
105123
if [[ -e $PIDFILE ]]; then
106124
echo "Cron seems to be running already"
107125
RUNPID=$( cat $PIDFILE )

Diff for: cronjobs/run-statistics.sh

+21-2
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,25 @@ fi
4747
ME=$( basename $0 )
4848

4949
# Overwrite some settings via command line arguments
50-
while getopts "hfvp:d:" opt; do
50+
while getopts "hfvt:p:d:" opt; do
5151
case "$opt" in
5252
h|\?)
53-
echo "Usage: $0 [-v] [-p PHP_BINARY] [-d SUBFOLDER]";
53+
echo "Usage: $0 [-v] [-f] [-t TIME_IN_SEC] [-p PHP_BINARY] [-d SUBFOLDER]";
5454
exit 0
5555
;;
5656
v) VERBOSE=1 ;;
5757
f) PHP_OPTS="$PHP_OPTS -f";;
5858
p) PHP_BIN=$OPTARG ;;
5959
d) SUBFOLDER=$OPTARG ;;
60+
t)
61+
if [[ $OPTARG =~ ^[0-9]+$ ]]; then
62+
TIMEOUT=$OPTARG
63+
PHP_OPTS="$PHP_OPTS -t $OPTARG"
64+
else
65+
echo "Option -t requires an integer" >&2
66+
exit 1
67+
fi
68+
;;
6069
:)
6170
echo "Option -$OPTARG requires an argument." >&2
6271
exit 1
@@ -102,6 +111,16 @@ fi
102111
# Our PID of this shell
103112
PID=$$
104113

114+
# If $PIDFILE exists and older than the time specified by -t, remove it.
115+
if [[ -e $PIDFILE ]]; then
116+
if [[ -n $TIMEOUT ]] && \
117+
[[ $(( $(date +%s) - $(stat -c %Y $PIDFILE) )) -gt $TIMEOUT ]]; then
118+
echo "$PIDFILE exists but older than the time you specified in -t option ($TIMEOUT sec)."
119+
echo "Removing PID file."
120+
rm $PIDFILE
121+
fi
122+
fi
123+
105124
if [[ -e $PIDFILE ]]; then
106125
echo "Cron seems to be running already"
107126
RUNPID=$( cat $PIDFILE )

Diff for: cronjobs/shared.inc.php

+23-3
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,39 @@ function cfip() { return (@defined('SECURITY')) ? 1 : 0; }
5151
require_once(BASEPATH . '../include/bootstrap.php');
5252
require_once(BASEPATH . '../include/version.inc.php');
5353

54+
// Load 3rd party logging library for running crons
55+
$log = KLogger::instance( BASEPATH . '../logs/' . $cron_name, KLogger::INFO );
56+
5457
// Command line switches
5558
array_shift($argv);
56-
foreach ($argv as $option) {
59+
foreach ($argv as $index => $option) {
5760
switch ($option) {
5861
case '-f':
5962
$monitoring->setStatus($cron_name . "_disabled", "yesno", 0);
6063
$monitoring->setStatus($cron_name . "_active", "yesno", 0);
6164
break;
65+
case '-t':
66+
// When `-t TIME_IN_SEC` is specified, we ignore the cron active flag
67+
// if the time elapsed `TIME_IN_SEC` seconds after the last job started.
68+
69+
// Check the next argument is the value for -t option.
70+
if (!($index + 1 < count($argv)) || // check if '-t' is not the last argument.
71+
!(ctype_digit($argv[$index + 1]))) { // check the next argument is numeric string
72+
$log->logFatal('Option -t requires an integer.');
73+
$monitoring->endCronjob($cron_name, 'E0085', 3, true, false);
74+
}
75+
76+
$timeout = intval($argv[$index + 1]);
77+
$timeElapsedFromLastStart = $dStartTime - $monitoring->getLastCronStarted($cron_name);
78+
79+
if ($timeElapsedFromLastStart > $timeout) {
80+
$log->logWarn("Previous cronjob `$cron_name` is started before than you specified by -t. Re-run forced.");
81+
$monitoring->setStatus($cron_name . "_active", "yesno", 0);
82+
}
83+
break;
6284
}
6385
}
6486

65-
// Load 3rd party logging library for running crons
66-
$log = KLogger::instance( BASEPATH . '../logs/' . $cron_name, KLogger::INFO );
6787
$log->LogDebug('Starting ' . $cron_name);
6888

6989
// Load the start time for later runtime calculations for monitoring

Diff for: include/classes/monitoring.class.php

+10
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ public function isDisabled($name) {
6060
return $aStatus['value'];
6161
}
6262

63+
/**
64+
* Get the timestamp that last time a cronjob started
65+
* @param name string Cronjob name
66+
* @return int unix timestamp of last time the cronjob started
67+
**/
68+
public function getLastCronStarted($name) {
69+
$aStatus = $this->getStatus($name . '_starttime');
70+
return $aStatus['value'];
71+
}
72+
6373
/**
6474
* Fetch a value from our table
6575
* @param name string Setting name

Diff for: include/config/error_codes.inc.php

+1
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,4 @@
8080
$aErrorCodes['E0082'] = 'Block does not supply any usable confirmation information';
8181
$aErrorCodes['E0083'] = 'Maintenance mode enabled, skipped';
8282
$aErrorCodes['E0084'] = 'Error updating %s table';
83+
$aErrorCodes['E0085'] = 'Cron disabled due to invalid arguments';

0 commit comments

Comments
 (0)