ShedLock icon indicating copy to clipboard operation
ShedLock copied to clipboard

Provide more generic lock annotation for non scheduler based contexts

Open holgerstolzenberg opened this issue 2 years ago • 7 comments

We are using ShedLock in our projects and like it pretty much. It's very mature and neatly integrated.

Yesterday I came to the point where I needed a distributed lock in a non scheduler based context.

The use case is to create database table partitions on startup of an Spring boot app by a method annotated with @EventListener(ApplicationStartedEvent.class).

@Transactional
@EventListener(ApplicationStartedEvent.class)
@SchedulerLock(name = INIT_PARTITIONS_LOCK_NAME)
public void init() {
  // lock might not be there for tests where scheduling is disabled
  if (isLockProviderAvailable()) {
    LockAssert.assertLocked();
    log.debug("Distributed lock for partition creation has been obtained");
  }

  log.info("Creating necessary partitions on startup...");
  service.forPartitionCreation().forEach(this::createPartition);
  log.info("All partitions have been successfully created");
}

I know that ShedLock is primarily designed for scheduler based stuff, but I gave it a shot and tried to use it as a "regular" distributed lock for the partition creation. What can I say - it worked.

From this situation a few thoughts arise that might end in a feature request for this project or just documentation stuff, so please forgive me if that is not a typical Github issue.

These are the points that I am thinking about:

  • Is it safe to use @SchedulerLock without @Scheduled
  • If so - is scope of ShedLock much broader than it is advertised for at the moment
  • If so - would it be possible to provide a more generic annotation for the given purpose, e.g. @DistributedLock
  • What other options do we have

Sure I could add another framework for providing the distributed lock or implement something custom in our code base. But why if ShedLock already has everything in its package? Looking at for example https://github.com/alturkovic/distributed-lock you can see that it is pretty close to what ShedLock offers but require an extra lock table if used with JDBC.

I'd rather like to start a discussion upon this to decide whether to actually add a refined issue. If you see a better place to post this, please tell me.

holgerstolzenberg avatar Feb 01 '22 11:02 holgerstolzenberg