use crate::errors::{
BigQueryError, BigQueryErrorPublicGenericDetails, BigQueryJobError, BigQueryJobErrorEntry,
};
use crate::{BigQueryDb, BigQueryJobRef, BigQueryResult};
use gcloud_sdk::google::cloud::bigquery::v2::{
CancelJobRequest, ErrorProto, GetJobRequest, GetQueryResultsRequest, GetQueryResultsResponse,
Job,
};
use tracing::Span;
impl BigQueryDb {
pub async fn cancel_job(&self, job: &BigQueryJobRef) -> BigQueryResult<()> {
let span = tracing::debug_span!(
"BigQuery Cancel Job",
"/bigquery/job_id" = job.job_id.as_str(),
);
let request = CancelJobRequest {
project_id: job.project_id.clone(),
job_id: job.job_id.to_string(),
location: job.location_field(),
};
self.retry(&span, "cancel a job", &request, |r| {
let mut client = self.job_client();
async move { client.cancel_job(r).await }
})
.await?;
Ok(())
}
pub(crate) async fn wait_for_job(
&self,
job: &BigQueryJobRef,
timeout_ms: u32,
span: &Span,
) -> BigQueryResult<GetQueryResultsResponse> {
let request = GetQueryResultsRequest {
project_id: job.project_id.clone(),
job_id: job.job_id.to_string(),
max_results: Some(0),
timeout_ms: Some(timeout_ms),
location: job.location_field(),
..Default::default()
};
loop {
let response = self
.retry(span, "wait for the query job", &request, |r| {
let mut client = self.job_client();
async move { client.get_query_results(r).await }
})
.await?;
if response.job_complete == Some(true) {
return Ok(response);
}
}
}
pub(crate) async fn get_query_job(
&self,
job: &BigQueryJobRef,
span: &Span,
) -> BigQueryResult<Job> {
let request = GetJobRequest {
project_id: job.project_id.clone(),
job_id: job.job_id.to_string(),
location: job.location_field(),
};
let details = self
.retry(span, "get the query job", &request, |r| {
let mut client = self.job_client();
async move { client.get_job(r).await }
})
.await?;
let status = details.status.clone().unwrap_or_default();
match status.error_result {
Some(error_result) => Err(BigQueryError::JobError(Box::new(
BigQueryJobError::new(
BigQueryErrorPublicGenericDetails::new(error_result.reason.clone()),
job_error_details(&error_result),
status.errors.into_iter().map(Into::into).collect(),
)
.with_job(job.clone()),
))),
None => Ok(details),
}
}
}
fn job_error_details(error: &ErrorProto) -> String {
if error.location.is_empty() {
error.message.clone()
} else {
format!("{} at {}", error.message, error.location)
}
}
impl From<ErrorProto> for BigQueryJobErrorEntry {
fn from(error: ErrorProto) -> Self {
BigQueryJobErrorEntry::new(error.reason, error.location, error.message)
}
}