Skip to content

Commit db8162d

Browse files
authored
Merge pull request #3 from DevDavido/development
1.0.2
2 parents c6cdae2 + 9be309a commit db8162d

File tree

7 files changed

+90
-40
lines changed

7 files changed

+90
-40
lines changed

.php_cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ return PhpCsFixer\Config::create()
2525
'unary_operator_spaces' => true,
2626
'binary_operator_spaces' => true,
2727
'blank_line_before_statement' => [
28-
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
28+
'statements' => ['break', 'continue', 'declare'],
2929
],
3030
'braces' => false,
3131
'phpdoc_single_line_var_spacing' => true,

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.0.2
4+
- Fixed: Regression bug which made only first website get audited
5+
- Improved: Logging of exceptions for audits
6+
- Updated: Bumped `symfony/polyfill-mbstring` dependency version
7+
38
## 1.0.1
49
- Fixed: Made sure tasks aren't executed concurrently
510
- Fixed: composer.json is compatible with Composer v2 now

Tasks.php

Lines changed: 74 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,41 +67,52 @@ public function schedule()
6767
*/
6868
public function auditSite(int $idSite)
6969
{
70-
if ($this->hasAnyTaskStartedToday()) {
71-
Log::info('Performance Audit tasks have been already started today');
70+
if ($this->hasAnyTaskRunning()) {
71+
Log::info('A Performance Audit task is currently already running');
7272
return;
7373
}
74+
if ($this->hasTaskStartedToday($idSite)) {
75+
Log::info('Performance Audit task for site ' . $idSite . ' has been already started today');
76+
return;
77+
}
78+
7479
Log::info('Performance Audit task for site ' . $idSite . ' will be started now');
75-
$this->markTaskAsStarted($idSite);
76-
$siteSettings = new MeasurableSettings($idSite);
77-
78-
$urls = $this->getPageUrls($idSite, 'last30');
79-
$runs = range(1, (int) $siteSettings->getSetting('run_count')->getValue());
80-
$emulatedDevices = EmulatedDevice::getList($siteSettings->getSetting('emulated_device')->getValue());
81-
82-
$this->performAudits($idSite, $urls, $emulatedDevices, $runs);
83-
$auditFileCount = iterator_count($this->getAuditFiles($idSite));
84-
Log::debug('Audit file count: ' . $auditFileCount);
85-
if ($auditFileCount > 0) {
86-
$this->storeResultsInDatabase($idSite, $this->processAuditFiles($idSite));
87-
$this->removeAuditFiles($idSite);
80+
try {
81+
$this->markTaskAsRunning();
82+
$this->markTaskAsStartedToday($idSite);
83+
$siteSettings = new MeasurableSettings($idSite);
84+
85+
$urls = $this->getPageUrls($idSite, 'last30');
86+
$runs = range(1, (int) $siteSettings->getSetting('run_count')->getValue());
87+
$emulatedDevices = EmulatedDevice::getList($siteSettings->getSetting('emulated_device')->getValue());
88+
89+
$this->performAudits($idSite, $urls, $emulatedDevices, $runs);
90+
$auditFileCount = iterator_count($this->getAuditFiles($idSite));
91+
Log::debug('Audit file count: ' . $auditFileCount);
92+
if ($auditFileCount > 0) {
93+
$this->storeResultsInDatabase($idSite, $this->processAuditFiles($idSite));
94+
$this->removeAuditFiles($idSite);
95+
}
96+
} catch (Exception $exception) {
97+
Log::error($exception->getMessage());
98+
} finally {
99+
$this->markTaskAsFinished();
88100
}
89101
Log::info('Performance Audit task for site ' . $idSite . ' has finished');
90102
}
91103

92104
/**
93-
* Check if any task has started today.
105+
* Check if any task is currently running.
94106
*
95107
* @return bool
96108
* @throws Exception
97109
*/
98-
private function hasAnyTaskStartedToday()
110+
private function hasAnyTaskRunning()
99111
{
100-
$tasksStartedToday = array_map(function($site) {
101-
return $this->hasTaskStartedToday((int) $site['idsite']);
102-
}, Site::getSites());
112+
Option::clearCachedOption($this->hasTaskRunningKey());
113+
$hasTaskRunning = !!Option::get($this->hasTaskRunningKey());
103114

104-
return in_array(true, $tasksStartedToday);
115+
return $hasTaskRunning;
105116
}
106117

107118
/**
@@ -113,8 +124,8 @@ private function hasAnyTaskStartedToday()
113124
*/
114125
private function hasTaskStartedToday(int $idSite)
115126
{
116-
Option::clearCachedOption($this->lastRunKey($idSite));
117-
$lastRun = Option::get($this->lastRunKey($idSite));
127+
Option::clearCachedOption($this->lastTaskRunKey($idSite));
128+
$lastRun = Option::get($this->lastTaskRunKey($idSite));
118129
if (!$lastRun) {
119130
return false;
120131
}
@@ -123,16 +134,50 @@ private function hasTaskStartedToday(int $idSite)
123134
}
124135

125136
/**
126-
* Marks this task as started in DB.
137+
* Marks a task as running in DB.
138+
*
139+
* @return void
140+
* @throws Exception
141+
*/
142+
private function markTaskAsRunning()
143+
{
144+
Log::debug('Mark task as running now');
145+
Option::set($this->hasTaskRunningKey(), 1);
146+
}
147+
148+
/**
149+
* Marks a task as finished in DB by deleting the running option key.
150+
*
151+
* @return void
152+
* @throws Exception
153+
*/
154+
private function markTaskAsFinished()
155+
{
156+
Log::debug('Mark task as finished now');
157+
Option::delete($this->hasTaskRunningKey());
158+
}
159+
160+
/**
161+
* Marks this task as started today in DB.
127162
*
128163
* @param int $idSite
129164
* @return void
130165
* @throws Exception
131166
*/
132-
private function markTaskAsStarted(int $idSite)
167+
private function markTaskAsStartedToday(int $idSite)
168+
{
169+
Log::debug('Mark task for site ' . $idSite . ' as started today');
170+
Option::set($this->lastTaskRunKey($idSite), Date::factory('today')->getTimestamp());
171+
}
172+
173+
/**
174+
* Returns the option name for a currently running task.
175+
*
176+
* @return string
177+
*/
178+
private static function hasTaskRunningKey()
133179
{
134-
Log::debug('Mark task for site ' . $idSite . ' as started');
135-
Option::set($this->lastRunKey($idSite), Date::factory('today')->getTimestamp());
180+
return 'hasRunningPerformanceAuditTask';
136181
}
137182

138183
/**
@@ -141,7 +186,7 @@ private function markTaskAsStarted(int $idSite)
141186
* @param int $idSite
142187
* @return string
143188
*/
144-
private static function lastRunKey($idSite)
189+
private static function lastTaskRunKey($idSite)
145190
{
146191
return 'lastRunPerformanceAuditTask_' . $idSite;
147192
}
@@ -246,7 +291,7 @@ private function performAudits(int $idSite, array $urls, array $emulatedDevices,
246291
->setEmulatedDevice($emulatedDevice)
247292
->audit($url);
248293
} catch (AuditFailedException $exception) {
249-
echo $exception->getMessage();
294+
Log::error($exception->getMessage());
250295
}
251296
}
252297
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"ext-json": "*",
1515
"ext-spl": "*",
1616
"ducks-project/spl-types": "^1.1",
17-
"symfony/polyfill-mbstring": "^1.17",
17+
"symfony/polyfill-mbstring": "^1.18",
1818
"symfony/process": "^4.4",
1919
"jeroen-g/lighthouse": "^0.2"
2020
},

