Skip to content

Commit

Permalink
correction of duplicate execution record, identification of commands …
Browse files Browse the repository at this point in the history
…and unfilled data types
  • Loading branch information
robersonfaria committed Jun 18, 2021
1 parent 023de89 commit 760f932
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
18 changes: 13 additions & 5 deletions src/Console/Scheduling/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Console\Scheduling\Event;
use Illuminate\Console\Scheduling\Schedule as BaseSchedule;
use RobersonFaria\DatabaseSchedule\Http\Services\ScheduleService;
use RobersonFaria\DatabaseSchedule\Models\ScheduleHistory;

class Schedule extends BaseSchedule
{
Expand All @@ -23,15 +22,15 @@ public function dueEvents($app)
// @var Event $event
if ($schedule->command === 'custom') {
$command = $schedule->command_custom;
$commandMd5 = md5($command);
$commandName = $command;
$event = $this->exec($command);
} else {
$command = $schedule->command . $schedule->mapOptions();
$commandMd5 = md5($schedule->command . $schedule->mapOptions() . json_encode($schedule->mapArguments() ?? []));
$commandName = $schedule->command . $schedule->mapOptions() . " " . $this->argumentsToString($schedule->mapArguments()[0] ?? []);
$event = $this->command($command, $schedule->mapArguments() ?? []);
}

$event->name(md5($commandMd5))
$event->name($commandName)
->cron($schedule->expression);

if ($schedule->even_in_maintenance_mode) {
Expand Down Expand Up @@ -68,7 +67,7 @@ public function dueEvents($app)
$event->onOneServer();
}

$event->after(
$event->onSuccess(
function () use ($schedule, $event, $command) {
$schedule->histories()->create(
[
Expand Down Expand Up @@ -97,4 +96,13 @@ function () use ($schedule, $event, $command) {

return parent::dueEvents($app);
}

public function argumentsToString($array)
{
$str = '';
foreach ($array as $key => $value) {
$str .= " {$key}={$value}";
}
return $str;
}
}
16 changes: 8 additions & 8 deletions src/Models/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,12 @@ public function mapArguments()
{
return [
array_map(function ($item) {
if(!empty($item["type"])) {
if (isset($item["type"]) && $item['type'] === 'function') {
return eval("return ${item['value']}");
}
settype($item['value'], $item['type']);
return $item['value'];
$type = $item['type'] ?? 'string';
if (isset($item["type"]) && $item['type'] === 'function') {
return eval("return ${item['value']}");
}
settype($item['value'], ($option['type'] ?? 'string'));
return $item['value'];
}, $this->params ?? [])
];
}
Expand All @@ -90,10 +89,11 @@ public function mapOptions()
$str = '';
if (isset($this->options) && count($this->options) > 0) {
foreach ($this->options as $name => $option) {
if ($option['type'] === 'function') {
$type = $option['type'] ?? 'string';
if ($type === 'function') {
$option['value'] = eval("return ${$option['value']}");
} else {
settype($option['value'], $option['type']);
settype($option['value'], $type);
}
$str .= ' --' . $name . '=' . $option['value'];
}
Expand Down

0 comments on commit 760f932

Please sign in to comment.