Files
nahodka-back/src/main/java/com/krylov/refound/config/SchedulerLockConfig.java
SlimusMinus a696a21a1f Run safely with several instances: ShedLock jobs, Redis WebSocket fan-out
- Scheduled jobs (nightly cleanup, moderation, search dictionary rebuild) take
  a ShedLock lock in Redis, so only one instance runs each; the moderation run
  stops before its lock can expire.
- STOMP events (chat messages, unread counters, transcoding status) are
  published to the Redis channel ws:broadcast and every instance hands them to
  its local simple broker, so subscribers on other instances receive them.
  Falls back to local delivery if Redis is down; app.websocket.cluster-broadcast
  switches it off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 01:31:12 +03:00

31 lines
1.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.krylov.refound.config;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.redis.spring.RedisLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
/**
* Распределённая блокировка фоновых задач (ShedLock). Приложение может работать
* в нескольких экземплярах: без блокировки ночная чистка и модерация запускались
* бы в каждом из них одновременно. Задача с {@code @SchedulerLock} выполняется
* тем экземпляром, который первым взял блокировку в Redis
* ({@code job-lock:refound:<имя задачи>}); остальные пропускают запуск.
*
* <p>Если Redis недоступен, блокировку взять нельзя и задача пропускается до
* следующего запуска — это безопаснее, чем выполнить её во всех экземплярах.
*/
@Configuration
@EnableSchedulerLock(defaultLockAtMostFor = "PT10M")
public class SchedulerLockConfig {
static final String LOCK_ENVIRONMENT = "refound";
@Bean
public LockProvider lockProvider(RedisConnectionFactory connectionFactory) {
return new RedisLockProvider(connectionFactory, LOCK_ENVIRONMENT);
}
}