feat(watcher): Ajout watcher de fichiers temps réel
- Migration 0006: colonne watcher_enabled
- Crate notify pour surveillance FS temps réel (FSEvents/inotify)
- Watcher redémarré toutes les 30s si config change
- Détection instantanée création/modification/suppression
- Création job immédiate quand fichier détecté
- API: support watcher_enabled dans UpdateMonitoringRequest
- Backoffice: toggle Watcher avec indicateur ⚡
- Fonctionne en parallèle du scheduler auto-scan
Usage: Activer Watcher + Auto-scan pour réactivité max
This commit is contained in:
@@ -19,6 +19,7 @@ pub struct LibraryResponse {
|
||||
pub monitor_enabled: bool,
|
||||
pub scan_mode: String,
|
||||
pub next_scan_at: Option<chrono::DateTime<chrono::Utc>>,
|
||||
pub watcher_enabled: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, ToSchema)]
|
||||
@@ -43,7 +44,7 @@ pub struct CreateLibraryRequest {
|
||||
)]
|
||||
pub async fn list_libraries(State(state): State<AppState>) -> Result<Json<Vec<LibraryResponse>>, ApiError> {
|
||||
let rows = sqlx::query(
|
||||
"SELECT l.id, l.name, l.root_path, l.enabled, l.monitor_enabled, l.scan_mode, l.next_scan_at,
|
||||
"SELECT l.id, l.name, l.root_path, l.enabled, l.monitor_enabled, l.scan_mode, l.next_scan_at, l.watcher_enabled,
|
||||
(SELECT COUNT(*) FROM books b WHERE b.library_id = l.id) as book_count
|
||||
FROM libraries l ORDER BY l.created_at DESC"
|
||||
)
|
||||
@@ -61,6 +62,7 @@ pub async fn list_libraries(State(state): State<AppState>) -> Result<Json<Vec<Li
|
||||
monitor_enabled: row.get("monitor_enabled"),
|
||||
scan_mode: row.get("scan_mode"),
|
||||
next_scan_at: row.get("next_scan_at"),
|
||||
watcher_enabled: row.get("watcher_enabled"),
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -111,6 +113,7 @@ pub async fn create_library(
|
||||
monitor_enabled: false,
|
||||
scan_mode: "manual".to_string(),
|
||||
next_scan_at: None,
|
||||
watcher_enabled: false,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -225,6 +228,7 @@ pub struct UpdateMonitoringRequest {
|
||||
pub monitor_enabled: bool,
|
||||
#[schema(value_type = String, example = "hourly")]
|
||||
pub scan_mode: String, // 'manual', 'hourly', 'daily', 'weekly'
|
||||
pub watcher_enabled: Option<bool>,
|
||||
}
|
||||
|
||||
/// Update monitoring settings for a library
|
||||
@@ -268,13 +272,16 @@ pub async fn update_monitoring(
|
||||
None
|
||||
};
|
||||
|
||||
let watcher_enabled = input.watcher_enabled.unwrap_or(false);
|
||||
|
||||
let result = sqlx::query(
|
||||
"UPDATE libraries SET monitor_enabled = $2, scan_mode = $3, next_scan_at = $4 WHERE id = $1 RETURNING id, name, root_path, enabled, monitor_enabled, scan_mode, next_scan_at"
|
||||
"UPDATE libraries SET monitor_enabled = $2, scan_mode = $3, next_scan_at = $4, watcher_enabled = $5 WHERE id = $1 RETURNING id, name, root_path, enabled, monitor_enabled, scan_mode, next_scan_at, watcher_enabled"
|
||||
)
|
||||
.bind(library_id)
|
||||
.bind(input.monitor_enabled)
|
||||
.bind(input.scan_mode)
|
||||
.bind(next_scan_at)
|
||||
.bind(watcher_enabled)
|
||||
.fetch_optional(&state.pool)
|
||||
.await?;
|
||||
|
||||
@@ -296,5 +303,6 @@ pub async fn update_monitoring(
|
||||
monitor_enabled: row.get("monitor_enabled"),
|
||||
scan_mode: row.get("scan_mode"),
|
||||
next_scan_at: row.get("next_scan_at"),
|
||||
watcher_enabled: row.get("watcher_enabled"),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -6,9 +6,10 @@ interface MonitoringFormProps {
|
||||
libraryId: string;
|
||||
monitorEnabled: boolean;
|
||||
scanMode: string;
|
||||
watcherEnabled: boolean;
|
||||
}
|
||||
|
||||
export function MonitoringForm({ libraryId, monitorEnabled, scanMode }: MonitoringFormProps) {
|
||||
export function MonitoringForm({ libraryId, monitorEnabled, scanMode, watcherEnabled }: MonitoringFormProps) {
|
||||
const [isPending, startTransition] = useTransition();
|
||||
|
||||
const handleSubmit = (formData: FormData) => {
|
||||
@@ -20,6 +21,7 @@ export function MonitoringForm({ libraryId, monitorEnabled, scanMode }: Monitori
|
||||
body: JSON.stringify({
|
||||
monitor_enabled: formData.get("monitor_enabled") === "true",
|
||||
scan_mode: formData.get("scan_mode"),
|
||||
watcher_enabled: formData.get("watcher_enabled") === "true",
|
||||
}),
|
||||
});
|
||||
if (response.ok) {
|
||||
@@ -34,16 +36,29 @@ export function MonitoringForm({ libraryId, monitorEnabled, scanMode }: Monitori
|
||||
return (
|
||||
<form action={handleSubmit} className="monitoring-form">
|
||||
<input type="hidden" name="id" value={libraryId} />
|
||||
<label className={`monitor-toggle ${isPending ? 'pending' : ''}`}>
|
||||
<input
|
||||
type="checkbox"
|
||||
name="monitor_enabled"
|
||||
value="true"
|
||||
defaultChecked={monitorEnabled}
|
||||
disabled={isPending}
|
||||
/>
|
||||
Auto
|
||||
</label>
|
||||
<div className="monitor-options">
|
||||
<label className={`monitor-toggle ${isPending ? 'pending' : ''}`}>
|
||||
<input
|
||||
type="checkbox"
|
||||
name="monitor_enabled"
|
||||
value="true"
|
||||
defaultChecked={monitorEnabled}
|
||||
disabled={isPending}
|
||||
/>
|
||||
<span className="toggle-label">Auto-scan</span>
|
||||
</label>
|
||||
<label className={`monitor-toggle watcher-toggle ${isPending ? 'pending' : ''} ${watcherEnabled ? 'active' : ''}`}>
|
||||
<input
|
||||
type="checkbox"
|
||||
name="watcher_enabled"
|
||||
value="true"
|
||||
defaultChecked={watcherEnabled}
|
||||
disabled={isPending}
|
||||
/>
|
||||
<span className="toggle-label">Watcher</span>
|
||||
<span className="toggle-hint" title="Detects file changes in real-time">⚡</span>
|
||||
</label>
|
||||
</div>
|
||||
<select
|
||||
name="scan_mode"
|
||||
defaultValue={scanMode}
|
||||
|
||||
@@ -1234,3 +1234,27 @@ tr.job-highlighted td {
|
||||
font-size: 0.75rem;
|
||||
padding: 2px 4px;
|
||||
}
|
||||
|
||||
/* Watcher toggle styles */
|
||||
.watcher-toggle {
|
||||
background: hsl(45 93% 90% / 0.3);
|
||||
border: 1px solid hsl(45 93% 47% / 0.3);
|
||||
border-radius: 4px;
|
||||
padding: 2px 6px;
|
||||
}
|
||||
|
||||
.watcher-toggle.active {
|
||||
background: hsl(45 93% 90% / 0.6);
|
||||
border-color: hsl(45 93% 47% / 0.6);
|
||||
}
|
||||
|
||||
.toggle-hint {
|
||||
margin-left: 4px;
|
||||
cursor: help;
|
||||
}
|
||||
|
||||
.monitor-options {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ export type LibraryDto = {
|
||||
monitor_enabled: boolean;
|
||||
scan_mode: string;
|
||||
next_scan_at: string | null;
|
||||
watcher_enabled: boolean;
|
||||
};
|
||||
|
||||
export type IndexJobDto = {
|
||||
@@ -135,10 +136,17 @@ export async function scanLibrary(libraryId: string, full?: boolean) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateLibraryMonitoring(libraryId: string, monitorEnabled: boolean, scanMode: string) {
|
||||
export async function updateLibraryMonitoring(libraryId: string, monitorEnabled: boolean, scanMode: string, watcherEnabled?: boolean) {
|
||||
const body: { monitor_enabled: boolean; scan_mode: string; watcher_enabled?: boolean } = {
|
||||
monitor_enabled: monitorEnabled,
|
||||
scan_mode: scanMode,
|
||||
};
|
||||
if (watcherEnabled !== undefined) {
|
||||
body.watcher_enabled = watcherEnabled;
|
||||
}
|
||||
return apiFetch<LibraryDto>(`/libraries/${libraryId}/monitoring`, {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify({ monitor_enabled: monitorEnabled, scan_mode: scanMode })
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ license.workspace = true
|
||||
anyhow.workspace = true
|
||||
axum.workspace = true
|
||||
chrono.workspace = true
|
||||
notify = "6.1"
|
||||
parsers = { path = "../../crates/parsers" }
|
||||
reqwest.workspace = true
|
||||
serde.workspace = true
|
||||
|
||||
@@ -2,12 +2,14 @@ use anyhow::Context;
|
||||
use axum::{extract::State, routing::get, Json, Router};
|
||||
use chrono::{DateTime, Utc};
|
||||
use axum::http::StatusCode;
|
||||
use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
|
||||
use parsers::{detect_format, parse_metadata, BookFormat};
|
||||
use serde::Serialize;
|
||||
use sha2::{Digest, Sha256};
|
||||
use sqlx::{postgres::PgPoolOptions, Row};
|
||||
use std::{collections::HashMap, path::Path, time::Duration};
|
||||
use stripstream_core::config::IndexerConfig;
|
||||
use tokio::sync::mpsc;
|
||||
use tracing::{error, info, trace, warn};
|
||||
use uuid::Uuid;
|
||||
use walkdir::WalkDir;
|
||||
@@ -92,6 +94,16 @@ async fn ready(State(state): State<AppState>) -> Result<Json<serde_json::Value>,
|
||||
|
||||
async fn run_worker(state: AppState, interval_seconds: u64) {
|
||||
let wait = Duration::from_secs(interval_seconds.max(1));
|
||||
|
||||
// Start file watcher task
|
||||
let watcher_state = state.clone();
|
||||
let _watcher_handle = tokio::spawn(async move {
|
||||
info!("[WATCHER] Starting file watcher service");
|
||||
if let Err(err) = run_file_watcher(watcher_state).await {
|
||||
error!("[WATCHER] Error: {}", err);
|
||||
}
|
||||
});
|
||||
|
||||
// Start scheduler task for auto-monitoring
|
||||
let scheduler_state = state.clone();
|
||||
let _scheduler_handle = tokio::spawn(async move {
|
||||
@@ -127,6 +139,140 @@ async fn run_worker(state: AppState, interval_seconds: u64) {
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_file_watcher(state: AppState) -> anyhow::Result<()> {
|
||||
let (tx, mut rx) = mpsc::channel::<(Uuid, String)>(100);
|
||||
|
||||
// Start watcher refresh loop
|
||||
let refresh_interval = Duration::from_secs(30);
|
||||
let pool = state.pool.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let mut watcher: Option<RecommendedWatcher> = None;
|
||||
let mut watched_libraries: HashMap<Uuid, String> = HashMap::new();
|
||||
|
||||
loop {
|
||||
// Get libraries with watcher enabled
|
||||
match sqlx::query(
|
||||
"SELECT id, root_path FROM libraries WHERE watcher_enabled = TRUE AND enabled = TRUE"
|
||||
)
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
{
|
||||
Ok(rows) => {
|
||||
let current_libraries: HashMap<Uuid, String> = rows
|
||||
.into_iter()
|
||||
.map(|row| {
|
||||
let id: Uuid = row.get("id");
|
||||
let root_path: String = row.get("root_path");
|
||||
let local_path = remap_libraries_path(&root_path);
|
||||
(id, local_path)
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Check if we need to recreate watcher
|
||||
let needs_restart = watched_libraries.len() != current_libraries.len()
|
||||
|| watched_libraries.iter().any(|(id, path)| {
|
||||
current_libraries.get(id) != Some(path)
|
||||
});
|
||||
|
||||
if needs_restart {
|
||||
info!("[WATCHER] Restarting watcher for {} libraries", current_libraries.len());
|
||||
|
||||
// Drop old watcher
|
||||
watcher = None;
|
||||
watched_libraries.clear();
|
||||
|
||||
if !current_libraries.is_empty() {
|
||||
let tx_clone = tx.clone();
|
||||
let libraries_clone = current_libraries.clone();
|
||||
|
||||
match setup_watcher(libraries_clone, tx_clone) {
|
||||
Ok(new_watcher) => {
|
||||
watcher = Some(new_watcher);
|
||||
watched_libraries = current_libraries;
|
||||
info!("[WATCHER] Watching {} libraries", watched_libraries.len());
|
||||
}
|
||||
Err(err) => {
|
||||
error!("[WATCHER] Failed to setup watcher: {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
error!("[WATCHER] Failed to fetch libraries: {}", err);
|
||||
}
|
||||
}
|
||||
|
||||
tokio::time::sleep(refresh_interval).await;
|
||||
}
|
||||
});
|
||||
|
||||
// Process watcher events
|
||||
while let Some((library_id, file_path)) = rx.recv().await {
|
||||
info!("[WATCHER] File changed in library {}: {}", library_id, file_path);
|
||||
|
||||
// Check if there's already a pending job for this library
|
||||
match sqlx::query_scalar::<_, bool>(
|
||||
"SELECT EXISTS(SELECT 1 FROM index_jobs WHERE library_id = $1 AND status IN ('pending', 'running'))"
|
||||
)
|
||||
.bind(library_id)
|
||||
.fetch_one(&state.pool)
|
||||
.await
|
||||
{
|
||||
Ok(exists) => {
|
||||
if !exists {
|
||||
// Create a quick scan job
|
||||
let job_id = Uuid::new_v4();
|
||||
match sqlx::query(
|
||||
"INSERT INTO index_jobs (id, library_id, type, status) VALUES ($1, $2, 'rebuild', 'pending')"
|
||||
)
|
||||
.bind(job_id)
|
||||
.bind(library_id)
|
||||
.execute(&state.pool)
|
||||
.await
|
||||
{
|
||||
Ok(_) => info!("[WATCHER] Created job {} for library {}", job_id, library_id),
|
||||
Err(err) => error!("[WATCHER] Failed to create job: {}", err),
|
||||
}
|
||||
} else {
|
||||
trace!("[WATCHER] Job already pending for library {}, skipping", library_id);
|
||||
}
|
||||
}
|
||||
Err(err) => error!("[WATCHER] Failed to check existing jobs: {}", err),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn setup_watcher(
|
||||
libraries: HashMap<Uuid, String>,
|
||||
tx: mpsc::Sender<(Uuid, String)>,
|
||||
) -> anyhow::Result<RecommendedWatcher> {
|
||||
let watcher = notify::recommended_watcher(move |res: Result<Event, notify::Error>| {
|
||||
match res {
|
||||
Ok(event) => {
|
||||
if event.kind.is_modify() || event.kind.is_create() || event.kind.is_remove() {
|
||||
for path in event.paths {
|
||||
if let Some((library_id, _)) = libraries.iter().find(|(_, root)| {
|
||||
path.starts_with(root)
|
||||
}) {
|
||||
let path_str = path.to_string_lossy().to_string();
|
||||
if detect_format(&path).is_some() {
|
||||
let _ = tx.try_send((*library_id, path_str));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => error!("[WATCHER] Event error: {}", err),
|
||||
}
|
||||
})?;
|
||||
|
||||
Ok(watcher)
|
||||
}
|
||||
|
||||
async fn check_and_schedule_auto_scans(pool: &sqlx::PgPool) -> anyhow::Result<()> {
|
||||
let libraries = sqlx::query(
|
||||
r#"
|
||||
|
||||
Reference in New Issue
Block a user