-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProcessMediaWikiJobsJob.php
More file actions
131 lines (114 loc) · 5 KB
/
ProcessMediaWikiJobsJob.php
File metadata and controls
131 lines (114 loc) · 5 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace App\Jobs;
use App\Services\MediaWikiHostResolver;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Maclof\Kubernetes\Client;
use Maclof\Kubernetes\Models\Job as KubernetesJob;
class ProcessMediaWikiJobsJob implements ShouldBeUnique, ShouldQueue {
use InteractsWithQueue, Queueable;
private string $wikiDomain;
private string $jobsKubernetesNamespace;
public function __construct(string $wikiDomain) {
$this->wikiDomain = $wikiDomain;
$this->jobsKubernetesNamespace = Config::get('wbstack.api_job_namespace');
}
public function uniqueId(): string {
return $this->wikiDomain;
}
public function handle(Client $kubernetesClient, MediaWikiHostResolver $resolver): void {
$FQDNOfService = $resolver->getBackendHostForDomain($this->wikiDomain);
$serviceName = explode('.', $FQDNOfService)[0];
$kubernetesClient->setNamespace('default');
$backendService = $kubernetesClient->services()->setFieldSelector([
'metadata.name' => $serviceName,
])->first();
if ($backendService === null) {
$this->fail(
new \RuntimeException(
'Unable to find a matching MediaWiki backend service in the cluster'
)
);
return;
}
$MWPodSelector = $backendService->toArray()['spec']['selector'];
$mediawikiPod = $kubernetesClient->pods()->setFieldSelector([
'status.phase' => 'Running',
])->setLabelSelector($MWPodSelector)->first();
if ($mediawikiPod === null) {
$this->fail(
new \RuntimeException(
'Unable to find a running MediaWiki pod in the cluster, ' .
'cannot continue.'
)
);
return;
}
$mediawikiPod = $mediawikiPod->toArray();
$kubernetesClient->setNamespace($this->jobsKubernetesNamespace);
$jobSpec = new KubernetesJob([
'metadata' => [
'name' => 'run-all-mw-jobs-' . hash('sha1', $this->wikiDomain),
'namespace' => $this->jobsKubernetesNamespace,
'labels' => [
'app.kubernetes.io/instance' => $this->wikiDomain,
'app.kubernetes.io/name' => 'run-all-mw-jobs',
],
],
'spec' => [
'ttlSecondsAfterFinished' => 0,
'template' => [
'metadata' => [
'name' => 'run-all-mw-jobs',
],
'spec' => [
'containers' => [
0 => [
'name' => 'run-all-mw-jobs',
'image' => $mediawikiPod['spec']['containers'][0]['image'],
'env' => array_merge(
$mediawikiPod['spec']['containers'][0]['env'],
[['name' => 'WBS_DOMAIN', 'value' => $this->wikiDomain]]
),
'command' => [
0 => 'bash',
1 => '-c',
2 => <<<'CMD'
JOBS_TO_GO=1
while [ "$JOBS_TO_GO" != "0" ]
do
echo "Running 1000 jobs"
php w/maintenance/runJobs.php --maxjobs 1000
echo Waiting for 1 seconds...
sleep 1
JOBS_TO_GO=$(php w/maintenance/showJobs.php | tr -d '[:space:]')
echo $JOBS_TO_GO jobs to go
done
CMD
],
],
],
'restartPolicy' => 'Never',
],
],
],
]);
$job = $kubernetesClient->jobs()->apply($jobSpec);
$jobName = data_get($job, 'metadata.name');
if (data_get($job, 'status') === 'Failure' || !$jobName) {
// The k8s client does not fail reliably on 4xx responses, so checking the name
// currently serves as poor man's error handling.
$this->fail(
new \RuntimeException('Job creation for wiki "' . $this->wikiDomain . '" failed with message: ' . data_get($job, 'message', 'n/a'))
);
return;
}
Log::info(
'MediaWiki Job for wiki "' . $this->wikiDomain . '" exists or was created with name "' . $jobName . '".'
);
}
}