Skip to content

Commit 9182502

Browse files
author
NiallSmyth
committed
rhubarb_app environment setting support to allow background tasks to work in new environment
1 parent e8c6b9e commit 9182502

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
* TODO: Windows support
66
* TODO: Restore PhpIdeConfig setting to allow debugging of running background tasks
77

8+
### 1.1.1
9+
* UPDATED: rhubarb_app environment setting support to allow background tasks to work in new environment
10+
811
### 1.1.0
912
* UPDATED: Stem 1.1 Support
1013

1114
### 1.0.0
1215

1316
* ADDED: Codeception instead of PHP unit
14-
* REMOVED: Reference to the PhpIdeConfig - not in Rhubarb 1.0.0 any longer
17+
* REMOVED: Reference to the PhpIdeConfig - not in Rhubarb 1.0.0 any longer

src/BackgroundTask.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
use Rhubarb\Crown\Logging\Log;
2222
use Rhubarb\Scaffolds\BackgroundTasks\Models\BackgroundTaskStatus;
23+
use Rhubarb\Crown\Application;
2324

2425
/**
2526
* Extend this class to create an executable BackgroundTask
@@ -34,9 +35,10 @@ abstract class BackgroundTask
3435
/**
3536
* Executes the long running code.
3637
*
38+
* @param BackgroundTaskStatus $status
3739
* @return void
3840
*/
39-
public abstract function execute(BackgroundTaskStatus $status);
41+
abstract public function execute(BackgroundTaskStatus $status);
4042

4143
/**
4244
* If you need to provide additional arguments for the task runner (such as passing encrypted conenction
@@ -62,6 +64,8 @@ public static function setShellArguments($rawShellArguments)
6264
/**
6365
* Initiates execution of the background task.
6466
*
67+
* @param array $settings Settings which will be passed to the execute method of the BackgroundTask (must be JSON serialisable)
68+
*
6569
* @return BackgroundTaskStatus The status object for this task.
6670
*/
6771
public static function initiate($settings = [])
@@ -79,12 +83,14 @@ public static function initiate($settings = [])
7983
$additionalArgumentString .= escapeshellarg($argument);
8084
}
8185

82-
$command = "/usr/bin/env php " . realpath(VENDOR_DIR."/rhubarbphp/rhubarb/platform/execute-cli.php") . " " . realpath(__DIR__ . "/Scripts/task-runner.php") . " " . escapeshellarg(get_called_class()) . " " . $task->BackgroundTaskStatusID . " " . $additionalArgumentString . " > /dev/null 2>&1 &";
86+
$runningRhubarbAppClass = escapeshellarg(get_class(Application::current()));
87+
$command = "rhubarb_app=$runningRhubarbAppClass /usr/bin/env php " . realpath(VENDOR_DIR . "/rhubarbphp/rhubarb/platform/execute-cli.php") . " " .
88+
realpath(__DIR__ . "/Scripts/task-runner.php") . " " . escapeshellarg(get_called_class()) . " " . $task->BackgroundTaskStatusID . " " . $additionalArgumentString . " > /dev/null 2>&1 &";
8389

8490
Log::debug("Launching background task " . $task->UniqueIdentifier, "BACKGROUND", $command);
8591

8692
exec($command);
8793

8894
return $task;
8995
}
90-
}
96+
}

src/Models/BackgroundTaskStatus.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* @property string $Message
3737
* @property string $TaskStatus The status of the task: Running, Complete or Failed
3838
* @property string $ExceptionDetails If the task failed, exception details will be contained here.
39+
* @property \stdClass $TaskSettings Settings to be passed to the task, stored as JSON
3940
*/
4041
class BackgroundTaskStatus extends Model
4142
{
@@ -53,13 +54,16 @@ protected function createSchema()
5354
$schema = new ModelSchema("tblBackgroundTaskStatus");
5455
$schema->addColumn(
5556
new AutoIncrementColumn("BackgroundTaskStatusID"),
56-
new StringColumn( "TaskClass", 300 ),
57-
new MySqlEnumColumn( "TaskStatus", self::TASK_STATUS_RUNNING,
58-
[ self::TASK_STATUS_COMPLETE, self::TASK_STATUS_FAILED, self::TASK_STATUS_RUNNING ] ),
57+
new StringColumn("TaskClass", 300),
58+
new MySqlEnumColumn(
59+
"TaskStatus",
60+
self::TASK_STATUS_RUNNING,
61+
[self::TASK_STATUS_COMPLETE, self::TASK_STATUS_FAILED, self::TASK_STATUS_RUNNING]
62+
),
5963
new DecimalColumn("PercentageComplete", 5, 2, 0),
60-
new StringColumn("Message",200),
64+
new StringColumn("Message", 200),
6165
new LongStringColumn("ExceptionDetails"),
62-
new JsonColumn( "TaskSettings", null, true )
66+
new JsonColumn("TaskSettings", null, true)
6367
);
6468

6569
return $schema;
@@ -81,9 +85,9 @@ public function start()
8185
$task->execute($this);
8286

8387
$this->TaskStatus = "Complete";
84-
} catch ( RhubarbException $er ) {
88+
} catch (RhubarbException $er) {
8589
$this->TaskStatus = "Failed";
86-
$this->ExceptionDetails = $er->getMessage()."\r\n\r\n".$er->getTraceAsString();
90+
$this->ExceptionDetails = $er->getMessage() . "\r\n\r\n" . $er->getTraceAsString();
8791
}
8892

8993
$this->save();
@@ -95,4 +99,4 @@ public function isRunning()
9599

96100
return $this->TaskStatus == "Running";
97101
}
98-
}
102+
}

0 commit comments

Comments
 (0)