supernova-ws /
SuperNova
| 1 | <?php |
||
| 2 | /** Created by Gorlum 27.10.2024 18:43 */ |
||
| 3 | |||
| 4 | namespace Fleet; |
||
| 5 | |||
| 6 | use Core\Scheduler\Lock; |
||
| 7 | use SN; |
||
| 8 | |||
| 9 | class FleetWatchdog { |
||
| 10 | /** @var $workBegin */ |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 11 | public static $workBegin; |
||
| 12 | |||
| 13 | |||
| 14 | /** @var float $eventStartedAt */ |
||
| 15 | public static $eventStartedAt = SN_TIME_NOW; |
||
| 16 | // /** @var float $lastEventEnd */ |
||
| 17 | // public static $lastEventEnd = SN_TIME_NOW; |
||
| 18 | |||
| 19 | /** @var int $currentMission */ |
||
| 20 | public static $currentMission = MT_NONE; |
||
| 21 | /** @var int $eventsProcessed */ |
||
| 22 | public static $eventsProcessed = 0; |
||
| 23 | /** @var int $currentEvent */ |
||
| 24 | public static $currentEvent = EVENT_FLEET_NONE; |
||
| 25 | |||
| 26 | const TASK_COMPLETE = 0; |
||
| 27 | const CONTINUE_EXECUTION = 2; |
||
| 28 | const TASK_TOO_LONG = 1; |
||
| 29 | const TASK_ALREADY_LOCKED = -1; |
||
| 30 | const LOCK_ACQUIRED = -2; |
||
| 31 | const FLEET_IS_EMPTY = 'FLEET_EMPTY'; |
||
| 32 | |||
| 33 | const EVENT_DISPATCH_STARTED = 'EVENT_DISPATCH_STARTED'; |
||
| 34 | /** @var Lock $runLock */ |
||
| 35 | private static $runLock; |
||
| 36 | |||
| 37 | |||
| 38 | public function __construct() { |
||
| 39 | // Dispatch started |
||
| 40 | self::$workBegin = microtime(true); |
||
| 41 | |||
| 42 | self::$eventStartedAt = self::$workBegin; |
||
|
0 ignored issues
–
show
It seems like
self::workBegin can also be of type string. However, the property $eventStartedAt is declared as type double. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||
| 43 | self::$currentMission = MT_NONE; |
||
| 44 | self::$currentEvent = EVENT_FLEET_NONE; |
||
| 45 | self::$eventsProcessed = 0; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @return int |
||
| 50 | */ |
||
| 51 | public function acquireLock() { |
||
| 52 | // Trying to acquire lock for current task |
||
| 53 | self::$runLock = $runLock = SN::$gc->fleetDispatcher->buildLock(); |
||
| 54 | if (!$runLock->attemptLock()) { |
||
| 55 | return self::TASK_ALREADY_LOCKED; |
||
| 56 | } |
||
| 57 | |||
| 58 | register_shutdown_function(function () use ($runLock) { |
||
| 59 | // $this->log_file('Shutting down'); |
||
| 60 | $timeLock = $runLock->isLocked(); |
||
| 61 | if ($timeLock > 0 || $timeLock === 0) { |
||
| 62 | $runLock->unLock(true); |
||
| 63 | $this->logTermination(); |
||
| 64 | // $this->log_file('UNLOCKING'); |
||
| 65 | } |
||
| 66 | // $this->log_file(SN::$gc->config->pass()->fleet_update_run_lock); |
||
| 67 | // $this->log_file('ALL RELEASED'); |
||
| 68 | }); |
||
| 69 | |||
| 70 | return self::LOCK_ACQUIRED; |
||
| 71 | } |
||
| 72 | |||
| 73 | public function begin(FleetDispatchEvent $fleetEvent) { |
||
| 74 | // Watchdog timer |
||
| 75 | // If flying fleet handler works more than `fleet_update_dispatch_time` seconds - stopping it |
||
| 76 | // Let next run handle rest of fleets |
||
| 77 | // var_dump(microtime(true), self::$workBegin, (microtime(true) - self::$workBegin) >= SN::$config->fleet_update_dispatch_time, $fleetEvent->fleet); |
||
| 78 | if ((microtime(true) - self::$workBegin) >= SN::$config->fleet_update_dispatch_time) { |
||
| 79 | $this->logTermination(); |
||
| 80 | |||
| 81 | return FleetWatchdog::TASK_TOO_LONG; |
||
| 82 | } |
||
| 83 | |||
| 84 | if (empty($fleetEvent->fleet)) { |
||
| 85 | // Fleet was destroyed in course of previous actions |
||
| 86 | return FleetWatchdog::FLEET_IS_EMPTY; |
||
| 87 | } |
||
| 88 | |||
| 89 | self::$eventStartedAt = microtime(true); |
||
|
0 ignored issues
–
show
It seems like
microtime(true) can also be of type string. However, the property $eventStartedAt is declared as type double. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||
| 90 | self::$currentMission = !empty($fleetEvent->fleet[FleetDispatcher::F_FLEET_MISSION]) ? $fleetEvent->fleet[FleetDispatcher::F_FLEET_MISSION] : MT_NONE; |
||
| 91 | self::$currentEvent = !empty($fleetEvent->event) ? $fleetEvent->event : MT_NONE; |
||
|
0 ignored issues
–
show
It seems like
! empty($fleetEvent->eve...->event : Fleet\MT_NONE can also be of type string. However, the property $currentEvent is declared as type integer. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||
| 92 | self::$eventsProcessed++; |
||
| 93 | |||
| 94 | return FleetWatchdog::CONTINUE_EXECUTION; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | */ |
||
| 99 | public function logTermination() { |
||
| 100 | SN::$debug->warning( |
||
| 101 | $this->getTerminationMessage(), |
||
| 102 | 'FFH Warning', |
||
| 103 | 504 |
||
| 104 | ); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function unlock() { |
||
| 108 | self::$runLock->unLock(true); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function getTerminationMessage() { |
||
| 115 | return sprintf( |
||
| 116 | 'Flying fleet handler works %1$s seconds (> %2$s) - skipping rest. Processed %3$d / %7$d events. Last event: mission %4$s event %6$s (%5$ss)', |
||
| 117 | number_format(microtime(true) - self::$workBegin, 4), |
||
| 118 | SN::$config->fleet_update_dispatch_time, |
||
| 119 | self::$eventsProcessed, |
||
| 120 | !empty(SN::$lang['type_mission'][self::$currentMission]) ? SN::$lang['type_mission'][self::$currentMission] : '!TERMINATED BY TIMEOUT!', |
||
| 121 | number_format(microtime(true) - self::$eventStartedAt, 4), |
||
| 122 | !empty(SN::$lang['fleet_events'][self::$currentEvent]) ? SN::$lang['fleet_events'][self::$currentEvent] : '!TERMINATED BY TIMEOUT!', |
||
| 123 | count(FleetDispatcher::$fleet_event_list) |
||
| 124 | ); |
||
| 125 | } |
||
| 126 | |||
| 127 | } |
||
| 128 |