summaryrefslogtreecommitdiff
path: root/pse-server/src/main/java/org/psesquared/server/util/Scheduler.java
blob: fbe81943b4f5da6f3831dc8a7a3e0e0aec8c519f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package org.psesquared.server.util;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import lombok.RequiredArgsConstructor;
import org.psesquared.server.authentication.api.service.AuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * A scheduler responsible for running scheduled actions.
 */
@Component
@RequiredArgsConstructor
public class Scheduler {

  /**
   * The seconds of one day.
   */
  private static final long ONE_DAY = 24 * 60 * (long) 60;

  /**
   * The service class of the authentication API.
   */
  @Autowired
  private final AuthenticationService authenticationService;

  /**
   * A scheduled operation that removes all non-verified users from the server,
   * that haven't been verified since at least 24 hours.
   * <br>
   * Standard: Runs every day at 3 AM.
   */
  @Scheduled(cron = "0 0 3 * * *")
  public void clean() {
    authenticationService.deleteInvalidUsersOlderThan(
        LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) - ONE_DAY);
  }

}