Skip to content

Commit 0ed9d8b

Browse files
authored
Remove jobs from queue if older than 1 week (#620)
* dont send emails older than 2 weeks * add check to remove old jobs * update jobs * move to maybe_fix_stalled * update jobs * update version and readme * update jobs * reorganize and add comments * remove error log * remove error log and add comments * update test * update tests
1 parent 00e34f1 commit 0ed9d8b

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

admin/class-boldgrid-backup-admin-jobs.php

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -259,31 +259,52 @@ public function post_jobs_email( $info ) {
259259
* Fix stalled jobs.
260260
*
261261
* @since 1.15.5
262+
*
263+
* A stalled job can be a job that has been pending for over a week. This is usually due to
264+
* a CRON bug that has since been resolved in 1.16.9. If the job is older than a week, it will
265+
* be removed.
262266
*
263-
* A stalled job is a job who's status has been set to "running", however it's been running longer
264-
* than expected.
267+
* A stalled job can also be a job who's status has been set to "running"
268+
* however it's been running longer than expected.
265269
*
266270
* For example, if a job is only supposed to take 1 minute, and it's been running for 3 hours, it's
267271
* stalled. Most likely the process was either killed and or had a fatal error.
268272
*/
269273
public function maybe_fix_stalled() {
270274
$made_changes = false;
271275

272-
foreach ( $this->jobs as &$job ) {
273-
if ( 'running' !== $job['status'] ) {
274-
continue;
276+
foreach ( $this->jobs as $key => &$job ) {
277+
// Maybe delete old job if it's older than one week.
278+
if ( preg_match('/-(\d{8})-\d{6}\.zip$/', $job['filepath'], $matches ) ) {
279+
$date_str = $matches[1];
280+
281+
// Create a DateTime object from the date string (format: YYYYMMDD)
282+
$file_date = DateTime::createFromFormat( 'Ymd', $date_str );
283+
284+
// Get the date for one week ago from now
285+
$one_week_ago = new DateTime('-1 week');
286+
287+
// Compare dates
288+
if ( $file_date < $one_week_ago ) {
289+
unset( $this->jobs[ $key ] );
290+
$made_changes = true;
291+
continue;
292+
}
275293
}
276294

277-
// Determine whether or not this job is stalled.
278-
$time_limit = HOUR_IN_SECONDS;
279-
$duration = time() - $job['start_time'];
280-
$is_stalled = $duration > $time_limit;
295+
// Maybe update job if it is running, and has stalled.
296+
if ( 'running' === $job['status'] ) {
297+
// Determine whether or not this job is stalled.
298+
$time_limit = HOUR_IN_SECONDS;
299+
$duration = time() - $job['start_time'];
300+
$is_stalled = $duration > $time_limit;
281301

282-
if ( $is_stalled ) {
283-
$job['end_time'] = time();
284-
$job['status'] = 'fail';
302+
if ( $is_stalled ) {
303+
$job['end_time'] = time();
304+
$job['status'] = 'fail';
285305

286-
$made_changes = true;
306+
$made_changes = true;
307+
}
287308
}
288309
}
289310

boldgrid-backup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Plugin Name: Total Upkeep
1717
* Plugin URI: https://www.boldgrid.com/boldgrid-backup/
1818
* Description: Automated backups, remote backup to Amazon S3 and Google Drive, stop website crashes before they happen and more. Total Upkeep is the backup solution you need.
19-
* Version: 1.16.9
19+
* Version: 1.16.10
2020
* Author: BoldGrid
2121
* Author URI: https://www.boldgrid.com/
2222
* License: GPL-2.0+

readme.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Tags: backup, cloud backup, database backup, restore, wordpress backup
44
Requires at least: 5.0
55
Tested up to: 6.7
66
Requires PHP: 5.4
7-
Stable tag: 1.16.9
7+
Stable tag: 1.16.10
88
License: GPLv2 or later
99
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1010

@@ -132,6 +132,10 @@ Have a problem? First, take a look at our [Getting Started](https://www.boldgrid
132132

133133
== Changelog ==
134134

135+
= 1.16.10 =
136+
Release Date: Feb 26, 2025
137+
* Bug Fix: Prevent old backlogged jobs from running due to a previous CRON bug.
138+
135139
= 1.16.9 =
136140
Release Date: Feb 25, 2025
137141
* Bug Fix: PHP Warning: Undefined array key “is_running” [#614](https://github.com/BoldGrid/boldgrid-backup/issues/614)

tests/admin/test-class-boldgrid-backup-admin-jobs.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,30 @@ class Test_Boldgrid_Backup_Admin_Jobs extends WP_UnitTestCase {
3232
* @since 1.15.5
3333
*/
3434
public function test_maybe_fix_stalled() {
35-
$test_jobs = array(
35+
$filepath = '/some/file/boldgrid-backup-domain.com-0134567-' . date( 'Ymd' ) . '-123456.zip';
36+
$old_job_path = '/some/file/boldgrid-backup-domain.com-0134567-' . date( 'Ymd', strtotime('-2 weeks') ) . '-123456.zip';
37+
$test_jobs = array(
3638
array(
39+
'filepath' => $filepath,
3740
'action' => 'some_action_1',
3841
'status' => 'running',
3942
// This job is not stalled. If the start time is right now, it's been running 0 seconds.
4043
'start_time' => time(),
4144
),
4245
array(
46+
'filepath' => $filepath,
4347
'action' => 'some_action_2',
4448
'status' => 'running',
4549
// This job was started 1 year ago. It should be flagged as being stalled.
4650
'start_time' => time() - YEAR_IN_SECONDS,
4751
),
52+
array(
53+
// This job was created over a week ago, so it should be removed.
54+
'filepath' => $old_job_path,
55+
'action' => 'some_action_3',
56+
'status' => 'running',
57+
'start_time' => time(),
58+
),
4859
);
4960

5061
update_option( $this->jobs->option, $test_jobs );
@@ -55,6 +66,8 @@ public function test_maybe_fix_stalled() {
5566
$this->assertEquals( 'running', $this->jobs->jobs[0]['status'] );
5667

5768
$this->assertEquals( 'fail', $this->jobs->jobs[1]['status'] );
69+
70+
$this->assertEquals( 2, count( $this->jobs->jobs ) );
5871
}
5972

6073
/**

0 commit comments

Comments
 (0)