Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename redis in app context and config to queue #590

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/demo/config/development.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ database:
# Recreating schema when application loaded. This is a dangerous operation, make sure that you using this flag only on dev environments or test mode
dangerously_recreate: false

# Redis Configuration
redis:
# Queue Configuration
queue:
# Redis connection URI
uri: {{get_env(name="REDIS_URL", default="redis://127.0.0.1")}}
# Dangerously flush all data in Redis on startup. dangerous operation, make sure that you using this flag only on dev environments or test mode
Expand Down
4 changes: 2 additions & 2 deletions examples/demo/config/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ database:
# Recreating schema when application loaded. This is a dangerous operation, make sure that you using this flag only on dev environments or test mode
dangerously_recreate: true

# Redis Configuration
redis:
# Queue Configuration
queue:
# Redis connection URI
uri: {{get_env(name="REDIS_URL", default="redis://127.0.0.1")}}
# Dangerously flush all data in Redis on startup. dangerous operation, make sure that you using this flag only on dev environments or test mode
Expand Down
4 changes: 2 additions & 2 deletions examples/demo/config/teste2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ database:
# Recreating schema when application loaded. This is a dangerous operation, make sure that you using this flag only on dev environments or test mode
dangerously_recreate: true

# Redis Configuration
redis:
# Queue Configuration
queue:
# Redis connection URI
uri: {{get_env(name="APP_REDIS_URI", default="redis://127.0.0.1")}}
# Dangerously flush all data in Redis on startup. dangerous operation, make sure that you using this flag only on dev environments or test mode
Expand Down
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub struct AppContext {
#[cfg(feature = "with-db")]
/// A database connection used by the application.
pub db: DatabaseConnection,
/// An optional connection pool for Redis, for worker tasks
pub redis: Option<Pool<RedisConnectionManager>>,
/// An optional connection pool for Queue, for worker tasks
pub queue: Option<Pool<RedisConnectionManager>>,
/// Configuration settings for the application
pub config: Config,
/// An optional email sender component that can be used to send email.
Expand Down
16 changes: 7 additions & 9 deletions src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,11 @@ pub async fn create_context<H: Hooks>(environment: &Environment) -> Result<AppCo
None
};

let redis = connect_redis(&config).await;

let ctx = AppContext {
environment: environment.clone(),
#[cfg(feature = "with-db")]
db,
redis,
queue: connect_redis(&config).await,
storage: Storage::single(storage::drivers::null::new()).into(),
cache: cache::Cache::new(cache::drivers::null::new()).into(),
config,
Expand All @@ -229,8 +227,8 @@ pub async fn create_app<H: Hooks, M: MigratorTrait>(
let app_context = create_context::<H>(environment).await?;
db::converge::<H, M>(&app_context.db, &app_context.config.database).await?;

if let Some(pool) = &app_context.redis {
redis::converge(pool, &app_context.config.redis).await?;
if let Some(pool) = &app_context.queue {
redis::converge(pool, &app_context.config.queue).await?;
}

run_app::<H>(&mode, app_context).await
Expand Down Expand Up @@ -305,17 +303,17 @@ fn create_processor<H: Hooks>(app_context: &AppContext) -> Result<Processor> {
queues = ?queues,
"registering queues (merged config and default)"
);
let mut p = if let Some(redis) = &app_context.redis {
let mut p = if let Some(queue) = &app_context.queue {
Processor::new(
redis.clone(),
queue.clone(),
DEFAULT_QUEUES
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>(),
)
} else {
return Err(Error::Message(
"redis is missing, cannot initialize workers".to_string(),
"queue is missing, cannot initialize workers".to_string(),
));
};

Expand Down Expand Up @@ -351,7 +349,7 @@ fn create_mailer(config: &config::Mailer) -> Result<Option<EmailSender>> {
// TODO: Refactor to eliminate unwrapping and instead return an appropriate
// error type.
pub async fn connect_redis(config: &Config) -> Option<Pool<RedisConnectionManager>> {
if let Some(redis) = &config.redis {
if let Some(redis) = &config.queue {
let manager = RedisConnectionManager::new(redis.uri.clone()).unwrap();
let redis = Pool::builder().build(manager).await.unwrap();
Some(redis)
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct Config {
pub server: Server,
#[cfg(feature = "with-db")]
pub database: Database,
pub redis: Option<Redis>,
pub queue: Option<Redis>,
pub auth: Option<Auth>,
#[serde(default)]
pub workers: Workers,
Expand Down
2 changes: 1 addition & 1 deletion src/controller/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn health(State(ctx): State<AppContext>) -> Result<Response> {
false
}
};
if let Some(pool) = ctx.redis {
if let Some(pool) = ctx.queue {
if let Err(error) = redis::ping(&pool).await {
tracing::error!(err.msg = %error, err.detail = ?error, "health_redis_ping_error");
is_ok = false;
Expand Down
6 changes: 3 additions & 3 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ where
async fn perform_later(ctx: &AppContext, args: T) -> Result<()> {
match &ctx.config.workers.mode {
WorkerMode::BackgroundQueue => {
if let Some(redis) = &ctx.redis {
Self::perform_async(redis, args).await.unwrap();
if let Some(queue) = &ctx.queue {
Self::perform_async(queue, args).await.unwrap();
} else {
error!(
error.msg =
"worker mode requested but no redis connection supplied, skipping job",
"worker mode requested but no queue connection supplied, skipping job",
"worker_error"
);
}
Expand Down
Loading