| Total Complexity | 61 |
| Total Lines | 478 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like WorkerModelsEvents often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WorkerModelsEvents, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 84 | class WorkerModelsEvents extends WorkerBase |
||
| 85 | { |
||
| 86 | private int $last_change; |
||
| 87 | |||
| 88 | // Array of planned reload actions that need to be started |
||
| 89 | private array $plannedReloadActions = []; |
||
| 90 | |||
| 91 | private int $timeout = 5; |
||
| 92 | |||
| 93 | // Array of core conf objects |
||
| 94 | private array $arrAsteriskConfObjects; |
||
| 95 | |||
| 96 | // Array of reload actions sorted by its priority |
||
| 97 | private array $reloadActions = []; |
||
| 98 | |||
| 99 | private array $otherModelsDependencyTable = []; |
||
| 100 | private array $pbxSettingsDependencyTable = []; |
||
| 101 | private array $customFilesDependencyTable = []; |
||
| 102 | |||
| 103 | private BeanstalkClient $beanstalkClient; |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Starts the model events worker. |
||
| 108 | * |
||
| 109 | * This method initializes the worker, subscribes to necessary events, and enters a loop waiting for these events. |
||
| 110 | * It acts as the main entry point for the worker's lifecycle. |
||
| 111 | * |
||
| 112 | * @param array $argv The command-line arguments passed to the worker. |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function start(array $argv): void |
||
| 116 | { |
||
| 117 | $this->initializeWorker(); |
||
| 118 | $this->subscribeToEvents(); |
||
| 119 | $this->waitForEvents(); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Initializes the worker by setting up initial state, loading configurations, and preparing dependencies. |
||
| 124 | * |
||
| 125 | * It sets the last change time, gets shared instances from the DI container, initializes PBX settings and model dependency tables, |
||
| 126 | * and sets up priority actions for reload. |
||
| 127 | * |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | private function initializeWorker(): void |
||
| 131 | { |
||
| 132 | |||
| 133 | $this->beanstalkClient = $this->getBeanstalkClient(); |
||
| 134 | |||
| 135 | $this->last_change = time() - $this->timeout; |
||
| 136 | |||
| 137 | // Array of core conf objects |
||
| 138 | $this->arrAsteriskConfObjects = $this->di->getShared(AsteriskConfModulesProvider::SERVICE_NAME); |
||
| 139 | |||
| 140 | // Initializes the PBX settings model dependency table. |
||
| 141 | $this->pbxSettingsDependencyTable = ProcessPBXSettings::getDependencyTable(); |
||
| 142 | |||
| 143 | // Initializes the custom files models dependency table. |
||
| 144 | $this->customFilesDependencyTable = ProcessCustomFiles::getDependencyTable(); |
||
| 145 | |||
| 146 | // Initializes the models dependency table. |
||
| 147 | $this->otherModelsDependencyTable = ProcessOtherModels::getDependencyTable(); |
||
| 148 | |||
| 149 | // Initializes the possible reload actions table. |
||
| 150 | $this->reloadActions = $this->getReloadActionsWithPriority(); |
||
| 151 | |||
| 152 | $this->plannedReloadActions = []; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Create BeanstalkClient connection |
||
| 157 | * @return BeanstalkClient |
||
| 158 | */ |
||
| 159 | private function getBeanstalkClient(): BeanstalkClient |
||
| 160 | { |
||
| 161 | $di = Di::getDefault(); |
||
| 162 | if (!$di) { |
||
| 163 | throw new RuntimeException("Dependency Injection container is not set."); |
||
| 164 | } |
||
| 165 | return $di->getShared(BeanstalkConnectionModelsProvider::SERVICE_NAME); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get priority reload actions |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | private function getReloadActionsWithPriority(): array |
||
| 173 | { |
||
| 174 | return [ |
||
| 175 | ReloadModuleStateAction::class, |
||
| 176 | ReloadTimezoneAction::class, |
||
| 177 | ReloadSyslogDAction::class, |
||
| 178 | ReloadRestAPIWorkerAction::class, |
||
| 179 | ReloadNetworkAction::class, |
||
| 180 | ReloadFirewallAction::class, |
||
| 181 | ReloadFail2BanConfAction::class, |
||
| 182 | ReloadSSHAction::class, |
||
| 183 | ReloadLicenseAction::class, |
||
| 184 | ReloadSentryAction::class, |
||
| 185 | ReloadNatsAction::class, |
||
| 186 | ReloadNTPAction::class, |
||
| 187 | ReloadPHPFPMAction::class, |
||
| 188 | ReloadNginxAction::class, |
||
| 189 | ReloadNginxConfAction::class, |
||
| 190 | ReloadCrondAction::class, |
||
| 191 | RestartPBXCoreAction::class, |
||
| 192 | ReloadPBXCoreAction::class, |
||
| 193 | ReloadModulesConfAction::class, |
||
| 194 | ReloadFeaturesAction::class, |
||
| 195 | ReloadPJSIPAction::class, |
||
| 196 | ReloadRTPAction::class, |
||
| 197 | ReloadIAXAction::class, |
||
| 198 | ReloadH323Action::class, |
||
| 199 | ReloadHepAction::class, |
||
| 200 | ReloadDialplanAction::class, |
||
| 201 | ReloadParkingAction::class, |
||
| 202 | ReloadQueuesAction::class, |
||
| 203 | ReloadManagerAction::class, |
||
| 204 | ReloadVoicemailAction::class, |
||
| 205 | ReloadMOHAction::class, |
||
| 206 | ReloadWorkerCallEventsAction::class, |
||
| 207 | ReloadRecordSavePeriodAction::class, |
||
| 208 | ReloadAdviceAction::class, |
||
| 209 | ReloadCloudDescriptionAction::class, |
||
| 210 | ReloadCloudParametersAction::class |
||
| 211 | ]; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Subscribes the worker to relevant Beanstalk queues for processing model changes and handling pings. |
||
| 216 | * |
||
| 217 | * It ensures that the worker listens for incoming messages related to model changes and system pings, |
||
| 218 | * setting up appropriate callbacks for each. |
||
| 219 | * |
||
| 220 | * @return void |
||
| 221 | */ |
||
| 222 | private function subscribeToEvents(): void |
||
| 223 | { |
||
| 224 | $this->beanstalkClient->subscribe(self::class, [$this, 'processModelChanges']); |
||
| 225 | $this->beanstalkClient->subscribe($this->makePingTubeName(self::class), [$this, 'pingCallBack']); |
||
| 226 | $this->beanstalkClient->setTimeoutHandler([$this, 'timeoutHandler']); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Waits for events in a loop until a restart condition is met. |
||
| 231 | * |
||
| 232 | * This method keeps the worker in a loop, processing incoming events from the Beanstalk queue. |
||
| 233 | * The loop continues until an external condition triggers the need to restart the worker. |
||
| 234 | * |
||
| 235 | * @return void |
||
| 236 | */ |
||
| 237 | private function waitForEvents(): void |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Timeout handles |
||
| 247 | */ |
||
| 248 | public function timeoutHandler(): void |
||
| 249 | { |
||
| 250 | $this->last_change = time() - $this->timeout; |
||
| 251 | $this->startReload(); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Starts the reload process if there are modified tables. |
||
| 256 | * |
||
| 257 | * @return void |
||
| 258 | */ |
||
| 259 | private function startReload(): void |
||
| 260 | { |
||
| 261 | // Check if there aren't any planned reload actions |
||
| 262 | if (count($this->plannedReloadActions) === 0) { |
||
| 263 | SystemMessages::sysLogMsg(__METHOD__, "No planed actions for reload", LOG_DEBUG); |
||
| 264 | return; |
||
| 265 | } |
||
| 266 | |||
| 267 | // Check if enough time has passed since the last change |
||
| 268 | $delta = time() - $this->last_change; |
||
| 269 | if ($delta < $this->timeout) { |
||
| 270 | SystemMessages::sysLogMsg(__METHOD__, "Wait more time before starting the reload.", LOG_DEBUG); |
||
| 271 | return; |
||
| 272 | } |
||
| 273 | |||
| 274 | $executedActions = []; |
||
| 275 | // Process changes for each method in priority order |
||
| 276 | foreach ($this->reloadActions as $actionClassName) { |
||
| 277 | // Skip if there is no change for this method |
||
| 278 | if (!array_key_exists($actionClassName, $this->plannedReloadActions)) { |
||
| 279 | continue; |
||
| 280 | } |
||
| 281 | // Call the method if it exists |
||
| 282 | try { |
||
| 283 | $parameters = $this->plannedReloadActions[$actionClassName]['parameters']; |
||
| 284 | $hashes = array_keys($parameters); |
||
| 285 | SystemMessages::sysLogMsg($actionClassName, "Start action for the next parameters hashes: ".PHP_EOL . json_encode($hashes, JSON_PRETTY_PRINT), LOG_DEBUG); |
||
| 286 | |||
| 287 | $actionObject = new $actionClassName(); |
||
| 288 | $actionObject->execute($parameters); |
||
| 289 | $executedActions[] = $actionClassName; |
||
| 290 | } catch (Throwable $exception) { |
||
| 291 | CriticalErrorsHandler::handleExceptionWithSyslog($exception); |
||
| 292 | } |
||
| 293 | |||
| 294 | } |
||
| 295 | if (count($executedActions)>0){ |
||
| 296 | SystemMessages::sysLogMsg(__METHOD__, "Reload actions were executed in the next order: ".PHP_EOL . json_encode($executedActions, JSON_PRETTY_PRINT), LOG_DEBUG); |
||
| 297 | } |
||
| 298 | |||
| 299 | // Send information about models changes to additional modules bulky without any details |
||
| 300 | PBXConfModulesProvider::hookModulesMethod(SystemConfigInterface::MODELS_EVENT_NEED_RELOAD, [$this->plannedReloadActions]); |
||
| 301 | |||
| 302 | // Reset the modified tables array |
||
| 303 | $this->plannedReloadActions = []; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Processes model changes received from the Beanstalk queue. |
||
| 308 | * |
||
| 309 | * @param BeanstalkClient $message The message received from the Beanstalk queue. |
||
| 310 | * @return void |
||
| 311 | */ |
||
| 312 | public function processModelChanges(BeanstalkClient $message): void |
||
| 313 | { |
||
| 314 | try { |
||
| 315 | // Decode the received message |
||
| 316 | $receivedMessage = json_decode($message->getBody(), true, 512, JSON_THROW_ON_ERROR); |
||
| 317 | |||
| 318 | // Check the source of the message and perform actions accordingly |
||
| 319 | if ($receivedMessage['source'] === BeanstalkConnectionModelsProvider::SOURCE_INVOKE_ACTION |
||
| 320 | && in_array($receivedMessage['action'], $this->reloadActions)) { |
||
| 321 | // Store the modified table and its parameters |
||
| 322 | $this->planReloadAction($receivedMessage['action'], $receivedMessage['parameters']); |
||
| 323 | |||
| 324 | } elseif ($receivedMessage['source'] === BeanstalkConnectionModelsProvider::SOURCE_MODELS_CHANGED) { |
||
| 325 | |||
| 326 | // Fill the modified tables array with the changes from the received message |
||
| 327 | $this->fillModifiedTables($receivedMessage); |
||
| 328 | } |
||
| 329 | |||
| 330 | // Start the reload process if there are modified tables |
||
| 331 | $this->startReload(); |
||
| 332 | |||
| 333 | if (!$receivedMessage) { |
||
| 334 | return; |
||
| 335 | } |
||
| 336 | |||
| 337 | // Send information about model changes to additional modules with changed data details |
||
| 338 | PBXConfModulesProvider::hookModulesMethod(SystemConfigInterface::MODELS_EVENT_CHANGE_DATA, [$receivedMessage]); |
||
| 339 | } catch (Throwable $exception) { |
||
| 340 | $this->needRestart = true; |
||
| 341 | CriticalErrorsHandler::handleExceptionWithSyslog($exception); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Fills the modified tables array with changes based on the received data. |
||
| 347 | * |
||
| 348 | * @param array $data The data containing the changes. |
||
| 349 | * @return void |
||
| 350 | */ |
||
| 351 | private function fillModifiedTables(array $data): void |
||
| 352 | { |
||
| 353 | $countPlannedActions = count($this->plannedReloadActions); |
||
| 354 | $modifiedModel = $data['model'] ?? ''; |
||
| 355 | if (empty($modifiedModel)){ |
||
| 356 | return; |
||
| 357 | } |
||
| 358 | |||
| 359 | SystemMessages::sysLogMsg(__METHOD__, "New changes received:".PHP_EOL . json_encode($data, JSON_PRETTY_PRINT), LOG_DEBUG); |
||
| 360 | |||
| 361 | // Clear cache for the called class |
||
| 362 | ModelsBase::clearCache($modifiedModel); |
||
| 363 | |||
| 364 | // Get new settings for dependent modules |
||
| 365 | $this->getNewSettingsForDependentModules($modifiedModel); |
||
| 366 | |||
| 367 | // Plan new reload actions |
||
| 368 | $this->planReloadActionsForCustomFiles($modifiedModel, $data); |
||
| 369 | $this->planReloadActionsForPbxSettings($modifiedModel, $data); |
||
| 370 | $this->planReloadActionsForOtherModels($modifiedModel, $data); |
||
| 371 | |||
| 372 | // Start counting time when the new reload actions were received |
||
| 373 | if ($countPlannedActions === 0 && count($this->plannedReloadActions) > 0) { |
||
| 374 | $this->last_change = time(); |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Retrieves new settings for dependent modules based on the called class. |
||
| 380 | * |
||
| 381 | * @param string $modifiedModel The called class for which to retrieve settings. |
||
| 382 | * @return void |
||
| 383 | */ |
||
| 384 | private function getNewSettingsForDependentModules(string $modifiedModel): void |
||
| 385 | { |
||
| 386 | foreach ($this->arrAsteriskConfObjects as $configClassObj) { |
||
| 387 | try { |
||
| 388 | $dependencies = call_user_func([$configClassObj, AsteriskConfigInterface::GET_DEPENDENCE_MODELS]); |
||
| 389 | // Check if the called class is a dependency and the config class has the GET_SETTINGS method |
||
| 390 | if (in_array($modifiedModel, $dependencies, true) && method_exists($configClassObj, AsteriskConfigInterface::GET_SETTINGS)) { |
||
| 391 | // Retrieve the new settings for the config class |
||
| 392 | call_user_func([$configClassObj, AsteriskConfigInterface::GET_SETTINGS]); |
||
| 393 | } |
||
| 394 | } catch (Throwable $e) { |
||
| 395 | CriticalErrorsHandler::handleExceptionWithSyslog($e); |
||
| 396 | continue; |
||
| 397 | } |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Fills the modified tables array based on the models dependency table and the called class. |
||
| 403 | * |
||
| 404 | * @param string $called_class The called model class. |
||
| 405 | * @param array $modelData Data received during model change. |
||
| 406 | * @return void |
||
| 407 | */ |
||
| 408 | private function planReloadActionsForOtherModels(string $modifiedModel, array $modelData): void |
||
| 409 | { |
||
| 410 | foreach ($this->otherModelsDependencyTable as $dependencyData) { |
||
| 411 | if (!in_array($modifiedModel, $dependencyData['modelClasses'], true)) { |
||
| 412 | continue; |
||
| 413 | } |
||
| 414 | foreach ($dependencyData['actions'] as $action) { |
||
| 415 | $this->planReloadAction($action, $modelData); |
||
| 416 | } |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Fills the modified tables array based on the custom files data, the called class, and the record ID. |
||
| 422 | * |
||
| 423 | * @param string $modifiedModel The modified model class (Must be CustomFiles) |
||
| 424 | * @param array $modelData Data received during model change. |
||
| 425 | * @return void |
||
| 426 | */ |
||
| 427 | private function planReloadActionsForCustomFiles(string $modifiedModel, array $modelData): void |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Add new reload action with parameters to the planned reload actions array. |
||
| 455 | * |
||
| 456 | * @param string $action The name of the action to be executed. |
||
| 457 | * @param array $parameters The parameters to be passed to the action. |
||
| 458 | * @return void |
||
| 459 | */ |
||
| 460 | private function planReloadAction(string $action, array $parameters = []): void |
||
| 473 | } |
||
| 474 | } |
||
| 475 | |||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Fills the modified tables array based on the PBX settings data, the called class, and the record ID. |
||
| 480 | * |
||
| 481 | * @param string $modifiedModel The modified model class (Must be PbxSettings) |
||
| 482 | * @param array $modelData Data received during model change. |
||
| 483 | * @return void |
||
| 484 | */ |
||
| 485 | private function planReloadActionsForPbxSettings(string $modifiedModel, array $modelData): void |
||
| 486 | { |
||
| 487 | // Check if the called class is not PbxSettings |
||
| 488 | if (PbxSettings::class !== $modifiedModel || empty($modelData['recordId'])) { |
||
| 489 | return; |
||
| 490 | } |
||
| 491 | |||
| 492 | // Clear cache for PbxSettings |
||
| 493 | PbxSettings::clearCache(PbxSettings::class); |
||
| 494 | |||
| 495 | // Find the PbxSettings record |
||
| 496 | /** @var PbxSettings $pbxSettings */ |
||
| 497 | $pbxSettings = PbxSettings::findFirstByKey($modelData['recordId']); |
||
| 498 | if ($pbxSettings === null) { |
||
| 499 | return; |
||
| 500 | } |
||
| 501 | $key = $pbxSettings->key; |
||
| 502 | |||
| 503 | // Iterate through the PBX settings dependency table and update the modified tables array |
||
| 504 | foreach ($this->pbxSettingsDependencyTable as $data) { |
||
| 505 | $additionalConditions = (isset($data['strPosKey']) && strpos($key, $data['strPosKey']) !== false); |
||
| 506 | |||
| 507 | // Check additional conditions and the setting name |
||
| 508 | if (!$additionalConditions && !in_array($key, $data['keys'], true)) { |
||
| 509 | continue; |
||
| 510 | } |
||
| 511 | |||
| 512 | // Update the modified tables array for each function |
||
| 513 | foreach ($data['actions'] as $action) { |
||
| 514 | $this->planReloadAction($action, $modelData); |
||
| 515 | } |
||
| 516 | } |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Invokes an action by publishing a job to the Beanstalk queue. |
||
| 521 | * |
||
| 522 | * @param string $action The action to invoke. |
||
| 523 | * @param array $parameters The parameters for the action. |
||
| 524 | * @param int $priority The priority of the job. |
||
| 525 | * @return void |
||
| 526 | */ |
||
| 527 | public static function invokeAction(string $action, array $parameters = [], int $priority = 0): void |
||
| 528 | { |
||
| 529 | $di = Di::getDefault(); |
||
| 530 | if (!$di) { |
||
| 531 | return; |
||
| 532 | } |
||
| 533 | /** @var BeanstalkClient $queue */ |
||
| 534 | $queue = $di->getShared(BeanstalkConnectionModelsProvider::SERVICE_NAME); |
||
| 535 | |||
| 536 | // Prepare the job data |
||
| 537 | $jobData = json_encode(['source' => BeanstalkConnectionModelsProvider::SOURCE_INVOKE_ACTION, 'action' => $action, 'parameters' => $parameters, 'model' => '']); |
||
| 538 | // Publish the job to the Beanstalk queue |
||
| 539 | $queue->publish($jobData, self::class, $priority, PheanstalkInterface::DEFAULT_DELAY, 3600); |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Callback for the ping to keep the connection alive. |
||
| 544 | * |
||
| 545 | * @param BeanstalkClient $message The received message. |
||
| 546 | * |
||
| 547 | * @return void |
||
| 548 | */ |
||
| 549 | public function pingCallBack(BeanstalkClient $message): void |
||
| 550 | { |
||
| 551 | // Start the reload process if there are modified tables |
||
| 552 | $this->startReload(); |
||
| 553 | $message->reply(json_encode($message->getBody() . ':pong')); |
||
| 554 | } |
||
| 555 | |||
| 556 | private function createUniqueKeyFromArray(array $array) { |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * The start point |
||
| 567 | */ |
||
| 568 | WorkerModelsEvents::startWorker($argv ?? []); |