All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fi.evolver.basics.spring.timer.ScheduledTaskRepository Maven / Gradle / Ivy

There is a newer version: 6.5.1
Show newest version
package fi.evolver.basics.spring.timer;

import java.time.Instant;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import fi.evolver.basics.spring.timer.entity.ScheduledTask;
import fi.evolver.basics.spring.timer.entity.ScheduledTask.State;
import fi.evolver.utils.ServerUtils;


@Repository
public interface ScheduledTaskRepository extends JpaRepository {

	@Modifying
	@Query("UPDATE ScheduledTask t SET t.state = fi.evolver.basics.spring.timer.entity.ScheduledTask$State.WAITING WHERE " +
			"  t.state = fi.evolver.basics.spring.timer.entity.ScheduledTask$State.RUNNING AND " +
			"  t.lastFiredTime IS NOT NULL AND " +
			"  t.lastFiredTime + t.timeoutMs < :now ")
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	int resetTimedOutTasks(long now);


	@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true)
	List findAllByNextFireTimeIsNull();


	@Modifying
	@Query("UPDATE ScheduledTask t SET " +
			"  t.nextFireTime = :nextFireTime," +
			"  t.lastFiredTime = 0" +
			"WHERE " +
			"  t.id = :id AND " +
			"  t.lastFiredTime IS NULL ")
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	int scheduleUnscheduledTask(long id, long nextFireTime);


	@Modifying
	@Query("UPDATE ScheduledTask t SET " +
			"  t.state = fi.evolver.basics.spring.timer.entity.ScheduledTask$State.PAUSED " +
			"WHERE " +
			"  t.id = :id AND " +
			"  t.state = :currentState AND " +
			"  t.lastFiredTime = :lastFiredTime")
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	int pauseTask(long id, State currentState, long lastFiredTime);


	@Modifying
	@Query("UPDATE ScheduledTask t SET " +
			"  t.lastFinishedTime = :lastFinishedTime, " +
			"  t.nextFireTime = :newFireTime, " +
			"  t.state = fi.evolver.basics.spring.timer.entity.ScheduledTask$State.WAITING " +
			"WHERE " +
			"  t.id = :id AND " +
			"  t.state = :currentState AND " +
			"  t.lastFiredTime = :lastFiredTime")
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	int scheduleTask(long id, State currentState, Long lastFiredTime, Long lastFinishedTime, Long newFireTime);


	@Transactional(propagation = Propagation.REQUIRES_NEW)
	default boolean scheduleTask(ScheduledTask task, Instant newFireTime) {
		if (task.getLastFiredTime() == null) {
			return scheduleUnscheduledTask(task.getId(), newFireTime.toEpochMilli()) > 0;
		}
		else {
			return scheduleTask(task.getId(), task.getState(), task.getLastFiredTime(), task.getLastFinishedTime(), newFireTime.toEpochMilli()) > 0;
		}
	}


	@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true)
	List findAllByStateAndNextFireTimeLessThanEqual(State state, long nextFireTime);


	@Modifying
	@Query("UPDATE ScheduledTask t SET " +
			"  t.lastFiredTime = :now, " +
			"  t.state = fi.evolver.basics.spring.timer.entity.ScheduledTask$State.RUNNING, " +
			"  t.lastInitiator = :server " +
			"WHERE " +
			"  t.id = :id AND " +
			"  t.state = fi.evolver.basics.spring.timer.entity.ScheduledTask$State.WAITING AND " +
			"  t.lastFiredTime = :lastFiredTime")
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	int prepareTask(long id, long lastFiredTime, long now, String server);


	@Transactional(propagation = Propagation.REQUIRES_NEW)
	default boolean prepareTask(ScheduledTask task, Instant now) {
		if (prepareTask(task.getId(), task.getLastFiredTime(), now.toEpochMilli(), ServerUtils.LOCAL_HOST_NAME) == 0)
			return false;

		task.setRunning(now);
		return true;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy