1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//! Webhook and script notification handling.
use crate::types::{DownloadId, Event};
use std::path::PathBuf;
use std::sync::Arc;
use super::UsenetDownloader;
/// Parameters for triggering webhooks
pub struct TriggerWebhooksParams {
/// The webhook event that occurred
pub event_type: crate::config::WebhookEvent,
/// The ID of the download
pub download_id: DownloadId,
/// The download name
pub name: String,
/// Optional category
pub category: Option<String>,
/// Current download status as string
pub status: String,
/// Optional destination path (for completed downloads)
pub destination: Option<PathBuf>,
/// Optional error message (for failed downloads)
pub error: Option<String>,
}
/// Parameters for triggering scripts
pub struct TriggerScriptsParams {
/// The script event that occurred
pub event_type: crate::config::ScriptEvent,
/// The ID of the download
pub download_id: DownloadId,
/// The download name
pub name: String,
/// Optional category
pub category: Option<String>,
/// Current download status as string
pub status: String,
/// Optional destination path (for completed downloads)
pub destination: Option<PathBuf>,
/// Optional error message (for failed downloads)
pub error: Option<String>,
/// Size in bytes
pub size_bytes: u64,
}
impl UsenetDownloader {
/// Trigger webhooks for download events
///
/// This method sends HTTP POST requests to all configured webhooks that are
/// subscribed to the given event type. Webhooks are executed asynchronously
/// (fire and forget) to avoid blocking the download pipeline.
pub(crate) fn trigger_webhooks(&self, params: TriggerWebhooksParams) {
let TriggerWebhooksParams {
event_type,
download_id,
name,
category,
status,
destination,
error,
} = params;
// Filter to only webhooks that match this event type before cloning
let matching_webhooks: Vec<_> = self
.config
.notifications
.webhooks
.iter()
.filter(|w| w.events.contains(&event_type))
.cloned()
.collect();
// Early return if no webhooks are subscribed
if matching_webhooks.is_empty() {
return;
}
let event_tx = self.event_tx.clone();
// Spawn async task to send webhooks (fire and forget)
tokio::spawn(async move {
let timestamp = chrono::Utc::now().timestamp();
// Pre-compute event string once (not per webhook)
let event_str: &'static str = match event_type {
crate::config::WebhookEvent::OnComplete => "complete",
crate::config::WebhookEvent::OnFailed => "failed",
crate::config::WebhookEvent::OnQueued => "queued",
};
// Build shared payload once - use Arc to share across webhooks
let payload = Arc::new(crate::types::WebhookPayload {
event: event_str.to_string(),
download_id,
name,
category,
status,
destination,
error,
timestamp,
});
for webhook in matching_webhooks {
// Build HTTP client for this webhook
let client = reqwest::Client::new();
let mut request = client
.post(&webhook.url)
.json(payload.as_ref())
.timeout(webhook.timeout);
// Add authentication header if configured
if let Some(auth) = &webhook.auth_header {
request = request.header("Authorization", auth);
}
// url is moved into the async block, only cloned for error reporting if needed
let url = webhook.url;
let timeout = webhook.timeout;
let result = tokio::time::timeout(timeout, request.send()).await;
// Handle webhook response
match result {
Ok(Ok(response)) => {
if !response.status().is_success() {
let error_msg = format!(
"Webhook returned status {}: {}",
response.status(),
response.text().await.unwrap_or_default()
);
tracing::warn!(url = %url, error = %error_msg, "webhook failed");
event_tx
.send(Event::WebhookFailed {
url,
error: error_msg,
})
.ok();
} else {
tracing::debug!(url = %url, "webhook sent successfully");
}
}
Ok(Err(e)) => {
let error_msg = format!("Failed to send webhook: {}", e);
tracing::warn!(url = %url, error = %error_msg, "webhook failed");
event_tx
.send(Event::WebhookFailed {
url,
error: error_msg,
})
.ok();
}
Err(_) => {
let error_msg = format!("Webhook timed out after {:?}", timeout);
tracing::warn!(url = %url, error = %error_msg, "webhook timeout");
event_tx
.send(Event::WebhookFailed {
url,
error: error_msg,
})
.ok();
}
}
}
});
}
/// Trigger scripts for download events
///
/// This method executes all configured scripts (both global and category-specific)
/// that are subscribed to the given event type. Scripts are executed asynchronously
/// (fire and forget) to avoid blocking the download pipeline.
///
/// # Execution Order
///
/// 1. Category-specific scripts (if download has a category)
/// 2. Global scripts
pub(crate) fn trigger_scripts(&self, params: TriggerScriptsParams) {
let TriggerScriptsParams {
event_type,
download_id,
name,
category,
status,
destination,
error,
size_bytes,
} = params;
use std::collections::HashMap;
// Build environment variables
let mut env_vars: HashMap<String, String> = HashMap::new();
env_vars.insert("USENET_DL_ID".to_string(), download_id.to_string());
env_vars.insert("USENET_DL_NAME".to_string(), name.clone());
env_vars.insert("USENET_DL_STATUS".to_string(), status.clone());
env_vars.insert("USENET_DL_SIZE".to_string(), size_bytes.to_string());
if let Some(cat) = &category {
env_vars.insert("USENET_DL_CATEGORY".to_string(), cat.clone());
}
if let Some(dest) = &destination {
env_vars.insert(
"USENET_DL_DESTINATION".to_string(),
dest.display().to_string(),
);
}
if let Some(err) = &error {
env_vars.insert("USENET_DL_ERROR".to_string(), err.clone());
}
// Category scripts first
if let Some(cat_name) = &category
&& let Some(cat_config) = self.config.persistence.categories.get(cat_name)
{
// Check if any category scripts match this event before cloning
let matching_scripts: Vec<_> = cat_config
.scripts
.iter()
.filter(|s| s.events.contains(&event_type))
.collect();
if !matching_scripts.is_empty() {
// Only clone env_vars if we have matching scripts
let mut cat_env_vars = env_vars.clone();
cat_env_vars.insert(
"USENET_DL_CATEGORY_DESTINATION".to_string(),
cat_config.destination.display().to_string(),
);
cat_env_vars.insert(
"USENET_DL_IS_CATEGORY_SCRIPT".to_string(),
"true".to_string(),
);
for script in matching_scripts {
self.run_script_async(&script.path, script.timeout, &cat_env_vars);
}
}
}
// Then global scripts - only clone for matching scripts
let matching_global: Vec<_> = self
.config
.notifications
.scripts
.iter()
.filter(|s| s.events.contains(&event_type))
.collect();
for script in matching_global {
self.run_script_async(&script.path, script.timeout, &env_vars);
}
}
/// Execute a script asynchronously (fire and forget)
///
/// This method spawns a tokio task to execute the script with the given
/// environment variables and timeout. It emits a ScriptFailed event if the
/// script fails or times out.
fn run_script_async(
&self,
script_path: &std::path::Path,
timeout: std::time::Duration,
env_vars: &std::collections::HashMap<String, String>,
) {
let script_path = script_path.to_path_buf();
let event_tx = self.event_tx.clone();
let env_vars = env_vars.clone();
tokio::spawn(async move {
// Execute the script with timeout
let result = tokio::time::timeout(
timeout,
tokio::process::Command::new(&script_path)
.envs(&env_vars)
.output(),
)
.await;
// Handle script execution result
match result {
Ok(Ok(output)) => {
if !output.status.success() {
let exit_code = output.status.code();
tracing::warn!(
script = ?script_path,
code = ?exit_code,
"notification script failed"
);
event_tx
.send(Event::ScriptFailed {
script: script_path.clone(),
exit_code,
})
.ok();
} else {
tracing::debug!(script = ?script_path, "script executed successfully");
}
}
Ok(Err(e)) => {
tracing::warn!(script = ?script_path, error = %e, "failed to run script");
event_tx
.send(Event::ScriptFailed {
script: script_path.clone(),
exit_code: None,
})
.ok();
}
Err(_) => {
tracing::warn!(script = ?script_path, timeout = ?timeout, "script timed out");
event_tx
.send(Event::ScriptFailed {
script: script_path.clone(),
exit_code: None,
})
.ok();
}
}
});
}
}