use Org\Mxchange\CoreFramework\Traits\Visitor\VisitorTrait;
use Org\Mxchange\CoreFramework\Visitor\Visitable;
+// Import SPL stuff
+use \InvalidArgumentException;
+use \UnexpectedValueException;
+
/**
* A Task handler
*
* @param $taskName A task name to register the task on
* @param $taskInstance An instance of a Taskable class
* @return void
+ * @throws InvalidArgumentException If a parameter is not valid
+ * @throws UnexpectedValueException If an unexpected value has been configured
*/
public function registerTask (string $taskName, Taskable $taskInstance) {
- // Get interval delay
+ // Is the parameter valid
+ if (empty($taskName)) {
+ // Task name cannot be empty
+ throw new InvalidArgumentException('Parameter "taskName" cannot be empty.');
+ }
+
+ // Get interval delay, startup delay and max runs
$intervalDelay = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_interval_delay');
- $startupDelay = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_startup_delay');
+ $startupDelay = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_startup_delay');
+ $maxRuns = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_max_runs');
// If the task is 'idle_loop', a deplay of zero seconds is fine
- assert($intervalDelay >= 0);
- assert(($taskName === 'idle_loop') || (($taskName != 'idle_loop') && ($intervalDelay > 0)));
- assert(($taskName === 'idle_loop') || (($taskName != 'idle_loop') && ($startupDelay > 0)));
+ if ($intervalDelay < 0) {
+ // Invalid configuration value
+ throw new UnexpectedValueException(sprintf('taskName=%s has intervalDelay=%d below zero', $taskName, $intervalDelay));
+ } elseif ($startupDelay < 0) {
+ // Invalid configuration value
+ throw new UnexpectedValueException(sprintf('taskName=%s has startupDelay=%d below zero', $taskName, $startupDelay));
+ } elseif ($maxRuns < 0) {
+ // Invalid configuration value
+ throw new UnexpectedValueException(sprintf('taskName=%s has maxRuns=%d below zero', $taskName, $maxRuns));
+ } elseif ($taskName != 'idle_loop' && $intervalDelay == 0) {
+ // Only idle_loop can have a zero interval delay
+ throw new UnexpectedValueException(sprintf('taskName=%s has zero interval delay which is only valid for "idle_loop" task', $taskName));
+ } elseif ($taskName != 'idle_loop' && $startupDelay == 0) {
+ // Only idle_loop can have a zero interval delay
+ throw new UnexpectedValueException(sprintf('taskName=%s has zero startup delay which is only valid for "idle_loop" task', $taskName));
+ }
// Create the entry
$taskEntry = [
// Interval time (delay) in milliseconds before this task is executed again
'task_interval_delay' => $intervalDelay,
// How often should this task run?
- 'task_max_runs' => FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('task_' . $taskName . '_max_runs'),
+ 'task_max_runs' => $maxRuns,
];
// Add the entry