Skip to content

Commit 1db44eb

Browse files
authored
Merge pull request #585 from BoldGrid/cron-interval
Cron interval
2 parents 3da3c45 + 7a4435b commit 1db44eb

File tree

7 files changed

+104
-10
lines changed

7 files changed

+104
-10
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public function add_all_crons( array $settings ) {
207207
$scheduled = $this->add_cron_entry( $settings );
208208
}
209209

210-
$jobs_scheduled = $this->schedule_jobs();
210+
$jobs_scheduled = $this->schedule_jobs( $settings );
211211
$site_check = $this->schedule_site_check( $settings );
212212

213213
$success = $scheduled && $jobs_scheduled;
@@ -313,17 +313,21 @@ public function read_cron_entry( $mode = 'backup' ) {
313313
* @see BoldGrid_Backup_Admin_Core::get_backup_identifier()
314314
* @see BoldGrid_Backup_Admin_Cron::get_cron_secret()
315315
*
316+
* @param array $settings Settings.
317+
*
316318
* @return bool Success.
317319
*/
318-
public function schedule_jobs() {
319-
$entry = sprintf(
320-
'*/5 * * * * %6$s "%1$s/%2$s" siteurl=%3$s id=%4$s secret=%5$s > /dev/null 2>&1',
320+
public function schedule_jobs( $settings ) {
321+
$cron_interval = isset( $settings['cron_interval'] ) ? $settings['cron_interval'] : '*/10 * * * *';
322+
$entry = sprintf(
323+
'%7$s %6$s "%1$s/%2$s" siteurl=%3$s id=%4$s secret=%5$s > /dev/null 2>&1',
321324
dirname( dirname( __FILE__ ) ),
322325
$this->run_jobs,
323326
get_site_url(),
324327
$this->core->get_backup_identifier(),
325328
$this->get_cron_secret(),
326-
$this->cron_command
329+
$this->cron_command,
330+
$cron_interval
327331
);
328332

329333
return $this->update_cron( $entry );

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,31 @@ public function maybe_fix_stalled() {
274274
public function run() {
275275
$this->set_jobs();
276276

277-
// If there are no jobs or already running, then abort.
278-
if ( empty( $this->jobs ) || $this->is_running() ) {
277+
/*
278+
* If there are no jobs in the job list, then delete this cron.
279+
* Please note, that this method is called by the cron itself,
280+
* so it will only delete if the cron list is empty when the cron starts.
281+
* Sometimes, the job will have completed, but not have been removed from the list yet.
282+
* In this case, the cron will not delete itself until the next scheduled cron run.
283+
*
284+
* The basic pattern is this:
285+
* 1. Automatic backup is scheduled.
286+
* 2. Automatic backup is run.
287+
* 3. While automatic backup is running, if remote storage is enabled,
288+
* the run-jobs.php cron is added.
289+
* 4. When automatic backup is complete, the upload backup job is added to the cron list.
290+
* 5. Next time the run-jobs.php cron runs, the upload backup job is run.
291+
* 6. The next time the run-jobs.php cron runs, the upload backup job is deleted.
292+
* 7. The next time the run-jobs.php cron runs, the run-jobs.php cron is deleted.
293+
* 8. Repeat.
294+
*/
295+
if ( empty( $this->jobs ) ) {
296+
$this->core->cron->delete_cron_entries( 'jobs' );
297+
wp_die();
298+
}
299+
300+
// If we have a job currently running, abort.
301+
if ( $this->is_running() ) {
279302
wp_die();
280303
}
281304

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,15 @@ private function update_settings() {
739739
$settings['scheduler'] = $_POST['scheduler'];
740740
}
741741

742+
/*
743+
* Save cron interval Settings.
744+
*
745+
* @since 1.16.0
746+
*/
747+
if ( isset( $_POST['cron_interval'] ) ) {
748+
$settings['cron_interval'] = $_POST['cron_interval'];
749+
}
750+
742751
/*
743752
* Save WP Cron / Crons.
744753
*

admin/js/boldgrid-backup-admin-settings.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,34 @@ BoldGrid.Settings = function( $ ) {
294294
}
295295
};
296296

297+
/**
298+
* Toggle Cron Interval
299+
*
300+
* @summary Toggle the cron interval select element.
301+
*
302+
* @since 1.16.0
303+
*/
304+
self.toggleCronInterval = function() {
305+
var $schedulerSelect = $( 'select[name="scheduler"]' ),
306+
toggleInterval = function( val ) {
307+
if ( 'cron' === val ) {
308+
$( '#cron_interval' ).show();
309+
} else {
310+
$( '#cron_interval' ).hide();
311+
}
312+
};
313+
314+
toggleInterval( $schedulerSelect.find( 'option:selected' ).val() );
315+
316+
$schedulerSelect.on( 'change', function() {
317+
toggleInterval(
318+
$( this )
319+
.find( 'option:selected' )
320+
.val()
321+
);
322+
} );
323+
};
324+
297325
// Onload event listener.
298326
$( function() {
299327

@@ -304,6 +332,8 @@ BoldGrid.Settings = function( $ ) {
304332

305333
self.toggleCompressionInfo();
306334

335+
self.toggleCronInterval();
336+
307337
$body.on( 'click', '#storage_locations input[type="checkbox"]', self.toggleNoStorage );
308338

309339
$backupDir.on( 'input', self.toggleMoveBackups );

admin/partials/settings/scheduler.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,28 @@
4545

4646
$scheduler_select = sprintf( '<select name="scheduler" id="scheduler">%1$s</select>', $scheduler_options );
4747

48+
$intervals = array(
49+
'*/5 * * * *' => esc_html__( 'Every 5 Minutes', 'boldgrid-backup' ),
50+
'*/10 * * * *' => esc_html__( 'Every 10 Minutes', 'boldgrid-backup' ),
51+
'*/30 * * * *' => esc_html__( 'Every 30 Minutes', 'boldgrid-backup' ),
52+
'0 * * * *' => esc_html__( 'Once Every Hour', 'boldgrid-backup' ),
53+
);
54+
55+
$selected_interval = ! empty( $settings['cron_interval'] ) ? $settings['cron_interval'] : '*/10 * * * *';
56+
57+
$cron_interval_options = '';
58+
59+
foreach ( $intervals as $key => $interval ) {
60+
$cron_interval_options .= sprintf(
61+
'<option value="%1$s" %3$s>%2$s</option>',
62+
$key,
63+
$interval,
64+
$key === $selected_interval ? 'selected="selected"' : ''
65+
);
66+
}
67+
68+
$cron_interval_select = sprintf( '<select name="cron_interval" id="cron_interval">%1$s</select>', $cron_interval_options );
69+
4870
return sprintf(
4971
'
5072
<div class="bg-box">
@@ -54,10 +76,12 @@
5476
<div class="bg-box-bottom">
5577
%2$s
5678
%3$s
79+
%5$s
5780
</div>
5881
</div>',
5982
__( 'Scheduler', 'boldgrid-backup' ),
6083
$scheduler_select,
6184
$wp_cron_warning,
62-
__( 'Advanced', 'boldgrid-backup' )
85+
__( 'Advanced', 'boldgrid-backup' ),
86+
$cron_interval_select
6387
);

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.15.10
19+
* Version: 1.16.0
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: 4.4
55
Tested up to: 6.4
66
Requires PHP: 5.4
7-
Stable tag: 1.15.10
7+
Stable tag: 1.16.0
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.0 =
136+
Release Date: February 26, 2024
137+
* New Feature: Add settings for cron interval for run-jobs.php [#584](https://github.com/BoldGrid/boldgrid-backup/issues/584)
138+
135139
= 1.15.10 =
136140

137141
Release date: February 7, 2024

0 commit comments

Comments
 (0)