Skip to content
Advertisement

Schedule tasks are called twice with the @SnapLock annotation

I want only one schedule method to work for many instances. I added the snap-scheduler to my project, and after that I deployed two different instances to the railway. And two entries about the execution of my method are added to the snap_lock table. Сan you explain where I went wrong?

It`s scheduler config:

@Configuration
@Import(SnapAppConfig.class)
public class SchedulerConfiguration {

    private static final String SNAP_DB_POOL_NAME = "snap-pool";

    @Bean(name = "snapDataSource")
    public DataSource customDataSource(final DataSourceProperties properties,
                                       @Value("${spring.datasource.hikari.schema}") final String schema) {
        final HikariDataSource dataSource = properties.initializeDataSourceBuilder()
                .type(HikariDataSource.class).build();

        dataSource.setPoolName(SNAP_DB_POOL_NAME);
        Optional.ofNullable(schema).ifPresent(dataSource::setSchema);

        return dataSource;
    }
}

It`s method with @SnapLock and @Scheduled annotations

@Slf4j
@Service
@RequiredArgsConstructor
public class SchedulerService {

    private static final String EUROPE_KIEV_ZONE = "Europe/Kiev";
    
    @SnapLock(key = "REPORT_CURRENT_TIME", time = 30)
    @Scheduled(cron = "0 */1 * * * *", zone = EUROPE_KIEV_ZONE)
    protected void test() {
        log.info("time now equals = " + Instant.now());
    }
}

It`s psql

CREATE TABLE snap_lock
(
    task_key    text                     NOT NULL,
    task_method text                     NOT NULL,
    lock_until  timestamp with time zone NOT NULL,
    lock_at     timestamp with time zone NOT NULL,
    lock_by     text                     NOT NULL,
    CONSTRAINT snap_lock_pkey PRIMARY KEY (task_key, task_method)
);

CREATE TABLE snap_task_audit
(
    id               integer GENERATED BY DEFAULT AS identity,
    task_key         text                     NOT NULL,
    task_method      text                     NOT NULL,
    run_on           text                     NOT NULL,
    start_run        timestamp with time zone NOT NULL,
    end_run          timestamp with time zone NOT NULL,
    run_time_seconds numeric                  NOT NULL,
    task_error       jsonb,
    CONSTRAINT snap_task_audit_pkey PRIMARY KEY (id)
);

CREATE TABLE snap_scheduler
(
    name            text                     NOT NULL,
    task_key        text                     NOT NULL,
    type            text                     NOT NULL,
    task_class      text                     NOT NULL,
    task_data       jsonb,
    task_data_class text,
    run_at          timestamp with time zone NOT NULL,
    recurrence      text,
    picked          boolean                  NOT NULL DEFAULT false,
    picked_by       text,
    end_run         timestamp with time zone,
    CONSTRAINT snap_scheduler_pkey PRIMARY KEY (task_key)
);

Advertisement

Answer

As an alternative, I solved my problem using a library https://github.com/lukas-krecan/ShedLock. Everything is similar there, the method through the base is blocked. But there were no problems with crowns.

Advertisement