Skip to content

Commit 94d85cd

Browse files
committed
add support for environment in grav scheduler
Signed-off-by: Andy Miller <[email protected]>
1 parent 0f879bd commit 94d85cd

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

system/blueprints/config/backups.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,13 @@ form:
122122
default: '* 3 * * *'
123123
validate:
124124
required: true
125+
.schedule_environment:
126+
type: select
127+
label: PLUGIN_ADMIN.BACKUPS_PROFILE_ENVIRONMENT
128+
help: PLUGIN_ADMIN.BACKUPS_PROFILE_ENVIRONMENT_HELP
129+
default: ''
130+
options:
131+
'': 'Default (cli)'
132+
localhost: 'Localhost'
133+
cli: 'CLI'
125134

system/config/backups.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ profiles:
1010
root: '/'
1111
schedule: false
1212
schedule_at: '0 3 * * *'
13+
schedule_environment: ''
1314
exclude_paths: "/backup\r\n/cache\r\n/images\r\n/logs\r\n/tmp"
1415
exclude_files: ".DS_Store\r\n.git\r\n.svn\r\n.hg\r\n.idea\r\n.vscode\r\nnode_modules"
1516

system/src/Grav/Common/Backup/Backups.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ public function onSchedulerInitialized(Event $event)
8989
$at = $profile['schedule_at'];
9090
$name = $inflector::hyphenize($profile['name']);
9191
$logs = 'logs/backup-' . $name . '.out';
92+
$environment = $profile['schedule_environment'] ?? null;
9293
/** @var Job $job */
93-
$job = $scheduler->addFunction('Grav\Common\Backup\Backups::backup', [$id], $name);
94+
$job = $scheduler->addFunction('Grav\Common\Backup\Backups::backup', [$id, null, $environment], $name);
9495
$job->at($at);
9596
$job->output($logs);
9697
$job->backlink('/tools/backups');
@@ -192,12 +193,19 @@ public static function getAvailableBackups($force = false)
192193
*
193194
* @param int $id
194195
* @param callable|null $status
196+
* @param string|null $environment Optional environment to load config from
195197
* @return string|null
196198
*/
197-
public static function backup($id = 0, ?callable $status = null)
199+
public static function backup($id = 0, ?callable $status = null, ?string $environment = null)
198200
{
199201
$grav = Grav::instance();
200202

203+
// If environment is specified and different from current, reload config
204+
if ($environment && $environment !== $grav['config']->get('setup.environment')) {
205+
$grav->setup($environment);
206+
$grav['config']->reload();
207+
}
208+
201209
$profiles = static::getBackupProfiles();
202210
/** @var UniformResourceLocator $locator */
203211
$locator = $grav['locator'];

0 commit comments

Comments
 (0)