1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-08-07 17:03:01 +03:00

mas-tasks: refactor worker building behind a macro

This commit is contained in:
Quentin Gliech
2023-08-03 15:02:40 +02:00
parent 8142cad3d6
commit 026ce2a30c
4 changed files with 44 additions and 89 deletions

View File

@@ -13,25 +13,14 @@
// limitations under the License. // limitations under the License.
use anyhow::Context; use anyhow::Context;
use apalis_core::{ use apalis_core::{context::JobContext, executor::TokioExecutor, monitor::Monitor};
builder::{WorkerBuilder, WorkerFactoryFn},
context::JobContext,
executor::TokioExecutor,
job::Job,
monitor::Monitor,
storage::builder::WithStorage,
};
use chrono::Duration; use chrono::Duration;
use mas_email::{Address, EmailVerificationContext, Mailbox}; use mas_email::{Address, EmailVerificationContext, Mailbox};
use mas_storage::job::{JobWithSpanContext, VerifyEmailJob}; use mas_storage::job::{JobWithSpanContext, VerifyEmailJob};
use rand::{distributions::Uniform, Rng}; use rand::{distributions::Uniform, Rng};
use tracing::info; use tracing::info;
use crate::{ use crate::{storage::PostgresStorageFactory, JobContextExt, State};
storage::PostgresStorageFactory,
utils::{metrics_layer, trace_layer},
JobContextExt, State,
};
#[tracing::instrument( #[tracing::instrument(
name = "job.verify_email", name = "job.verify_email",
@@ -99,16 +88,8 @@ pub(crate) fn register(
state: &State, state: &State,
storage_factory: &PostgresStorageFactory, storage_factory: &PostgresStorageFactory,
) -> Monitor<TokioExecutor> { ) -> Monitor<TokioExecutor> {
let storage = storage_factory.build(); let verify_email_worker =
let worker_name = format!("{job}-{suffix}", job = VerifyEmailJob::NAME); crate::build!(VerifyEmailJob => verify_email, suffix, state, storage_factory);
let worker = WorkerBuilder::new(worker_name)
.layer(state.inject())
.layer(trace_layer())
.layer(metrics_layer())
.with_storage_config(storage, |c| {
c.fetch_interval(std::time::Duration::from_secs(1))
})
.build_fn(verify_email);
monitor.register(worker) monitor.register(verify_email_worker)
} }

View File

@@ -107,6 +107,32 @@ impl JobContextExt for apalis_core::context::JobContext {
} }
} }
/// Helper macro to build a storage-backed worker.
macro_rules! build {
($job:ty => $fn:ident, $suffix:expr, $state:expr, $factory:expr) => {{
let storage = $factory.build();
let worker_name = format!(
"{job}-{suffix}",
job = <$job as ::apalis_core::job::Job>::NAME,
suffix = $suffix
);
let builder = ::apalis_core::builder::WorkerBuilder::new(worker_name)
.layer($state.inject())
.layer(crate::utils::trace_layer())
.layer(crate::utils::metrics_layer());
let builder = ::apalis_core::storage::builder::WithStorage::with_storage_config(
builder,
storage,
|c| c.fetch_interval(std::time::Duration::from_secs(1)),
);
::apalis_core::builder::WorkerFactory::build(builder, ::apalis_core::job_fn::job_fn($fn))
}};
}
pub(crate) use build;
/// Initialise the workers. /// Initialise the workers.
/// ///
/// # Errors /// # Errors

View File

