View Javadoc
1   /*
2    * The coLAB project
3    * Copyright (C) 2021-2023 AlbaSim, MEI, HEIG-VD, HES-SO
4    *
5    * Licensed under the MIT License
6    */
7   package ch.colabproject.colab.api.controller;
8   
9   import ch.colabproject.colab.api.controller.document.ExternalDataManager;
10  import ch.colabproject.colab.api.controller.monitoring.CronJobLogManager;
11  import ch.colabproject.colab.api.model.monitoring.CronJobLogName;
12  import ch.colabproject.colab.api.security.SessionManager;
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  
16  import javax.ejb.Schedule;
17  import javax.ejb.Singleton;
18  import javax.ejb.Startup;
19  import javax.inject.Inject;
20  
21  /**
22   * Do periodic tasks
23   *
24   * @author maxence
25   */
26  @Singleton
27  @Startup
28  public class CronTab {
29  
30      /** logger */
31      private static final Logger logger = LoggerFactory.getLogger(CronTab.class);
32  
33      /** session manager */
34      @Inject
35      private SessionManager sessionManager;
36  
37      /** To manager OpenGraph cache */
38      @Inject
39      private ExternalDataManager externalDataManager;
40  
41      /** To manage CronJobLogs */
42      @Inject
43      private CronJobLogManager cronJobLogManager;
44  
45      /**
46       * Each minute
47       */
48      @Schedule(hour = "*", minute = "*", persistent = false)
49      public void saveActivityDates() {
50          logger.trace("CRON: Persist activity dates to database");
51          sessionManager.writeActivityDatesToDatabase();
52          cronJobLogManager.updateCronJobLogLastRunTime(CronJobLogName.SAVE_ACTIVITIES_DATE);
53      }
54  
55      /**
56       * each midnight, clear expired sessions
57       */
58      @Schedule(hour = "0", minute = "0", persistent = false)
59      public void dropOldHttpSession() {
60          logger.info("CRON: drop expired http session");
61          sessionManager.clearExpiredHttpSessions();
62          cronJobLogManager.updateCronJobLogLastRunTime(CronJobLogName.DROP_OLD_HTTP_SESSIONS);
63      }
64  
65      /**
66       * each 00:30, clean outdated UrlMetadata
67       */
68      @Schedule(hour = "0", minute = "30", persistent = false)
69      public void dropOldUrlMetadata() {
70          logger.info("CRON: clean url metadata cache");
71          externalDataManager.clearOutdated();
72          cronJobLogManager.updateCronJobLogLastRunTime(CronJobLogName.DROP_OLD_URL_METADATA);
73      }
74  }