fix scheduler in intervals

This commit is contained in:
Henry Jordan
2020-06-09 14:28:16 +00:00
parent 848ff9a887
commit 7dadafab11
3 changed files with 50 additions and 69 deletions

View File

@@ -6,7 +6,7 @@ use Maveriks\WebApplication;
use \Illuminate\Support\Carbon; use \Illuminate\Support\Carbon;
use Illuminate\Console\Scheduling\ScheduleRunCommand as BaseCommand; use Illuminate\Console\Scheduling\ScheduleRunCommand as BaseCommand;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use ProcessMaker\Model\TaskScheduler; use ProcessMaker\BusinessModel\TaskSchedulerBM;
class ScheduleRunCommand extends BaseCommand class ScheduleRunCommand extends BaseCommand
{ {
@@ -45,38 +45,7 @@ class ScheduleRunCommand extends BaseCommand
$webApplication->setRootDir($this->option('processmakerPath')); $webApplication->setRootDir($this->option('processmakerPath'));
$webApplication->loadEnvironment($workspace, false); $webApplication->loadEnvironment($workspace, false);
} }
TaskScheduler::all()->each(function ($p) use ($that) { TaskSchedulerBM::executeScheduler($this);
$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();
$that->schedule->exec($p->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":
$interval = $now->diffInDays($datework);
$result = ($interval !== 0 && $interval % (intval($p->everyOn) * 7) == 0);
break;
case "month":
$interval = $now->diffInMonths($datework);
$result = ($interval !== 0 && $interval % intval($p->everyOn) == 0);
break;
case "year":
$interval = $now->diffInYears($datework);
$result = ($interval !== 0 && $interval % intval($p->everyOn) == 0);
break;
}
return $result;
}
return true;
});
});
parent::handle(); parent::handle();
} }
} }

View File

@@ -11,14 +11,47 @@ use ProcessMaker\Core\System;
class TaskSchedulerBM class TaskSchedulerBM
{ {
public function executeTasks(){ /**
TaskScheduler::all()->each(function($p){ * Execute the records with Laravel Task Scheduler
if($p->isDue()){ */
Log::info("EXECUTE::" . $p->title . " -->" . $p->expression); public static function executeScheduler($that){
} TaskScheduler::all()->each(function ($p) use ($that) {
$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();
$that->schedule->exec($p->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":
$interval = $now->diffInDays($datework);
$result = ($interval !== 0 && $interval % (intval($p->everyOn) * 7) == 0);
break;
case "month":
$interval = $now->diffInMonths($datework);
$result = ($interval !== 0 && $interval % intval($p->everyOn) == 0);
break;
case "year":
$interval = $now->diffInYears($datework);
$result = ($interval !== 0 && $interval % intval($p->everyOn) == 0);
break;
}
return $result;
}
return true;
});
}); });
} }
/**
* Return the records in Schedule Table by category
*/
public static function getSchedule($category){ public static function getSchedule($category){
$tasks = TaskScheduler::all(); $tasks = TaskScheduler::all();
$count = $tasks->count(); $count = $tasks->count();
@@ -32,23 +65,23 @@ class TaskSchedulerBM
return TaskScheduler::where('category', $category)->get(); return TaskScheduler::where('category', $category)->get();
} }
} }
/**
* Save the record Schedule in Schedule Table
*/
public static function saveSchedule(array $request_data){ public static function saveSchedule(array $request_data){
$task = TaskScheduler::find($request_data['id']); $task = TaskScheduler::find($request_data['id']);
if(isset($request_data['expression'])){
$task->expression = $request_data['expression'];
}
if(isset($request_data['enable'])){ if(isset($request_data['enable'])){
$task->enable = $request_data['enable']; $task->enable = $request_data['enable'];
} }
if(isset($request_data['startingTime'])){
if(isset($request_data['expression'])){
$task->expression = $request_data['expression'];
$task->startingTime = $request_data['startingTime']; $task->startingTime = $request_data['startingTime'];
}
if(isset($request_data['endingTime'])){
$task->endingTime = $request_data['endingTime']; $task->endingTime = $request_data['endingTime'];
}
if(isset($request_data['timezone'])){
$task->timezone = $request_data['timezone']; $task->timezone = $request_data['timezone'];
$task->everyOn = $request_data['everyOn'];
$task->interval = $request_data['interval'];
} }
$task->save(); $task->save();
return array(); return array();
@@ -206,22 +239,6 @@ class TaskSchedulerBM
"startingTime" => "0:00", "startingTime" => "0:00",
"endingTime" => "23:59", "endingTime" => "23:59",
"expression" => "* * */1 * *", "expression" => "* * */1 * *",
"description" => "ID_TASK_SCHEDULER_LDAP_DESC"
),
array(
"title" => "ID_TASK_SCHEDULER_SEND_NOT",
"enable" => "1",
"service" => "",
"category" => "emails_notifications",
"file" => "workflow/engine/bin/sendnotificationscron.php",
"startingTime" => "0:00",
"endingTime" => "23:59",
"expression" => "*/5 * * * *",
"description" => "ID_TASK_SCHEDULER_SEND_NOT_DESC"
),
array(
"title" => "ID_TASK_SCHEDULER_ACTION_EMAIL",
"enable" => "1",
"service" => "", "service" => "",
"category" => "emails_notifications", "category" => "emails_notifications",
"file" => "workflow/engine/bin/actionsByEmailEmailResponse.php", "file" => "workflow/engine/bin/actionsByEmailEmailResponse.php",

View File

@@ -20,9 +20,4 @@ class TaskScheduler extends Model
public $timestamps = true; public $timestamps = true;
const CREATED_AT = 'creation_date'; const CREATED_AT = 'creation_date';
const UPDATED_AT = 'last_update'; const UPDATED_AT = 'last_update';
public function isDue(){
$date = Carbon::now();
return CronExpression::factory($this->expression)->isDue($date->toDateTimeString());
}
} }