Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make UploadableFile::get_data async for AsyncRead #82

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gitlab-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bytes = "1.9.0"
zip = "2.2.2"
pin-project = "1.0.7"
futures = "0.3.15"
async-trait = "0.1.50"
async-trait = "0.1.86"
tempfile = "3.15.0"
parking_lot = "0.12.0"
tracing-subscriber = "0.3.10"
Expand Down
7 changes: 4 additions & 3 deletions gitlab-runner/examples/demo-runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ enum DemoFile {
Fact { index: usize, value: String },
}

#[async_trait::async_trait]
impl UploadableFile for DemoFile {
type Data<'a> = Box<dyn AsyncRead + Send + Unpin>;

Expand All @@ -151,11 +152,11 @@ impl UploadableFile for DemoFile {
}
}

fn get_data(&self) -> Box<dyn AsyncRead + Send + Unpin> {
match self {
async fn get_data(&self) -> Result<Box<dyn AsyncRead + Send + Unpin>, ()> {
Ok(match self {
DemoFile::ReadMe => Box::new(b"Demo runner artifact\n".as_slice()),
DemoFile::Fact { value, .. } => Box::new(Cursor::new(value.clone().into_bytes())),
}
})
}
}

Expand Down
6 changes: 4 additions & 2 deletions gitlab-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub use client::Phase;
/// generated or downloaded from some other source on demand. The
/// `get_path` method is required so that the globbing Gitlab expects
/// can be performed without the handler needing to be involved.
#[async_trait::async_trait]
pub trait UploadableFile: Eq {
/// The type of the data stream returned by
/// [`get_data`](Self::get_data)
Expand All @@ -76,7 +77,7 @@ pub trait UploadableFile: Eq {
/// Get something that can provide the data for this file.
///
/// This can be any implementor of [`AsyncRead`].
fn get_data(&self) -> Self::Data<'_>;
async fn get_data(&self) -> Result<Self::Data<'_>, ()>;
}

/// An [`UploadableFile`] type for JobHandlers that expose no files.
Expand All @@ -87,13 +88,14 @@ pub trait UploadableFile: Eq {
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct NoFiles {}

#[async_trait::async_trait]
impl UploadableFile for NoFiles {
type Data<'a> = &'a [u8];

fn get_path(&self) -> Cow<'_, str> {
unreachable!("tried to get path of NoFiles")
}
fn get_data(&self) -> Self::Data<'_> {
async fn get_data(&self) -> Result<Self::Data<'_>, ()> {
unreachable!("tried to read data from NoFiles");
}
}
Expand Down
2 changes: 1 addition & 1 deletion gitlab-runner/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ where
let path = file.get_path();
match uploader.file(path.to_string()).await {
Ok(mut upload) => {
let mut data = file.get_data();
let mut data = file.get_data().await?;
match futures::io::copy(&mut data, &mut upload).await {
Ok(_) => {
uploaded += 1;
Expand Down
6 changes: 3 additions & 3 deletions gitlab-runner/tests/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ impl UploadableFile for TestFile {
TestFile::Test2 => "test2".into(),
}
}
fn get_data(&self) -> &'_ [u8] {
match self {
async fn get_data(&self) -> Result<&'_ [u8], ()> {
Ok(match self {
TestFile::Test => b"testdata".as_slice(),
TestFile::Test2 => b"testdata2".as_slice(),
}
})
}
}

Expand Down