From 12f6944e59afe7628f55c66666b76d147c54edb7 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Sun, 11 Jul 2021 13:17:02 +0300 Subject: [PATCH] add github actions (#4) * add github actions * fix clippy * skip check * get rid of collect * fix tests * run ignored tests --- .github/dependabot.yml | 7 +++ .github/workflows/rust.yml | 93 +++++++++++++++++++++++++++++++++++++ .github/workflows/style.yml | 26 +++++++++++ CHANGELOG.md | 2 +- src/executor.rs | 14 ++---- src/lib.rs | 2 + src/postgres.rs | 8 +++- src/worker_pool.rs | 39 ++++++++-------- 8 files changed, 159 insertions(+), 32 deletions(-) create mode 100644 .github/workflows/rust.yml create mode 100644 .github/workflows/style.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 1347b82..478bbb2 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -8,3 +8,10 @@ updates: time: "03:37" # UTC commit-message: prefix: "chore(cargo):" + + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + commit-message: + prefix: "ci(actions):" diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..cd68e28 --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,93 @@ +name: Test and Build Rust + +on: + push: + pull_request: + schedule: + # Check if it works with current dependencies (weekly on Wednesday 2:32 UTC) + - cron: '32 2 * * 3' + +jobs: + test: + name: Test + runs-on: ubuntu-latest + + services: + # Label used to access the service container + postgres: + # Docker Hub image + image: postgres + # Provide the password for postgres + env: + POSTGRES_PASSWORD: postgres + POSTGRES_USER: postgres + # Set health checks to wait until postgres has started + + ports: + - 5432:5432 + + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + steps: + - uses: actions/checkout@v2 + + - name: Setup Rust + uses: actions-rs/toolchain@v1 + with: + components: clippy + override: true + profile: minimal + toolchain: nightly + + - name: Run clippy + uses: actions-rs/cargo@v1 + with: + command: clippy + args: --verbose --all-targets --all-features -- -D warnings + + - name: Install diesel-cli + uses: actions-rs/cargo@v1 + with: + command: install + args: diesel_cli --no-default-features --features "postgres" + + - name: Setup db + run: diesel setup + + - name: Run tests + uses: actions-rs/cargo@v1 + with: + command: test + args: --verbose --all-features + + - name: Run dirty tests + uses: actions-rs/cargo@v1 + with: + command: test + args: --verbose --all-features --ignored + + release: + name: Release x86_64-unknown-linux-gnu + runs-on: ubuntu-latest + needs: test + + steps: + - uses: actions/checkout@v2 + + - name: Setup Rust + uses: actions-rs/toolchain@v1 + with: + override: true + profile: minimal + target: x86_64-unknown-linux-gnu + toolchain: nightly + + - name: Build release + uses: actions-rs/cargo@v1 + with: + command: build + args: --release --verbose --all-features --target x86_64-unknown-linux-gnu diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml new file mode 100644 index 0000000..993ff10 --- /dev/null +++ b/.github/workflows/style.yml @@ -0,0 +1,26 @@ +name: Rust Code Formatting + +on: + push: + pull_request: + +jobs: + rustfmt: + name: Rustfmt + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Setup Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + profile: minimal + components: rustfmt + + - name: Check format + uses: actions-rs/cargo@v1 + with: + command: fmt + args: -- --check --verbose diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ffbad3..3f6c929 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased - - Simplify usage by re-exporting serde traits - [#3](https://github.com/ayrat555/fang/pull/2) + - Simplify usage by re-exporting serde traits - [#3](https://github.com/ayrat555/fang/pull/3) ## 0.3.0 (2021-07-04) diff --git a/src/executor.rs b/src/executor.rs index fbb392b..805a7ff 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -152,25 +152,19 @@ impl Executor { Ok(task) => self.storage.finish_task(&task).unwrap(), Err((task, error)) => self.storage.fail_task(&task, error).unwrap(), }; - - () } RetentionMode::RemoveAll => { match result { Ok(task) => self.storage.remove_task(task.id).unwrap(), Err((task, _error)) => self.storage.remove_task(task.id).unwrap(), }; - - () } RetentionMode::RemoveFinished => match result { Ok(task) => { self.storage.remove_task(task.id).unwrap(); - () } Err((task, error)) => { self.storage.fail_task(&task, error).unwrap(); - () } }, } @@ -395,10 +389,10 @@ mod executor_tests { let found_task = executor.storage.find_task_by_id(task.id).unwrap(); assert_eq!(FangTaskState::Failed, found_task.state); - assert_eq!( - "panicked during task execution Any".to_string(), - found_task.error_message.unwrap() - ); + assert!(found_task + .error_message + .unwrap() + .contains("panicked during task execution Any")); Ok(()) }); diff --git a/src/lib.rs b/src/lib.rs index 223e994..7d01087 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![allow(clippy::nonstandard_macro_braces)] + #[macro_use] extern crate diesel; diff --git a/src/postgres.rs b/src/postgres.rs index d384528..05df5c5 100644 --- a/src/postgres.rs +++ b/src/postgres.rs @@ -32,6 +32,12 @@ pub struct Postgres { pub connection: PgConnection, } +impl Default for Postgres { + fn default() -> Self { + Self::new() + } +} + impl Postgres { pub fn new() -> Self { let connection = Self::pg_connection(None); @@ -155,7 +161,7 @@ impl Postgres { .ok() } - fn fetch_task_of_type(&self, task_type: &String) -> Option { + fn fetch_task_of_type(&self, task_type: &str) -> Option { fang_tasks::table .order(fang_tasks::created_at.asc()) .limit(1) diff --git a/src/worker_pool.rs b/src/worker_pool.rs index c5e7f95..05a9748 100644 --- a/src/worker_pool.rs +++ b/src/worker_pool.rs @@ -22,6 +22,12 @@ pub struct WorkerParams { pub task_type: Option, } +impl Default for WorkerParams { + fn default() -> Self { + Self::new() + } +} + impl WorkerParams { pub fn new() -> Self { Self { @@ -67,7 +73,7 @@ impl WorkerPool { .worker_params .task_type .clone() - .unwrap_or("".to_string()); + .unwrap_or_else(|| "".to_string()); let name = format!("worker_{}{}", worker_type, idx); WorkerThread::spawn_in_pool(self.worker_params.clone(), name, 0) } @@ -200,26 +206,19 @@ mod job_pool_tests { assert!(tasks.len() > 40); - let test_worker1_jobs: Vec = tasks - .clone() - .into_iter() - .filter(|job| { - serde_json::to_string(&job.metadata) - .unwrap() - .contains("worker_1") - }) - .collect(); + let test_worker1_jobs = tasks.clone().into_iter().filter(|job| { + serde_json::to_string(&job.metadata) + .unwrap() + .contains("worker_1") + }); - let test_worker2_jobs: Vec = tasks - .into_iter() - .filter(|job| { - serde_json::to_string(&job.metadata) - .unwrap() - .contains("worker_2") - }) - .collect(); + let test_worker2_jobs = tasks.into_iter().filter(|job| { + serde_json::to_string(&job.metadata) + .unwrap() + .contains("worker_2") + }); - assert!(test_worker1_jobs.len() > 20); - assert!(test_worker2_jobs.len() > 20); + assert!(test_worker1_jobs.count() > 20); + assert!(test_worker2_jobs.count() > 20); } }