-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSpawnQueryserviceUpdaterJob.php
More file actions
118 lines (101 loc) · 4.46 KB
/
SpawnQueryserviceUpdaterJob.php
File metadata and controls
118 lines (101 loc) · 4.46 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
<?php
namespace App\Jobs;
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 SpawnQueryserviceUpdaterJob implements ShouldBeUnique, ShouldQueue {
use InteractsWithQueue, Queueable;
public string $wikiDomain;
public string $entities;
public string $sparqlUrl;
public string $qsKubernetesNamespace;
public function __construct(string $wikiDomain, string $entities, string $sparqlUrl) {
$sortedEntities = explode(',', $entities);
asort($sortedEntities);
$this->wikiDomain = $wikiDomain;
$this->entities = implode(',', $sortedEntities);
$this->sparqlUrl = $sparqlUrl;
$this->qsKubernetesNamespace = Config::get('wbstack.qs_job_namespace');
}
public function uniqueId(): string {
return $this->wikiDomain . $this->entities;
}
public function handle(Client $kubernetesClient): void {
$kubernetesClient->setNamespace('default');
$qsUpdaterPod = $kubernetesClient->pods()->setFieldSelector([
'status.phase' => 'Running',
])->setLabelSelector([
'app.kubernetes.io/name' => 'queryservice-updater',
])->first();
if ($qsUpdaterPod === null) {
$this->fail(
new \RuntimeException(
'Unable to find a running queryservice-updater pod in the cluster, ' .
'cannot continue.'
)
);
return;
}
$qsUpdaterPod = $qsUpdaterPod->toArray();
$kubernetesClient->setNamespace($this->qsKubernetesNamespace);
$jobSpec = new KubernetesJob([
'metadata' => [
'name' => 'run-qs-updater-' . hash('sha1', $this->uniqueId()),
'namespace' => $this->qsKubernetesNamespace,
'labels' => [
'app.kubernetes.io/instance' => $this->wikiDomain,
'app.kubernetes.io/name' => 'run-qs-updater',
],
],
'spec' => [
'ttlSecondsAfterFinished' => 172800, // 1 week
'template' => [
'metadata' => [
'name' => 'run-qs-updater',
],
'spec' => [
'containers' => [
0 => [
'name' => 'run-qs-updater',
'image' => $qsUpdaterPod['spec']['containers'][0]['image'],
'env' => $qsUpdaterPod['spec']['containers'][0]['env'],
'command' => [
0 => 'bash',
1 => '-c',
2 => <<<CMD
/wdqsup/runUpdateWbStack.sh -- \
--wikibaseHost {$this->wikiDomain} \
--ids {$this->entities} \
--entityNamespaces 120,122,146 \
--sparqlUrl {$this->sparqlUrl} \
--wikibaseScheme http \
--conceptUri https://{$this->wikiDomain}
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('Queryservice Updater creation for wiki "' . $this->wikiDomain . '" failed with message: ' . data_get($job, 'message', 'n/a'))
);
return;
}
Log::info(
'Queryservice Updater for wiki "' . $this->wikiDomain . '" and entities "' . $this->entities . '" exists or was created with name "' . $jobName . '".'
);
}
}