You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-11-20 12:02:22 +03:00
Extract the job tracing span logic to a layer
This commit is contained in:
@@ -17,6 +17,7 @@ use apalis_core::{
|
||||
builder::{WorkerBuilder, WorkerFactory},
|
||||
context::JobContext,
|
||||
executor::TokioExecutor,
|
||||
job::Job,
|
||||
job_fn::job_fn,
|
||||
monitor::Monitor,
|
||||
storage::builder::WithStorage,
|
||||
@@ -25,84 +26,80 @@ use chrono::Duration;
|
||||
use mas_email::{Address, EmailVerificationContext, Mailbox};
|
||||
use mas_storage::job::{JobWithSpanContext, VerifyEmailJob};
|
||||
use rand::{distributions::Uniform, Rng};
|
||||
use tracing::{info, info_span, Instrument};
|
||||
use tracing_opentelemetry::OpenTelemetrySpanExt;
|
||||
use tracing::info;
|
||||
|
||||
use crate::{JobContextExt, State};
|
||||
use crate::{layers::TracingLayer, JobContextExt, State};
|
||||
|
||||
#[tracing::instrument(
|
||||
name = "job.verify_email",
|
||||
fields(user_email.id = %job.user_email_id()),
|
||||
skip_all,
|
||||
err(Debug),
|
||||
)]
|
||||
async fn verify_email(
|
||||
job: JobWithSpanContext<VerifyEmailJob>,
|
||||
ctx: JobContext,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let span = info_span!(
|
||||
"job.verify_email",
|
||||
job.id = %ctx.id(),
|
||||
job.attempts = ctx.attempts(),
|
||||
user_email.id = %job.user_email_id(),
|
||||
let state = ctx.state();
|
||||
let mut repo = state.repository().await?;
|
||||
let mut rng = state.rng();
|
||||
let mailer = state.mailer();
|
||||
let clock = state.clock();
|
||||
|
||||
// Lookup the user email
|
||||
let user_email = repo
|
||||
.user_email()
|
||||
.lookup(job.user_email_id())
|
||||
.await?
|
||||
.context("User email not found")?;
|
||||
|
||||
// Lookup the user associated with the email
|
||||
let user = repo
|
||||
.user()
|
||||
.lookup(user_email.user_id)
|
||||
.await?
|
||||
.context("User not found")?;
|
||||
|
||||
// Generate a verification code
|
||||
let range = Uniform::<u32>::from(0..1_000_000);
|
||||
let code = rng.sample(range);
|
||||
let code = format!("{code:06}");
|
||||
|
||||
let address: Address = user_email.email.parse()?;
|
||||
|
||||
// Save the verification code in the database
|
||||
let verification = repo
|
||||
.user_email()
|
||||
.add_verification_code(&mut rng, &clock, &user_email, Duration::hours(8), code)
|
||||
.await?;
|
||||
|
||||
// And send the verification email
|
||||
let mailbox = Mailbox::new(Some(user.username.clone()), address);
|
||||
|
||||
let context = EmailVerificationContext::new(user.clone(), verification.clone());
|
||||
|
||||
mailer.send_verification_email(mailbox, &context).await?;
|
||||
|
||||
info!(
|
||||
email.id = %user_email.id,
|
||||
"Verification email sent"
|
||||
);
|
||||
|
||||
if let Some(context) = job.span_context() {
|
||||
span.add_link(context);
|
||||
}
|
||||
repo.save().await?;
|
||||
|
||||
async move {
|
||||
let state = ctx.state();
|
||||
let mut repo = state.repository().await?;
|
||||
let mut rng = state.rng();
|
||||
let mailer = state.mailer();
|
||||
let clock = state.clock();
|
||||
|
||||
// Lookup the user email
|
||||
let user_email = repo
|
||||
.user_email()
|
||||
.lookup(job.user_email_id())
|
||||
.await?
|
||||
.context("User email not found")?;
|
||||
|
||||
// Lookup the user associated with the email
|
||||
let user = repo
|
||||
.user()
|
||||
.lookup(user_email.user_id)
|
||||
.await?
|
||||
.context("User not found")?;
|
||||
|
||||
// Generate a verification code
|
||||
let range = Uniform::<u32>::from(0..1_000_000);
|
||||
let code = rng.sample(range);
|
||||
let code = format!("{code:06}");
|
||||
|
||||
let address: Address = user_email.email.parse()?;
|
||||
|
||||
// Save the verification code in the database
|
||||
let verification = repo
|
||||
.user_email()
|
||||
.add_verification_code(&mut rng, &clock, &user_email, Duration::hours(8), code)
|
||||
.await?;
|
||||
|
||||
// And send the verification email
|
||||
let mailbox = Mailbox::new(Some(user.username.clone()), address);
|
||||
|
||||
let context = EmailVerificationContext::new(user.clone(), verification.clone());
|
||||
|
||||
mailer.send_verification_email(mailbox, &context).await?;
|
||||
|
||||
info!(
|
||||
email.id = %user_email.id,
|
||||
"Verification email sent"
|
||||
);
|
||||
|
||||
repo.save().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
.instrument(span)
|
||||
.await
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn register(monitor: Monitor<TokioExecutor>, state: &State) -> Monitor<TokioExecutor> {
|
||||
pub(crate) fn register(
|
||||
suffix: &str,
|
||||
monitor: Monitor<TokioExecutor>,
|
||||
state: &State,
|
||||
) -> Monitor<TokioExecutor> {
|
||||
let storage = state.store();
|
||||
let worker = WorkerBuilder::new("verify-email")
|
||||
let worker_name = format!("{job}-{suffix}", job = VerifyEmailJob::NAME);
|
||||
let worker = WorkerBuilder::new(worker_name)
|
||||
.layer(state.inject())
|
||||
.layer(TracingLayer::new())
|
||||
.with_storage(storage)
|
||||
.build(job_fn(verify_email));
|
||||
monitor.register(worker)
|
||||
|
||||
Reference in New Issue
Block a user