Class DigestScheduleStorage

java.lang.Object
io.meeds.commons.digest.service.DigestScheduleStorage

@Component public class DigestScheduleStorage extends Object
The work list of the digest sender job: who is a candidate, the claim of an occurrence, the waiting items of a user and their deletion.
  • Constructor Details

  • Method Details

    • findCandidates

      public List<DigestUserEntity> findCandidates(io.meeds.commons.digest.model.DigestFrequency frequency, Instant cutoff, long afterId, org.springframework.data.domain.Limit limit)
      One batch of the candidates of a frequency: the frequency is on and the watermark is older than the cutoff, ids after the last one seen, ascending. The exact "due now in his timezone" check is done in Java by the caller, the query only narrows the set.
      Parameters:
      frequency - daily or weekly
      cutoff - the watermark must be before it
      afterId - only rows with a greater id, 0 for the first batch
      limit - how many rows at most
      Returns:
      the candidates, by ascending id
    • find

      public DigestUserEntity find(long id)
    • claim

      @Transactional public boolean claim(long userId, io.meeds.commons.digest.model.DigestFrequency frequency, Instant expected, Instant now)
      Claims an occurrence: moves the watermark of the frequency to now, only if it still holds the value the caller read. Zero rows means another worker, or another server, took it first. Joins the transaction of the caller when there is one: the claim then commits, or rolls back, with what the caller does with the occurrence.
      Parameters:
      userId - the work list row id
      frequency - daily or weekly
      expected - the watermark the caller read
      now - the new watermark
      Returns:
      true when this caller got the occurrence
    • findItems

      public List<DigestItemEntity> findItems(String username, Instant after, Instant until)
      The waiting items of a user in a window, most recent first
      Parameters:
      username - the recipient
      after - excluded lower bound, the previous watermark
      until - included upper bound, the new watermark
      Returns:
      the items
    • deleteCoveredItems

      @Transactional public int deleteCoveredItems(String username, Instant coveredUntil)
      Deletes the items every enabled frequency of the user has covered
      Parameters:
      username - the recipient
      coveredUntil - the oldest watermark of his enabled frequencies
      Returns:
      how many items were deleted
    • forget

      @Transactional public void forget(long id, String username)
      Forgets a user for good: his waiting items and his work list row
      Parameters:
      id - the work list row id
      username - the user
    • cleanup

      @Transactional public int cleanup(Instant retentionLimit)
      Safety cleanup, the first step of every run: the items of users who have no digest enabled any more, and the items older than the retention
      Parameters:
      retentionLimit - items older than this are deleted whatever happened
      Returns:
      how many items were deleted