124 lines
3.7 KiB
PHP
124 lines
3.7 KiB
PHP
<?php
|
|
/**
|
|
* FuzeWorks Component.
|
|
*
|
|
* The FuzeWorks PHP FrameWork
|
|
*
|
|
* Copyright (C) 2013-2019 TechFuze
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
* @author TechFuze
|
|
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
|
*
|
|
* @link http://techfuze.net/fuzeworks
|
|
* @since Version 1.2.0
|
|
*
|
|
* @version Version 1.2.0
|
|
*/
|
|
|
|
namespace FuzeWorks\Async\Constraint;
|
|
use FuzeWorks\Async\Constraint;
|
|
use FuzeWorks\Async\Task;
|
|
|
|
/**
|
|
* Class GroupConstraint
|
|
*
|
|
* Use this constraint to maximize the amount of concurrent tasks within a certain group.
|
|
* If the amount of running tasks equals the maximum concurrent tasks, this constraint will issue a delay.
|
|
* Useful when multiple tasks are not dependent on each other, but should be run separately.
|
|
*
|
|
* @package FuzeWorks\Async\Constraint
|
|
*/
|
|
class GroupConstraint implements Constraint
|
|
{
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $groupName;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
protected $maxConcurrent = 3;
|
|
|
|
protected $returnStatus = Task::DELAYED;
|
|
|
|
/**
|
|
* GroupConstraint constructor.
|
|
*
|
|
* To add a group, simply provide the $groupName and the maximum of concurrent tasks within this group.
|
|
*
|
|
* @param string $groupName
|
|
* @param int $maxConcurrent
|
|
*/
|
|
public function __construct(string $groupName, int $maxConcurrent = 3)
|
|
{
|
|
$this->groupName = $groupName;
|
|
$this->maxConcurrent = $maxConcurrent;
|
|
}
|
|
|
|
public function init(Task $task)
|
|
{
|
|
$task->addAttribute('groupName', $this->groupName);
|
|
}
|
|
|
|
public function getGroupName(): string
|
|
{
|
|
return $this->groupName;
|
|
}
|
|
|
|
public function intervene(Task $task, array $tasks): bool
|
|
{
|
|
// Find all tasks within this group and that are running
|
|
$runningTasks = 0;
|
|
foreach ($tasks as $t)
|
|
{
|
|
foreach ($t->getConstraints() as $constraint)
|
|
{
|
|
if ($constraint instanceof GroupConstraint && $this->groupName === $constraint->getGroupName())
|
|
{
|
|
if ($t->getStatus() === Task::RUNNING || $t->getStatus() === Task::POST)
|
|
$runningTasks++;
|
|
}
|
|
}
|
|
|
|
// If the running tasks exceeds the maximum, block this task from running
|
|
if ($runningTasks >= $this->maxConcurrent)
|
|
{
|
|
$this->returnStatus = Task::DELAYED;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function blockCode(): int
|
|
{
|
|
return $this->returnStatus;
|
|
}
|
|
|
|
public function delayTime(): int
|
|
{
|
|
return time() + 1;
|
|
}
|
|
} |