@@ -67,41 +67,52 @@ public function schedule()
67
67
*/
68
68
public function auditSite (int $ idSite )
69
69
{
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 ' );
72
72
return ;
73
73
}
74
+ if ($ this ->hasTaskStartedToday ($ idSite )) {
75
+ Log::info ('Performance Audit task for site ' . $ idSite . ' has been already started today ' );
76
+ return ;
77
+ }
78
+
74
79
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 ();
88
100
}
89
101
Log::info ('Performance Audit task for site ' . $ idSite . ' has finished ' );
90
102
}
91
103
92
104
/**
93
- * Check if any task has started today .
105
+ * Check if any task is currently running .
94
106
*
95
107
* @return bool
96
108
* @throws Exception
97
109
*/
98
- private function hasAnyTaskStartedToday ()
110
+ private function hasAnyTaskRunning ()
99
111
{
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 ());
103
114
104
- return in_array ( true , $ tasksStartedToday ) ;
115
+ return $ hasTaskRunning ;
105
116
}
106
117
107
118
/**
@@ -113,8 +124,8 @@ private function hasAnyTaskStartedToday()
113
124
*/
114
125
private function hasTaskStartedToday (int $ idSite )
115
126
{
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 ));
118
129
if (!$ lastRun ) {
119
130
return false ;
120
131
}
@@ -123,16 +134,50 @@ private function hasTaskStartedToday(int $idSite)
123
134
}
124
135
125
136
/**
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.
127
162
*
128
163
* @param int $idSite
129
164
* @return void
130
165
* @throws Exception
131
166
*/
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 ()
133
179
{
134
- Log::debug ('Mark task for site ' . $ idSite . ' as started ' );
135
- Option::set ($ this ->lastRunKey ($ idSite ), Date::factory ('today ' )->getTimestamp ());
180
+ return 'hasRunningPerformanceAuditTask ' ;
136
181
}
137
182
138
183
/**
@@ -141,7 +186,7 @@ private function markTaskAsStarted(int $idSite)
141
186
* @param int $idSite
142
187
* @return string
143
188
*/
144
- private static function lastRunKey ($ idSite )
189
+ private static function lastTaskRunKey ($ idSite )
145
190
{
146
191
return 'lastRunPerformanceAuditTask_ ' . $ idSite ;
147
192
}
@@ -246,7 +291,7 @@ private function performAudits(int $idSite, array $urls, array $emulatedDevices,
246
291
->setEmulatedDevice ($ emulatedDevice )
247
292
->audit ($ url );
248
293
} catch (AuditFailedException $ exception ) {
249
- echo $ exception ->getMessage ();
294
+ Log:: error ( $ exception ->getMessage () );
250
295
}
251
296
}
252
297
}
0 commit comments