Skip to content

Commit

Permalink
Merge pull request #116 from bellorchid/feature/scheduler-add-process…
Browse files Browse the repository at this point in the history
…_max_time

增加执行最大时间限制
  • Loading branch information
canuonifeng committed Jul 2, 2018
2 parents ebb7942 + bdc3bf5 commit 54aca87
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Provider/SchedulerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function register(Container $biz)
$biz['autoload.aliases']['Scheduler'] = 'Codeages\Biz\Framework\Scheduler';

$biz['scheduler.options'] = array(
'max_process_exec_time' => 600,
'max_num' => 10,
'timeout' => 120,
'max_retry_num' => 5,
Expand Down
14 changes: 11 additions & 3 deletions src/Scheduler/Service/Impl/SchedulerServiceImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ public function execute()
$this->updateWaitingJobsToAcquired();
do {
$result = $this->runAcquiredJobs($initProcess['id']);
$peak_memory = !function_exists('memory_get_peak_usage') ? 0 : memory_get_peak_usage();
} while ($result && $peak_memory < SchedulerService::JOB_MEMORY_LIMIT);
$peakMemory = !function_exists('memory_get_peak_usage') ? 0 : memory_get_peak_usage();
$currentTime = $this->getMillisecond();
$processUsedTime = (int) (($currentTime - $process['start_time']) / 1000);
} while ($result && $peakMemory < SchedulerService::JOB_MEMORY_LIMIT && $processUsedTime < $this->getMaxProcessExecTime());
$process['end_time'] = $this->getMillisecond();
$process['cost_time'] = $process['end_time'] - $process['start_time'];
$process['peak_memory'] = $peak_memory;
$process['peak_memory'] = $peakMemory;

$this->updateJobProcess($initProcess['id'], $process);
}
Expand Down Expand Up @@ -464,6 +466,7 @@ public function updateJobProcess($id, $process)
public function updateJob($id, $fields)
{
$fields = ArrayToolkit::parts($fields, array('args', 'priority', 'pre_fire_time', 'next_fire_time', ' misfire_threshold', 'misfire_policy', 'enabled'));

return $this->getJobDao()->update($id, $fields);
}

Expand Down Expand Up @@ -528,4 +531,9 @@ protected function getMaxRetryNum()
{
return $this->biz['scheduler.options']['max_retry_num'];
}

protected function getMaxProcessExecTime()
{
return $this->biz['scheduler.options']['max_process_exec_time'];
}
}

0 comments on commit 54aca87

Please sign in to comment.