Class: Gcloud::Bigquery::Job
- Inherits:
-
Object
- Object
- Gcloud::Bigquery::Job
- Defined in:
- lib/gcloud/bigquery/job.rb,
lib/gcloud/bigquery/job/list.rb
Overview
# Job
Represents a generic Job that may be performed on a Table.
The subclasses of Job represent the specific BigQuery job types: CopyJob, ExtractJob, LoadJob, and QueryJob.
A job instance is created when you call Project#query_job, Dataset#query_job, Table#copy, Table#extract, Table#load, or View#data.
Direct Known Subclasses
Defined Under Namespace
Classes: List
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#configuration ⇒ Object
(also: #config)
The configuration for the job.
-
#created_at ⇒ Object
The time when the job was created.
-
#done? ⇒ Boolean
Checks if the job’s state is ‘DONE`.
-
#ended_at ⇒ Object
The time when the job ended.
-
#error ⇒ Hash
The last error for the job, if any errors have occurred.
-
#errors ⇒ Object
The errors for the job, if any errors have occurred.
-
#failed? ⇒ Boolean
Checks if an error is present.
-
#initialize ⇒ Job
constructor
A new instance of Job.
-
#job_id ⇒ Object
The ID of the job.
-
#pending? ⇒ Boolean
Checks if the job’s state is ‘PENDING`.
-
#project_id ⇒ Object
The ID of the project containing the job.
-
#reload! ⇒ Object
(also: #refresh!)
Reloads the job with current data from the BigQuery service.
-
#rerun! ⇒ Object
Created a new job with the current configuration.
-
#running? ⇒ Boolean
Checks if the job’s state is ‘RUNNING`.
-
#started_at ⇒ Object
The time when the job was started.
-
#state ⇒ Object
The current state of the job.
-
#statistics ⇒ Object
(also: #stats)
The statistics for the job.
-
#status ⇒ Object
The job’s status.
-
#wait_until_done! ⇒ Object
Refreshes the job until the job is ‘DONE`.
Constructor Details
#initialize ⇒ Job
Returns a new instance of Job.
67 68 69 70 |
# File 'lib/gcloud/bigquery/job.rb', line 67 def initialize @connection = nil @gapi = {} end |
Instance Attribute Details
#connection ⇒ Object
59 60 61 |
# File 'lib/gcloud/bigquery/job.rb', line 59 def connection @connection end |
#gapi ⇒ Object
63 64 65 |
# File 'lib/gcloud/bigquery/job.rb', line 63 def gapi @gapi end |
Class Method Details
.from_gapi(gapi, conn) ⇒ Object
262 263 264 265 266 267 268 |
# File 'lib/gcloud/bigquery/job.rb', line 262 def self.from_gapi gapi, conn klass = klass_for gapi klass.new.tap do |f| f.gapi = gapi f.connection = conn end end |
Instance Method Details
#configuration ⇒ Object Also known as: config
The configuration for the job. Returns a hash.
156 157 158 159 160 |
# File 'lib/gcloud/bigquery/job.rb', line 156 def configuration hash = @gapi["configuration"] || {} hash = hash.to_hash if hash.respond_to? :to_hash hash end |
#created_at ⇒ Object
The time when the job was created.
126 127 128 129 130 |
# File 'lib/gcloud/bigquery/job.rb', line 126 def created_at return nil if @gapi["statistics"].nil? return nil if @gapi["statistics"]["creationTime"].nil? Time.at(@gapi["statistics"]["creationTime"] / 1000.0) end |
#done? ⇒ Boolean
Checks if the job’s state is ‘DONE`. When `true`, the job has stopped running. However, a `DONE` state does not mean that the job completed successfully. Use #failed? to detect if an error occurred or if the job was successful.
113 114 115 116 |
# File 'lib/gcloud/bigquery/job.rb', line 113 def done? return false if state.nil? "done".casecmp(state).zero? end |
#ended_at ⇒ Object
The time when the job ended. This field is present when the job’s state is ‘DONE`.
145 146 147 148 149 |
# File 'lib/gcloud/bigquery/job.rb', line 145 def ended_at return nil if @gapi["statistics"].nil? return nil if @gapi["statistics"]["endTime"].nil? Time.at(@gapi["statistics"]["endTime"] / 1000.0) end |
#error ⇒ Hash
The last error for the job, if any errors have occurred. Returns a hash.
198 199 200 |
# File 'lib/gcloud/bigquery/job.rb', line 198 def error status["errorResult"] end |
#errors ⇒ Object
The errors for the job, if any errors have occurred. Returns an array of hash objects. See #error.
205 206 207 |
# File 'lib/gcloud/bigquery/job.rb', line 205 def errors Array status["errors"] end |
#failed? ⇒ Boolean
Checks if an error is present.
120 121 122 |
# File 'lib/gcloud/bigquery/job.rb', line 120 def failed? !error.nil? end |
#job_id ⇒ Object
The ID of the job.
74 75 76 |
# File 'lib/gcloud/bigquery/job.rb', line 74 def job_id @gapi["jobReference"]["jobId"] end |
#pending? ⇒ Boolean
Checks if the job’s state is ‘PENDING`.
103 104 105 106 |
# File 'lib/gcloud/bigquery/job.rb', line 103 def pending? return false if state.nil? "pending".casecmp(state).zero? end |
#project_id ⇒ Object
The ID of the project containing the job.
80 81 82 |
# File 'lib/gcloud/bigquery/job.rb', line 80 def project_id @gapi["jobReference"]["projectId"] end |
#reload! ⇒ Object Also known as: refresh!
Reloads the job with current data from the BigQuery service.
223 224 225 226 227 228 229 230 231 |
# File 'lib/gcloud/bigquery/job.rb', line 223 def reload! ensure_connection! resp = connection.get_job job_id if resp.success? @gapi = resp.data else fail ApiError.from_response(resp) end end |
#rerun! ⇒ Object
Created a new job with the current configuration.
211 212 213 214 215 216 217 218 219 |
# File 'lib/gcloud/bigquery/job.rb', line 211 def rerun! ensure_connection! resp = connection.insert_job configuration if resp.success? Job.from_gapi resp.data, connection else fail ApiError.from_response(resp) end end |
#running? ⇒ Boolean
Checks if the job’s state is ‘RUNNING`.
96 97 98 99 |
# File 'lib/gcloud/bigquery/job.rb', line 96 def running? return false if state.nil? "running".casecmp(state).zero? end |
#started_at ⇒ Object
The time when the job was started. This field is present after the job’s state changes from ‘PENDING` to either `RUNNING` or `DONE`.
136 137 138 139 140 |
# File 'lib/gcloud/bigquery/job.rb', line 136 def started_at return nil if @gapi["statistics"].nil? return nil if @gapi["statistics"]["startTime"].nil? Time.at(@gapi["statistics"]["startTime"] / 1000.0) end |
#state ⇒ Object
The current state of the job. The possible values are ‘PENDING`, `RUNNING`, and `DONE`. A `DONE` state does not mean that the job completed successfully. Use #failed? to discover if an error occurred or if the job was successful.
89 90 91 92 |
# File 'lib/gcloud/bigquery/job.rb', line 89 def state return nil if @gapi["status"].nil? @gapi["status"]["state"] end |
#statistics ⇒ Object Also known as: stats
The statistics for the job. Returns a hash.
168 169 170 171 172 |
# File 'lib/gcloud/bigquery/job.rb', line 168 def statistics hash = @gapi["statistics"] || {} hash = hash.to_hash if hash.respond_to? :to_hash hash end |
#status ⇒ Object
178 179 180 181 182 |
# File 'lib/gcloud/bigquery/job.rb', line 178 def status hash = @gapi["status"] || {} hash = hash.to_hash if hash.respond_to? :to_hash hash end |
#wait_until_done! ⇒ Object
Refreshes the job until the job is ‘DONE`. The delay between refreshes will incrementally increase.
250 251 252 253 254 255 256 257 258 |
# File 'lib/gcloud/bigquery/job.rb', line 250 def wait_until_done! backoff = ->(retries) { sleep 2 * retries + 5 } retries = 0 until done? backoff.call retries retries += 1 reload! end end |