@@ -12,17 +12,8 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::time::Duration;
use anyhow::Context; use anyhow::Context;
use apalis_core::{ use apalis_core::{context::JobContext, executor::TokioExecutor, monitor::Monitor};
builder::{WorkerBuilder, WorkerFactoryFn},
context::JobContext,
executor::TokioExecutor,
job::Job,
monitor::Monitor,
storage::builder::WithStorage,
};
use mas_matrix::ProvisionRequest; use mas_matrix::ProvisionRequest;
use mas_storage::{ use mas_storage::{
job::{DeleteDeviceJob, JobWithSpanContext, ProvisionDeviceJob, ProvisionUserJob}, job::{DeleteDeviceJob, JobWithSpanContext, ProvisionDeviceJob, ProvisionUserJob},
@@ -31,11 +22,7 @@ use mas_storage::{
}; };
use tracing::info; use tracing::info;
use crate::{ use crate::{storage::PostgresStorageFactory, JobContextExt, State};
storage::PostgresStorageFactory,
utils::{metrics_layer, trace_layer},
JobContextExt, State,
};
/// Job to provision a user on the Matrix homeserver. /// Job to provision a user on the Matrix homeserver.
/// This works by doing a PUT request to the /_synapse/admin/v2/users/{user_id} /// This works by doing a PUT request to the /_synapse/admin/v2/users/{user_id}
@@ -163,32 +150,12 @@ pub(crate) fn register(
state: &State, state: &State,
storage_factory: &PostgresStorageFactory, storage_factory: &PostgresStorageFactory,
) -> Monitor<TokioExecutor> { ) -> Monitor<TokioExecutor> {
let storage = storage_factory.build(); let provision_user_worker =
let worker_name = format!("{job}-{suffix}", job = ProvisionUserJob::NAME); crate::build!(ProvisionUserJob => provision_user, suffix, state, storage_factory);
let provision_user_worker = WorkerBuilder::new(worker_name) let provision_device_worker =
.layer(state.inject()) crate::build!(ProvisionDeviceJob => provision_device, suffix, state, storage_factory);
.layer(trace_layer()) let delete_device_worker =
.layer(metrics_layer()) crate::build!(DeleteDeviceJob => delete_device, suffix, state, storage_factory);
.with_storage_config(storage, |c| c.fetch_interval(Duration::from_secs(1)))
.build_fn(provision_user);
let storage = storage_factory.build();
let worker_name = format!("{job}-{suffix}", job = ProvisionDeviceJob::NAME);
let provision_device_worker = WorkerBuilder::new(worker_name)
.layer(state.inject())
.layer(trace_layer())
.layer(metrics_layer())
.with_storage_config(storage, |c| c.fetch_interval(Duration::from_secs(1)))
.build_fn(provision_device);
let storage = storage_factory.build();
let worker_name = format!("{job}-{suffix}", job = DeleteDeviceJob::NAME);
let delete_device_worker = WorkerBuilder::new(worker_name)
.layer(state.inject())
.layer(trace_layer())
.layer(metrics_layer())
.with_storage_config(storage, |c| c.fetch_interval(Duration::from_secs(1)))
.build_fn(delete_device);
monitor monitor
.register(provision_user_worker) .register(provision_user_worker)

View File

@@ -12,29 +12,16 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::time::Duration;
use anyhow::Context; use anyhow::Context;
use apalis_core::{ use apalis_core::{context::JobContext, executor::TokioExecutor, monitor::Monitor};
builder::{WorkerBuilder, WorkerFactoryFn},
context::JobContext,
executor::TokioExecutor,
job::Job,
monitor::Monitor,
storage::builder::WithStorage,
};
use mas_storage::{ use mas_storage::{
job::{DeactivateUserJob, DeleteDeviceJob, JobWithSpanContext}, job::{DeactivateUserJob, JobWithSpanContext},
user::UserRepository, user::UserRepository,
RepositoryAccess, RepositoryAccess,
}; };
use tracing::info; use tracing::info;
use crate::{ use crate::{storage::PostgresStorageFactory, JobContextExt, State};
storage::PostgresStorageFactory,
utils::{metrics_layer, trace_layer},
JobContextExt, State,
};
/// Job to deactivate a user, both locally and on the Matrix homeserver. /// Job to deactivate a user, both locally and on the Matrix homeserver.
#[tracing::instrument( #[tracing::instrument(
@@ -83,14 +70,8 @@ pub(crate) fn register(
state: &State, state: &State,
storage_factory: &PostgresStorageFactory, storage_factory: &PostgresStorageFactory,
) -> Monitor<TokioExecutor> { ) -> Monitor<TokioExecutor> {
let storage = storage_factory.build(); let deactivate_user_worker =
let worker_name = format!("{job}-{suffix}", job = DeleteDeviceJob::NAME); crate::build!(DeactivateUserJob => deactivate_user, suffix, state, storage_factory);
let deactivate_user_worker = WorkerBuilder::new(worker_name)
.layer(state.inject())
.layer(trace_layer())
.layer(metrics_layer())
.with_storage_config(storage, |c| c.fetch_interval(Duration::from_secs(1)))
.build_fn(deactivate_user);
monitor.register(deactivate_user_worker) monitor.register(deactivate_user_worker)
} }