ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilTestProcessLockerFile.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30{
31 public const PROCESS_NAME_TEST_START_LOCK_CHECK = 'testStartLockCheck';
32 public const PROCESS_NAME_RANDOM_PASS_BUILD = 'randomPassBuild';
33 public const PROCESS_NAME_TEST_FINISH = 'testFinish';
34
38 protected $lockFileHandles = [];
39
44 public function __construct(
45 private readonly ilTestProcessLockFileStorage $lockFileStorage,
46 private readonly ilLogger|TestLogger $logger
47 ) {
48 }
49
54 {
55 parent::onBeforeExecutingTestStartOperation();
56 $this->requestLock(self::PROCESS_NAME_TEST_START_LOCK_CHECK);
57 }
58
63 {
64 $this->releaseLock(self::PROCESS_NAME_TEST_START_LOCK_CHECK);
65 parent::onAfterExecutingTestStartOperation();
66 }
67
71 protected function onBeforeExecutingRandomPassBuildOperation($withTaxonomyTables = false)
72 {
73 parent::onBeforeExecutingRandomPassBuildOperation($withTaxonomyTables);
74 $this->requestLock(self::PROCESS_NAME_RANDOM_PASS_BUILD);
75 }
76
80 protected function onAfterExecutingRandomPassBuildOperation($withTaxonomyTables = false)
81 {
82 $this->releaseLock(self::PROCESS_NAME_RANDOM_PASS_BUILD);
83 parent::onAfterExecutingRandomPassBuildOperation($withTaxonomyTables);
84 }
85
90 {
91 parent::onBeforeExecutingTestStartOperation();
92 $this->requestLock(self::PROCESS_NAME_TEST_FINISH);
93 }
94
99 {
100 $this->releaseLock(self::PROCESS_NAME_TEST_FINISH);
101 parent::onAfterExecutingTestStartOperation();
102 }
103
104 protected function onBeforeExecutingNamedOperation(string $operationDescriptor): void
105 {
106 $this->requestLock($operationDescriptor);
107 parent::onBeforeExecutingNamedOperation($operationDescriptor);
108 }
109
110 protected function onAfterExecutingNamedOperation(string $operationDescriptor): void
111 {
112 $this->releaseLock($operationDescriptor);
113 parent::onAfterExecutingNamedOperation($operationDescriptor);
114 }
115
116 private function requestLock($processName)
117 {
118 $lockFilePath = $this->getLockFilePath($processName);
119 $this->lockFileHandles[$processName] = fopen($lockFilePath, 'w');
120 if (!flock($this->lockFileHandles[$processName], LOCK_EX)) {
121 $this->logger->error("Flock failed for {$lockFilePath}.");
122 }
123 }
124
125 private function getLockFilePath($processName): string
126 {
127 $path = $this->lockFileStorage->getAbsolutePath();
128 return $path . '/' . $processName . '.lock';
129 }
130
131 private function releaseLock($processName)
132 {
133 flock($this->lockFileHandles[$processName], LOCK_UN);
134 fclose($this->lockFileHandles[$processName]);
135 }
136}
Component logger with individual log levels by component id.
__construct(private readonly ilTestProcessLockFileStorage $lockFileStorage, private readonly ilLogger|TestLogger $logger)
ilTestProcessLockerFile constructor.
onBeforeExecutingNamedOperation(string $operationDescriptor)
onAfterExecutingRandomPassBuildOperation($withTaxonomyTables=false)
{}
onBeforeExecutingRandomPassBuildOperation($withTaxonomyTables=false)
{}
onAfterExecutingNamedOperation(string $operationDescriptor)
$path
Definition: ltiservices.php:30