Skip to content

Commit 2823d07

Browse files
committed
Scheduler Props
1 parent e923ff6 commit 2823d07

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ To the job work correctly, it is necessary generate a row in the queue_config ta
7575
| class_name | The full path with namespace of your job class (\App\Jobs\TestJob) |
7676
| active | If the job is active or not |
7777
| schedulable | If the job is schedulable or not |
78-
| schedule_config | A JSON config of the schedule. {"method" : "The schedule methods from laravel", "params": "The params to the schedule method (optional)"} |
78+
| schedule_config | A JSON config of the schedule. {"method" : "The schedule methods from laravel", "params": "The params to the schedule method (optional)", "props": [ { "my_job_prop": 1 }, { "my_job_prop": 2 } ]} |
7979
| max_attemps | The max attempts of the queue |
8080
| max_instances | The max parallel instances of the queue |
8181
| timeout | The timeout of the queue |

src/AbstractJob.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@ abstract class AbstractJob implements ShouldQueue
1717

1818
private $connectionName;
1919

20+
protected $name;
21+
2022
abstract function execute();
2123

22-
abstract function getName();
24+
public function setName($name) {
25+
$this->name = $name;
26+
}
27+
28+
public function getName() {
29+
return $this->name;
30+
}
2331

2432
private function preventKillProcess()
2533
{
@@ -66,6 +74,9 @@ final public function dispatch()
6674
final public function setProps($props)
6775
{
6876
foreach ($props as $key => $value) {
77+
if ($value instanceof \stdClass) {
78+
$value = json_encode($value);
79+
}
6980
$this->$key = $value;
7081
}
7182
}

src/Core/Scheduler.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,20 @@ public static function schedule(Schedule $schedule)
2828
$params = $scheduleConfig->params;
2929
}
3030

31-
$schedule->call(function () use ($schedulableQueue) {
32-
31+
$schedule->call(function () use ($schedulableQueue, $scheduleConfig) {
3332
$className = $schedulableQueue->class_name;
34-
35-
$job = (new $className());
36-
$job->dispatch();
37-
33+
if ($scheduleConfig->props && is_array($scheduleConfig->props)) {
34+
foreach($scheduleConfig->props as $prop) {
35+
$job = (new $className());
36+
$job->setName($schedulableQueue->name);
37+
$job->setProps($prop);
38+
$job->dispatch();
39+
}
40+
} else {
41+
$job = (new $className());
42+
$job->setName($schedulableQueue->name);
43+
$job->dispatch();
44+
}
3845
})->$method($params);
3946
}
4047
}

0 commit comments

Comments
 (0)