Skip to content

Commit

Permalink
Merge pull request symbiote#14 from ajshort/master
Browse files Browse the repository at this point in the history
Custom Job Config
  • Loading branch information
nyeholt committed Mar 17, 2014
2 parents 8bc0d69 + 8febf1f commit f221122
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
47 changes: 39 additions & 8 deletions code/services/AbstractQueuedJob.php
Expand Up @@ -73,22 +73,20 @@ public function getJobType() {
}

/**
* Implement yourself!
* Performs setup tasks the first time this job is run.
*
* Be aware that this is only executed ONCE for every job
*
* If you want to do some checking on every restart, look into using the prepareForRestart method
* This is only executed once for every job. If you want to run something on every job restart, use the
* {@link prepareForRestart} method.
*/
public function setup() {

$this->loadCustomConfig();
}

/**
* This is called when you want to perform some form of initialisation on a restart of a
* job.
* Run when an already setup job is being restarted.
*/
public function prepareForRestart() {

$this->loadCustomConfig();
}

/**
Expand Down Expand Up @@ -139,6 +137,39 @@ public function setJobData($totalSteps, $currentStep, $isComplete, $jobData, $me

}

/**
* Gets custom config settings to use when running the job.
*
* @return array|null
*/
public function getCustomConfig() {
return $this->CustomConfig;
}

/**
* Sets custom config settings to use when the job is run.
*
* @param array $config
*/
public function setCustomConfig(array $config) {
$this->CustomConfig = $config;
}

/**
* Sets custom configuration settings from the job data.
*/
private function loadCustomConfig() {
$custom = $this->getCustomConfig();

if (!is_array($custom)) {
return;
}

foreach ($custom as $class => $settings) {
foreach ($settings as $setting => $value) Config::inst()->update($class, $setting, $value);
}
}

public function addMessage($message, $severity='INFO') {
$severity = strtoupper($severity);
$this->messages[] = '[' . date('Y-m-d H:i:s') . "][$severity] $message";
Expand Down
13 changes: 8 additions & 5 deletions code/services/QueuedJobService.php
Expand Up @@ -378,6 +378,9 @@ public function runJob($jobId) {

$broken = false;

// Push a config context onto the stack for the duration of this job run.
Config::nest();

try {
$job = $this->initialiseJob($jobDescriptor);

Expand All @@ -395,8 +398,6 @@ public function runJob($jobId) {
// have we stalled at all?
$stallCount = 0;

$currentBaseUrl = Director::absoluteBaseURL();

if ($job->SubsiteID && class_exists('Subsite')) {
Subsite::changeSubsite($job->SubsiteID);

Expand All @@ -405,7 +406,8 @@ public function runJob($jobId) {
if ($subsite && $subsite->exists()) {
$domain = $subsite->domain();
$base = rtrim(Director::protocol() . $domain, '/') . '/';
Director::setbaseURL($base);

Config::inst()->update('Director', 'alternate_base_url', $base);
}
}

Expand Down Expand Up @@ -475,8 +477,7 @@ public function runJob($jobId) {
$broken = true;
}
}

Director::setBaseURL($currentBaseUrl);

// a last final save. The job is complete by now
if ($jobDescriptor) {
$jobDescriptor->write();
Expand All @@ -496,6 +497,8 @@ public function runJob($jobId) {

$errorHandler->clear();

Config::unnest();

// okay lets reset our user if we've got an original
if ($runAsUser && $originalUser) {
Session::clear("loggedInAs");
Expand Down

0 comments on commit f221122

Please sign in to comment.