summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Utils/Runners/JobQueueRunner.php
blob: 29e124dbd7b493ef8d015974f047e44d7319c421 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php

namespace SMW\Tests\Utils\Runners;

use Job;
use JobQueueGroup;
use SMW\Connection\ConnectionProvider as IConnectionProvider;
use SMW\Tests\TestEnvironment;
use SMW\Tests\Utils\Connection\ConnectionProvider;

/**
 * Partly copied from the MW 1.19 RunJobs maintenance script
 *
 * @group SMW
 * @group SMWExtension
 *
 * @license GNU GPL v2+
 * @since 1.9.2
 */
class JobQueueRunner {

	protected $type = null;
	protected $status = [];
	protected $connectionProvider = null;

	/**
	 * @var TestEnvironment
	 */
	private $testEnvironment;

	/**
	 * @since 1.9.2
	 *
	 * @param string|null $type
	 * @param IConnectionProvider|null $connectionProvider
	 */
	public function __construct( $type = null, IConnectionProvider $connectionProvider = null ) {
		$this->type = $type;
		$this->connectionProvider = $connectionProvider;

		if ( $this->connectionProvider === null ) {
			$this->connectionProvider = new ConnectionProvider();
		}

		$this->testEnvironment = new TestEnvironment();
	}

	/**
	 * @since 2.1
	 *
	 * @param string $type
	 *
	 * @return JobQueueRunner
	 */
	public function setType( $type ) {
		$this->type = $type;
		return $this;
	}

	/**
	 * @since 2.1
	 *
	 * @param IConnectionProvider $connectionProvider
	 *
	 * @return JobQueueRunner
	 */
	public function setConnectionProvider( IConnectionProvider $connectionProvider ) {
		$this->connectionProvider = $connectionProvider;
		return $this;
	}

	/**
	 * @since 1.9.2
	 */
	public function run() {

		$conds = '';
		$connection = $this->connectionProvider->getConnection();

		if ( $this->type !== null ) {
			$conds = "job_cmd = " . $connection->addQuotes( $this->type );
		}

		while ( $connection->selectField( 'job', 'job_id', $conds, __METHOD__ ) ) {

			$job = $this->type === null ? $this->pop() : $this->pop_type( $this->type );

			if ( !$job ) {
				break;
			}

			wfWaitForSlaves();

			$this->status[] = [
				'type'   => $job->command,
				'status' => $job->run()
			];
		}

		$this->testEnvironment->executePendingDeferredUpdates();
	}

	/**
	 * @since  2.0
	 */
	public function deleteAllJobs() {

		$conditions = '*';
		$connection = $this->connectionProvider->getConnection();

		if ( $this->type !== null ) {
			$conditions = "job_cmd = " . $connection->addQuotes( $this->type );
		}

		$connection->delete(
			'job',
			$conditions,
			__METHOD__
		);
	}

	/**
	 * @since 1.9.2
	 *
	 * @return array
	 */
	public function getStatus() {
		return $this->status;
	}

	/**
	 * @see https://gerrit.wikimedia.org/r/#/c/162009/
	 */
	private function pop() {
		$offset = 0;

		if ( class_exists( 'JobQueueGroup' ) ) {
			return JobQueueGroup::singleton()->pop();
		}

		return Job::pop( $offset );
	}

	/**
	 * @see https://gerrit.wikimedia.org/r/#/c/162009/
	 */
	public function pop_type( $type ) {

		if ( class_exists( 'JobQueueGroup' ) ) {
			return JobQueueGroup::singleton()->get( $type )->pop();
		}

		return Job::pop_type( $type );
	}

}