Skip to content

Commit

Permalink
test: triggers + refactor settings (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-online authored Oct 28, 2024
1 parent 99a2a54 commit 12ae54d
Show file tree
Hide file tree
Showing 54 changed files with 706 additions and 458 deletions.
21 changes: 16 additions & 5 deletions .github/workflows/continuous-delivery-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches:
- main
pull_request:
release:
types: [published]

jobs:
push_readme:
Expand All @@ -25,7 +27,7 @@ jobs:
readme_file: 'README.md'

build:
name: Docker Build & Push
name: Build & Push
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -48,11 +50,16 @@ jobs:
echo "PR number: ${{ github.event.number }}"
echo "tag=pr-${{ github.event.number }}" >> "$GITHUB_OUTPUT"
echo "uitag=ui-pr-${{ github.event.number }}" >> "$GITHUB_OUTPUT"
elif [ -n "${{ github.event.release.tag_name }}" ]; then
echo "Release tag: ${{ github.event.release.tag_name }}"
echo "tag=${{ github.event.release.tag_name }}" >> "$GITHUB_OUTPUT"
echo "uitag=ui-${{ github.event.release.tag_name }}" >> "$GITHUB_OUTPUT"
else
echo "Not a PR branch"
echo "tag=latest" >> "$GITHUB_OUTPUT"
echo "uitag=ui" >> "$GITHUB_OUTPUT"
fi
id: tag

- name: Build and push (full)
Expand All @@ -79,8 +86,10 @@ jobs:
push: true
tags: danonline/autopulse:${{ steps.tag.outputs.tag }}-postgres
file: Dockerfile.alpine
cache-from: type=gha
cache-to: type=gha,mode=max
cache-from: |
type=registry,ref=danonline/autopulse:${{ steps.tag.outputs.tag }}
type=gha
# cache-to: type=gha,mode=max
build-args: |
ENABLED_FEATURES=postgres
Expand All @@ -90,8 +99,10 @@ jobs:
push: true
tags: danonline/autopulse:${{ steps.tag.outputs.tag }}-sqlite
file: Dockerfile.alpine
cache-from: type=gha
cache-to: type=gha,mode=max
cache-from: |
type=registry,ref=danonline/autopulse:${{ steps.tag.outputs.tag }}
type=gha
# cache-to: type=gha,mode=max
build-args: |
ENABLED_FEATURES=sqlite
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ jobs:
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-debug-${{ hashFiles('**/Cargo.lock') }}
key: ${{ runner.os }}-cargo-release-${{ hashFiles('**/Cargo.lock') }}

- name: Clean docs folder
run: cargo clean --doc

- name: Build docs
run: cargo doc --no-deps
run: cargo doc --no-deps --release

- name: Add redirect
run: |
Expand All @@ -58,4 +58,7 @@ jobs:
projectName: autopulse
directory: target/doc
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
productionBranch: main
productionBranch: main

- name: Clean docs folder
run: cargo clean --doc
22 changes: 0 additions & 22 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ regex = "1.10.6"
clap = { version = "4.5.18", features = ["derive"] }
clap_derive = { version = "4.5.18" }

[dev-dependencies]
# Standardize logging output
tracing-test = "0.2.5"
# [dev-dependencies]
# # Standardize logging output
# tracing-test = "0.2.5"
6 changes: 1 addition & 5 deletions Dockerfile.alpine
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ WORKDIR /app

# Plan out the build
FROM chef AS planner
COPY Cargo.toml .
RUN mkdir -p src && echo "fn main() {println!(\"if you see this, the build broke\")}" > src/main.rs
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS builder
Expand Down Expand Up @@ -42,9 +41,6 @@ COPY README.md README.md
# Database support: postgres, sqlite
ARG ENABLED_FEATURES="postgres,sqlite"

# Add file to include
ADD default.toml default.toml

RUN cargo build --release --target x86_64-unknown-linux-musl --no-default-features --features ${ENABLED_FEATURES}

FROM alpine AS runtime
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Here's some quick links:

#### Configuration

autopulse requires a configuration file to run. By default, it looks for `config.toml` in the current working directory. You can override the [default values](https://github.com/dan-online/autopulse/blob/main/default.toml) using a config file or by [setting environment variables](https://github.com/dan-online/autopulse/blob/main/example/docker-compose.yml) in the format of: ``AUTOPULSE__{SECTION}__{KEY}``.
autopulse requires a configuration file to run. By default, it looks for `config.toml` in the current working directory. You can override the default values using a config file or by [setting environment variables](https://github.com/dan-online/autopulse/blob/main/example/docker-compose.yml) in the format of: ``AUTOPULSE__{SECTION}__{KEY}``.

For example: `AUTOPULSE__APP__DATABASE_URL`

Expand Down
22 changes: 0 additions & 22 deletions default.toml

This file was deleted.

4 changes: 2 additions & 2 deletions src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum ProcessStatus {

/// Whether a file was found or not.
///
/// Note: only used if [opts.check_path](crate::utils::settings::Opts::check_path) is set.
/// Note: only used if [opts.check_path](crate::settings::opts::Opts::check_path) is set.
#[derive(Serialize)]
pub enum FoundStatus {
Found,
Expand Down Expand Up @@ -77,7 +77,7 @@ pub struct ScanEvent {
/// The status of the file being found.
pub found_status: String,

/// The number of times the scan event has failed. Used for retries and is limited to [opts.max_retries](crate::utils::settings::Opts::max_retries).
/// The number of times the scan event has failed. Used for retries and is limited to [opts.max_retries](crate::settings::opts::Opts::max_retries).
pub failed_times: i32,
/// The time the scan event will be retried.
pub next_retry_at: Option<chrono::NaiveDateTime>,
Expand Down
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! - **[Triggers](service::triggers)**: Create triggers that will be executed by a service when a certain event occurs
//! - **[Targets](service::targets)**: Create targets that will be scanned by a service
//! - **[Webhooks](service::webhooks)**: Send webhooks to services to notify them of an event
//! - **[Settings](utils::settings)**: Settings handler
//! - **[Settings](settings)**: Settings handler
//! - **[Database](db::conn::AnyConnection)**: Database handler
//!
//! ## About
Expand All @@ -23,19 +23,32 @@ use routes::status::status;
use routes::triggers::trigger_post;
use routes::{index::hello, triggers::trigger_get};
use service::manager::PulseManager;
use settings::Settings;
use std::sync::Arc;
use tracing::info;
use tracing_appender::non_blocking::WorkerGuard;
use utils::cli::Args;
use utils::logs::setup_logs;
use utils::settings::Settings;

#[doc(hidden)]
mod tests;

/// Web server routes
pub mod routes;

/// Settings configuration
///
/// Used to configure the service.
///
/// Can be defined in 2 ways:
/// - Config file
/// - `config.{json,toml,yaml,json5,ron,ini}` in the current directory
/// - Environment variables
/// - `AUTOPULSE__{SECTION}__{KEY}` (e.g. `AUTOPULSE__APP__DATABASE_URL`)
///
/// See [Settings] for all options
pub mod settings;

/// Database handler
pub mod db;

Expand Down Expand Up @@ -102,7 +115,7 @@ async fn run(settings: Settings, _guard: Option<WorkerGuard>) -> anyhow::Result<
}

#[doc(hidden)]
fn main() -> anyhow::Result<()> {
pub fn main() -> anyhow::Result<()> {
let args = Args::parse();

let settings = Settings::get_settings(args.config).with_context(|| "Failed to get settings")?;
Expand Down
7 changes: 6 additions & 1 deletion src/routes/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ use std::sync::Arc;

#[derive(Deserialize)]
#[serde(default)]
struct ListQuery {
pub struct ListQuery {
/// The number of items to retrieve per page. (default: 10)
limit: u8,
/// The page number to retrieve. (default: 1)
page: u64,
/// The field to sort the results by. Can be one of `id`, `file_path`, `process_status`, `event_source`, `created_at`, or `updated_at`.
sort: Option<String>,
/// Filter the scan events by process status. Can be one of `pending`, `complete`, `retry`, or `failed`.
status: Option<String>,
/// Filter the scan events by a search query.
search: Option<String>,
}

Expand Down
4 changes: 2 additions & 2 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ pub mod stats;
/// - `limit`: The number of items to retrieve per page.
/// - `sort`: The field to sort the results by. Can be one of `id`, `file_path`, `process_status`, `event_source`, `created_at`, or `updated_at`.
///
/// See [ListQuery](list::ListQuery) for more information.
/// See [list::ListQuery] for more information.
///
/// # Responses
///
/// - **200 OK**: Returns a [ListResponse](list::ListResponse) object containing the list of scan events.
/// - **200 OK**: Returns a list of [ScanEvent](crate::db::models::ScanEvent) objects.
/// - **401 Unauthorized**: Returned if the request is not authenticated.
pub mod list;

Expand Down
8 changes: 5 additions & 3 deletions src/routes/triggers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
db::models::{FoundStatus, NewScanEvent},
service::{manager::PulseManager, triggers::manual::ManualQueryParams, webhooks::EventType},
utils::{check_auth::check_auth, rewrite::rewrite_path, settings::Trigger, sify::sify},
settings::trigger::Trigger,
utils::{check_auth::check_auth, sify::sify},
};
use actix_web::{
get, post,
Expand Down Expand Up @@ -52,7 +53,7 @@ pub async fn trigger_post(
let (mut path, search) = path.clone();

if let Some(rewrite) = &rewrite {
path = rewrite_path(path, rewrite);
path = rewrite.rewrite_path(path);
}

let new_scan_event = NewScanEvent {
Expand Down Expand Up @@ -135,7 +136,8 @@ pub async fn trigger_get(
let mut file_path = query.path.clone();

if let Some(rewrite) = &trigger_settings.rewrite {
file_path = rewrite_path(file_path, rewrite);
// file_path = rewrite_path(file_path, rewrite);
file_path = rewrite.rewrite_path(file_path);
}

let new_scan_event = NewScanEvent {
Expand Down
2 changes: 1 addition & 1 deletion src/service/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
models::{FoundStatus, NewScanEvent, ProcessStatus, ScanEvent},
schema::scan_events::{dsl::scan_events, found_status, process_status},
},
utils::settings::{Settings, Trigger},
settings::{trigger::Trigger, Settings},
};
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl, TextExpressionMethods};
use std::sync::Arc;
Expand Down
3 changes: 2 additions & 1 deletion src/service/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use crate::{
},
},
service::webhooks::WebhookManager,
utils::{settings::Settings, sify::sify},
settings::Settings,
utils::sify::sify,
};
use diesel::{BoolExpressionMethods, ExpressionMethods, QueryDsl, RunQueryDsl};
use std::{path::PathBuf, sync::Arc};
Expand Down
2 changes: 1 addition & 1 deletion src/service/targets/autopulse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
db::models::ScanEvent,
utils::settings::{Auth, TargetProcess},
settings::{auth::Auth, target::TargetProcess},
};
use reqwest::header;
use serde::Deserialize;
Expand Down
2 changes: 1 addition & 1 deletion src/service/targets/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{db::models::ScanEvent, utils::settings::TargetProcess};
use crate::{db::models::ScanEvent, settings::target::TargetProcess};
use serde::Deserialize;
use tracing::{debug, error};

Expand Down
10 changes: 6 additions & 4 deletions src/service/targets/emby.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::{collections::HashMap, fmt::Display, io::Cursor};

use crate::{
db::models::ScanEvent,
utils::{default_true::default_true, settings::TargetProcess},
};
use crate::{db::models::ScanEvent, settings::target::TargetProcess};
use reqwest::header;
use serde::{Deserialize, Serialize};
use struson::{
Expand All @@ -12,6 +9,11 @@ use struson::{
};
use tracing::{debug, error};

#[doc(hidden)]
fn default_true() -> bool {
true
}

#[derive(Clone, Deserialize)]
pub struct Emby {
/// URL to the Jellyfin/Emby server
Expand Down
2 changes: 1 addition & 1 deletion src/service/targets/fileflows.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{db::models::ScanEvent, utils::settings::TargetProcess};
use crate::{db::models::ScanEvent, settings::target::TargetProcess};
use reqwest::header;
use serde::Deserialize;
use tracing::error;
Expand Down
2 changes: 1 addition & 1 deletion src/service/targets/plex.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{db::models::ScanEvent, utils::settings::TargetProcess};
use crate::{db::models::ScanEvent, settings::target::TargetProcess};
use reqwest::header;
use serde::Deserialize;
use tracing::{debug, error, trace};
Expand Down
2 changes: 1 addition & 1 deletion src/service/targets/tdarr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use reqwest::header;
use serde::{Deserialize, Serialize};

use crate::{db::models::ScanEvent, utils::settings::TargetProcess};
use crate::{db::models::ScanEvent, settings::target::TargetProcess};

#[derive(Deserialize, Clone)]
pub struct Tdarr {
Expand Down
2 changes: 1 addition & 1 deletion src/service/triggers/lidarr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::settings::{Rewrite, Timer, TriggerRequest};
use crate::settings::{rewrite::Rewrite, timer::Timer, trigger::TriggerRequest};
use serde::Deserialize;

#[derive(Deserialize, Clone)]
Expand Down
Loading

0 comments on commit 12ae54d

Please sign in to comment.