-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSiteStatsUpdateJob.php
More file actions
86 lines (66 loc) · 2.61 KB
/
SiteStatsUpdateJob.php
File metadata and controls
86 lines (66 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace App\Jobs;
use App\Http\Curl\HttpRequest;
use App\Services\MediaWikiHostResolver;
use App\Wiki;
use Illuminate\Bus\Batchable;
use Illuminate\Support\Facades\Log;
/*
*
* Job that updates site_stats table in mediawiki by calling initSiteStats.php
*
* Example: php artisan job:dispatch SiteStatsUpdateJob
*/
class SiteStatsUpdateJob extends Job {
use Batchable;
private $wiki_id;
public function __construct($wiki_id) {
$this->wiki_id = $wiki_id;
}
public function handle(HttpRequest $request, MediaWikiHostResolver $mwHostResolver): void {
$timeStart = microtime(true);
$wiki = Wiki::where('id', $this->wiki_id)->first();
if (!$wiki) {
$this->fail(new \RuntimeException(" Could not find wiki with id: $this->wiki_id"));
}
Log::info(__METHOD__ . ": Updating stats for $wiki->domain");
$request->setOptions([
CURLOPT_URL => $mwHostResolver->getBackendHostForDomain($wiki->domain) . '/w/api.php?action=wbstackSiteStatsUpdate&format=json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_TIMEOUT => 60 * 5,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [
'content-type: application/x-www-form-urlencoded',
'host: ' . $wiki->domain,
],
]);
$rawResponse = $request->execute();
$err = $request->error();
$request->close();
if ($err) {
Log::error(__METHOD__ . ": wbstackSiteStatsUpdate failed: $rawResponse");
$this->fail(
new \RuntimeException('curl error for ' . $wiki->domain . ': ' . $err)
);
return; // safegaurd
}
$response = json_decode($rawResponse, true);
if (!is_array($response) || !array_key_exists('wbstackSiteStatsUpdate', $response)) {
$this->fail(
new \RuntimeException('wbstackSiteStatsUpdate call for ' . $wiki->domain . '. No wbstackSiteStatsUpdate key in response: ' . $rawResponse)
);
return; // safegaurd
}
if ($response['wbstackSiteStatsUpdate']['return'] !== 0) {
$this->fail(
new \RuntimeException('wbstackSiteStatsUpdate call for ' . $wiki->domain . ' was not successful: ' . $rawResponse)
);
return; // safegaurd
}
$timeEnd = microtime(true);
$executionTime = ($timeEnd - $timeStart);
Log::info(__METHOD__ . ": Finished in: $executionTime s");
}
}