Repository navigation
Expand file tree
/
Copy pathJobConfigurationTrait.php
More file actions
196 lines (173 loc) · 5.85 KB
/
Copy pathJobConfigurationTrait.php
File metadata and controls
196 lines (173 loc) · 5.85 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\BigQuery;
use Google\Cloud\Core\ArrayTrait;
use Ramsey\Uuid\Uuid;
/**
* A utility trait implementing shared behavior between job configurations.
*/
trait JobConfigurationTrait
{
use ArrayTrait;
/**
* @var string $jobIdPrefix
*/
private $jobIdPrefix;
/**
* @var array $config
*/
private $config = [];
/**
* Sets shared job configuration properties.
*
* @access private
* @param string $projectId The project's ID.
* @param array $config A set of configuration options for a job.
* @param string|null $location The geographic location in which the job is
* executed.
*/
public function jobConfigurationProperties(
$projectId,
array $config,
$location
) {
$this->config = array_replace_recursive([
'projectId' => $projectId,
'jobReference' => ['projectId' => $projectId]
], $config);
if ($location && !isset($this->config['jobReference']['location'])) {
$this->config['jobReference']['location'] = $location;
}
if (!isset($this->config['jobReference']['jobId'])) {
$this->config['jobReference']['jobId'] = $this->generateJobId();
}
}
/**
* Sets dryRun flag. If set to true, don't actually run this job. A valid query will
* return a mostly empty response with some processing statistics, while an
* invalid query will return the same error it would if it wasn't a dry run.
*
* @param bool $dryRun Whether or not to execute as a dry run.
* @return JobConfigurationInterface
*/
public function dryRun($dryRun)
{
$this->config['configuration']['dryRun'] = $dryRun;
return $this;
}
/**
* Sets a prefix for the job ID.
*
* @param string $jobIdPrefix If provided, the job ID will be of format
* `{$jobIdPrefix}-{jobId}`.
* @return JobConfigurationInterface
*/
public function jobIdPrefix($jobIdPrefix)
{
$this->jobIdPrefix = $jobIdPrefix;
return $this;
}
/**
* Sets the labels associated with the job. Labels can use these to organize
* and group jobs. Label keys and values can be no longer than 63 characters,
* can only contain lowercase letters, numeric characters, underscores and
* dashes. International characters are allowed. Label values are optional.
* Label keys must start with a letter and each label in the list must have
* a different key.
*
* @param array $labels The labels to apply.
* @return JobConfigurationInterface
*/
public function labels(array $labels)
{
$this->config['configuration']['labels'] = $labels;
return $this;
}
/**
* Sets the optional reservation that job would use. User can specify a
* reservation to execute the job. If reservation is not set, reservation is
* determined based on the rules defined by the reservation assignments. The
* expected format is
* projects/{project}/locations/{location}/reservations/{reservation}.
*
* @param string $reservation The reservation to use.
* @return JobConfigurationInterface
*/
public function reservation(string $reservation): self
{
$this->config['configuration']['reservation'] = $reservation;
return $this;
}
/**
* Sets the optional job timeout in milliseconds relative to the job
* creation time. If this time limit is exceeded, BigQuery attempts to stop
* the job, but might not always succeed in canceling it before the job
* completes. For example, a job that takes more than 60 seconds to complete
* has a better chance of being stopped than a job that takes 10 seconds to
* complete.
*
* @param int $jobTimeoutMs The job timeout in milliseconds.
* @return JobConfigurationInterface
*/
public function jobTimeoutMs(int $jobTimeoutMs): self
{
$this->config['configuration']['jobTimeoutMs'] = $jobTimeoutMs;
return $this;
}
/**
* Specifies the geographic location of the job. Required for jobs started
* outside of the US and EU regions.
*
* @param string $location The geographic location of the job.
* **Defaults to** a location specified in the client configuration,
* if provided, or through location metadata fetched by a network
* request
* (by calling {@see Table::reload()}, for example).
* @return JobConfigurationInterface
*/
public function location($location)
{
$this->config['jobReference']['location'] = $location;
return $this;
}
/**
* Returns the job config as an array.
*
* @access private
* @return array
*/
public function toArray()
{
if ($this->jobIdPrefix) {
$this->config['jobReference']['jobId'] = sprintf(
'%s-%s',
$this->jobIdPrefix,
$this->config['jobReference']['jobId']
);
}
return $this->config;
}
/**
* Generate a Job ID.
*
* @return string
*/
protected function generateJobId()
{
return Uuid::uuid4()->toString();
}
}