tests/Integration/PerformanceAuditIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ abstract class PerformanceAuditIntegrationTest extends IntegrationTestCase
4242
public function setUp(): void
4343
{
4444
if (!$this->isServerRunning()) {
45-
throw new Exception("Server not found on port localhost:80. For integration tests, an server must be running.");
45+
throw new Exception('Server not found on port localhost:80. For integration tests, an server must be running.');
4646
}
4747

4848
parent::setUp();

vendor/composer/installed.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,17 @@
109109
},
110110
{
111111
"name": "symfony/polyfill-mbstring",
112-
"version": "v1.17.1",
113-
"version_normalized": "1.17.1.0",
112+
"version": "v1.18.0",
113+
"version_normalized": "1.18.0.0",
114114
"source": {
115115
"type": "git",
116116
"url": "https://github.com/symfony/polyfill-mbstring.git",
117-
"reference": "7110338d81ce1cbc3e273136e4574663627037a7"
117+
"reference": "a6977d63bf9a0ad4c65cd352709e230876f9904a"
118118
},
119119
"dist": {
120120
"type": "zip",
121-
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/7110338d81ce1cbc3e273136e4574663627037a7",
122-
"reference": "7110338d81ce1cbc3e273136e4574663627037a7",
121+
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/a6977d63bf9a0ad4c65cd352709e230876f9904a",
122+
"reference": "a6977d63bf9a0ad4c65cd352709e230876f9904a",
123123
"shasum": ""
124124
},
125125
"require": {
@@ -128,11 +128,11 @@
128128
"suggest": {
129129
"ext-mbstring": "For best performance"
130130
},
131-
"time": "2020-06-06T08:46:27+00:00",
131+
"time": "2020-07-14T12:35:20+00:00",
132132
"type": "library",
133133
"extra": {
134134
"branch-alias": {
135-
"dev-master": "1.17-dev"
135+
"dev-master": "1.18-dev"
136136
},
137137
"thanks": {
138138
"name": "symfony/polyfill",

vendor/symfony/polyfill-mbstring/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"minimum-stability": "dev",
2929
"extra": {
3030
"branch-alias": {
31-
"dev-master": "1.17-dev"
31+
"dev-master": "1.18-dev"
3232
},
3333
"thanks": {
3434
"name": "symfony/polyfill",

0 commit comments

Comments
 (0)