Skip to content

Commit 2719584

Browse files
committed
worker: plumb Emails through the environment
1 parent 6b0e026 commit 2719584

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

src/bin/background-worker.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
extern crate tracing;
1717

1818
use crates_io::cloudfront::CloudFront;
19-
use crates_io::config;
2019
use crates_io::db::DieselPool;
2120
use crates_io::fastly::Fastly;
2221
use crates_io::storage::Storage;
2322
use crates_io::worker::swirl::Runner;
2423
use crates_io::worker::{Environment, RunnerExt};
24+
use crates_io::{config, Emails};
2525
use crates_io::{db, ssh};
2626
use crates_io_env_vars::{var, var_parsed};
2727
use crates_io_index::RepositoryConfig;
@@ -73,6 +73,7 @@ fn main() -> anyhow::Result<()> {
7373
.build()
7474
.expect("Couldn't build client");
7575

76+
let emails = Emails::from_environment(&config);
7677
let fastly = Fastly::from_environment(client);
7778

7879
let connection_pool = r2d2::Pool::builder()
@@ -88,6 +89,7 @@ fn main() -> anyhow::Result<()> {
8889
fastly,
8990
storage,
9091
connection_pool.clone(),
92+
emails,
9193
);
9294

9395
let environment = Arc::new(environment);

src/email.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::path::PathBuf;
2-
use std::sync::Mutex;
2+
use std::sync::{Arc, Mutex};
33

44
use crate::util::errors::{server_error, AppResult};
55

@@ -12,7 +12,7 @@ use lettre::transport::smtp::SmtpTransport;
1212
use lettre::{Message, Transport};
1313
use rand::distributions::{Alphanumeric, DistString};
1414

15-
#[derive(Debug)]
15+
#[derive(Debug, Clone)]
1616
pub struct Emails {
1717
backend: EmailBackend,
1818
}
@@ -48,7 +48,7 @@ impl Emails {
4848
pub fn new_in_memory() -> Self {
4949
Self {
5050
backend: EmailBackend::Memory {
51-
mails: Mutex::new(Vec::new()),
51+
mails: Arc::new(Mutex::new(Vec::new())),
5252
},
5353
}
5454
}
@@ -204,6 +204,7 @@ Source type: {source}\n",
204204
}
205205
}
206206

207+
#[derive(Clone)]
207208
enum EmailBackend {
208209
/// Backend used in production to send mails using SMTP.
209210
Smtp {
@@ -214,7 +215,7 @@ enum EmailBackend {
214215
/// Backend used locally during development, will store the emails in the provided directory.
215216
FileSystem { path: PathBuf },
216217
/// Backend used during tests, will keep messages in memory to allow tests to retrieve them.
217-
Memory { mails: Mutex<Vec<StoredEmail>> },
218+
Memory { mails: Arc<Mutex<Vec<StoredEmail>>> },
218219
}
219220

220221
// Custom Debug implementation to avoid showing the SMTP password.

src/tests/util/test_app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ impl TestAppBuilder {
272272
None,
273273
app.storage.clone(),
274274
app.primary_database.clone(),
275+
app.emails.clone(),
275276
);
276277

277278
let runner = Runner::new(app.primary_database.clone(), Arc::new(environment))

src/worker/environment.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::cloudfront::CloudFront;
22
use crate::db::DieselPool;
33
use crate::fastly::Fastly;
44
use crate::storage::Storage;
5+
use crate::Emails;
56
use crates_io_index::{Repository, RepositoryConfig};
67
use parking_lot::{Mutex, MutexGuard};
78
use std::ops::{Deref, DerefMut};
@@ -15,6 +16,7 @@ pub struct Environment {
1516
fastly: Option<Fastly>,
1617
pub storage: Arc<Storage>,
1718
pub connection_pool: DieselPool,
19+
pub emails: Emails,
1820
}
1921

2022
impl Environment {
@@ -24,6 +26,7 @@ impl Environment {
2426
fastly: Option<Fastly>,
2527
storage: Arc<Storage>,
2628
connection_pool: DieselPool,
29+
emails: Emails,
2730
) -> Self {
2831
Self {
2932
repository_config,
@@ -32,6 +35,7 @@ impl Environment {
3235
fastly,
3336
storage,
3437
connection_pool,
38+
emails,
3539
}
3640
}
3741

0 commit comments

Comments
 (0)