2020-05-29 18:36:07 +00:00
|
|
|
<?php
|
|
|
|
|
namespace App\Console\Commands;
|
2020-06-15 17:42:10 +00:00
|
|
|
use Illuminate\Support\Carbon;
|
2020-05-29 18:36:07 +00:00
|
|
|
use Illuminate\Console\Scheduling\ScheduleRunCommand as BaseCommand;
|
2020-06-15 17:42:10 +00:00
|
|
|
use Maveriks\WebApplication;
|
2020-06-09 14:45:12 +00:00
|
|
|
use ProcessMaker\Model\TaskScheduler;
|
2020-05-29 18:36:07 +00:00
|
|
|
class ScheduleRunCommand extends BaseCommand
|
|
|
|
|
{
|
|
|
|
|
use AddParametersTrait;
|
2020-06-02 19:04:44 +00:00
|
|
|
/**
|
2020-05-29 18:36:07 +00:00
|
|
|
* Create a new command instance.
|
|
|
|
|
*
|
2020-06-02 19:04:44 +00:00
|
|
|
* @param \Illuminate\Console\Scheduling\Schedule $schedule
|
2020-05-29 18:36:07 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(\Illuminate\Console\Scheduling\Schedule $schedule)
|
|
|
|
|
{
|
|
|
|
|
$this->startedAt = Carbon::now();
|
|
|
|
|
$this->signature = "schedule:run";
|
2020-06-15 17:42:10 +00:00
|
|
|
$this->signature .= "
|
|
|
|
|
{--workspace=workflow : Workspace to use.}
|
|
|
|
|
{--user=apache : Operating system's user who executes the crons.}
|
2020-05-29 18:36:07 +00:00
|
|
|
{--processmakerPath=./ : ProcessMaker path.}
|
2020-06-15 17:42:10 +00:00
|
|
|
";
|
2020-06-02 19:04:44 +00:00
|
|
|
$this->description .= ' (ProcessMaker has extended this command)';
|
2020-05-29 18:36:07 +00:00
|
|
|
parent::__construct($schedule);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Execute the console command.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
|
|
|
|
$that = $this;
|
|
|
|
|
$workspace = $this->option('workspace');
|
2020-06-11 19:20:16 +00:00
|
|
|
$user = $this->option('user');
|
2020-05-29 18:36:07 +00:00
|
|
|
if (!empty($workspace)) {
|
|
|
|
|
$webApplication = new WebApplication();
|
|
|
|
|
$webApplication->setRootDir($this->option('processmakerPath'));
|
|
|
|
|
$webApplication->loadEnvironment($workspace, false);
|
|
|
|
|
}
|
2020-06-11 19:20:16 +00:00
|
|
|
TaskScheduler::all()->each(function ($p) use ($that, $user) {
|
2020-07-07 16:06:59 +00:00
|
|
|
$win = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
|
2020-06-11 19:20:16 +00:00
|
|
|
if ($p->enable == 1) {
|
|
|
|
|
$starting = isset($p->startingTime) ? $p->startingTime : "0:00";
|
|
|
|
|
$ending = isset($p->startingTime) ? $p->endingTime : "23:59";
|
|
|
|
|
$timezone = isset($p->timezone) && $p->timezone != "" ? $p->timezone : date_default_timezone_get();
|
2020-07-07 16:06:59 +00:00
|
|
|
$body = $p->body;
|
|
|
|
|
if (!$win) {
|
|
|
|
|
$body = str_replace(" -c"," " . $user . " -c", $p->body);
|
|
|
|
|
}
|
2020-06-11 19:20:16 +00:00
|
|
|
$that->schedule->exec($body)->cron($p->expression)->between($starting, $ending)->timezone($timezone)->when(function () use ($p) {
|
|
|
|
|
$now = Carbon::now();
|
|
|
|
|
$result = false;
|
|
|
|
|
$datework = Carbon::createFromFormat('Y-m-d H:i:s', $p->last_update);
|
|
|
|
|
if (isset($p->everyOn)) {
|
|
|
|
|
switch ($p->interval) {
|
|
|
|
|
case "day":
|
|
|
|
|
$interval = $now->diffInDays($datework);
|
|
|
|
|
$result = ($interval !== 0 && ($interval % intval($p->everyOn)) == 0);
|
|
|
|
|
break;
|
|
|
|
|
case "week":
|
|
|
|
|
$diff = $now->diffInDays($datework);
|
|
|
|
|
if ($diff % (intval($p->everyOn) * 7) < 7 && $diff % (intval($p->everyOn) * 7) >= 0) {
|
|
|
|
|
$result = true;
|
|
|
|
|
} else {
|
|
|
|
|
$result = false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "month":
|
|
|
|
|
$interval = $now->diffInMonths($datework);
|
|
|
|
|
if ($interval % intval($p->everyOn) == 0) {
|
|
|
|
|
$result = true;
|
|
|
|
|
} else {
|
|
|
|
|
$result = false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "year":
|
|
|
|
|
$interval = $now->diffInYears($datework);
|
|
|
|
|
if ($interval % intval($p->everyOn) == 0) {
|
|
|
|
|
$result = true;
|
|
|
|
|
} else {
|
|
|
|
|
$result = false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
2020-06-09 14:45:12 +00:00
|
|
|
}
|
2020-06-11 19:20:16 +00:00
|
|
|
return true;
|
2020-10-06 21:00:54 +00:00
|
|
|
})->onOneServer();
|
2020-06-11 19:20:16 +00:00
|
|
|
}
|
2020-06-09 14:45:12 +00:00
|
|
|
});
|
2020-05-29 18:36:07 +00:00
|
|
|
parent::handle();
|
|
|
|
|
}
|
2020-07-07 16:06:59 +00:00
|
|
|
}
|