• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

common-workflow-language / cwlviewer / #1809

31 Mar 2025 07:07AM CUT coverage: 70.306%. Remained the same
#1809

Pull #695

github

kinow
Add missing license header
Pull Request #695: Add missing license header

1700 of 2418 relevant lines covered (70.31%)

0.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/org/commonwl/view/Scheduler.java
1
package org.commonwl.view;
2

3
import java.io.File;
4
import java.io.IOException;
5
import java.time.Duration;
6
import java.time.Instant;
7
import java.util.Calendar;
8
import java.util.Date;
9
import java.util.List;
10
import java.util.stream.Stream;
11
import org.apache.commons.io.FileUtils;
12
import org.apache.commons.io.filefilter.AgeFileFilter;
13
import org.commonwl.view.workflow.QueuedWorkflowRepository;
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.beans.factory.annotation.Value;
18
import org.springframework.scheduling.annotation.Scheduled;
19
import org.springframework.stereotype.Component;
20

21
/** Scheduler class for recurrent processes. */
22
@Component
23
public class Scheduler {
24

25
  private final Logger logger = LoggerFactory.getLogger(this.getClass());
×
26
  private final QueuedWorkflowRepository queuedWorkflowRepository;
27

28
  @Value("${queuedWorkflowAgeLimitHours}")
29
  private Integer QUEUED_WORKFLOW_AGE_LIMIT_HOURS;
30

31
  @Value("${tmpDirAgeLimitDays}")
32
  private Integer TMP_DIR_AGE_LIMIT_DAYS;
33

34
  // We do not want to remove the bundles, as we use the disk as a sort of
35
  // cache. Whenever a workflow page is displayed in the browser the UI
36
  // fires a request to re-generate it. We skip that by keeping files on disk.
37
  @Value("${graphvizStorage}")
38
  private String graphvizStorage;
39

40
  @Value("${gitStorage}")
41
  private String gitStorage;
42

43
  @Autowired
44
  public Scheduler(QueuedWorkflowRepository queuedWorkflowRepository) {
×
45
    this.queuedWorkflowRepository = queuedWorkflowRepository;
×
46
  }
×
47

48
  /**
49
   * A Scheduled function to delete old queued workflow entries from the queue. Age is determined by
50
   * QUEUED_WORKFLOW_AGE_LIMIT_HOURS
51
   */
52
  @Scheduled(cron = "${cron.deleteOldQueuedWorkflows}")
53
  public void removeOldQueuedWorkflowEntries() {
54
    Calendar calendar = Calendar.getInstance();
×
55
    Date now = new Date();
×
56
    calendar.setTime(now);
×
57

58
    if (QUEUED_WORKFLOW_AGE_LIMIT_HOURS == null) {
×
59
      QUEUED_WORKFLOW_AGE_LIMIT_HOURS = 24;
×
60
    }
61

62
    // calculate time QUEUED_WORKFLOW_AGE_LIMIT_HOURS before now
63
    calendar.add(Calendar.HOUR, -QUEUED_WORKFLOW_AGE_LIMIT_HOURS);
×
64
    Date removeTime = calendar.getTime();
×
65

66
    logger.info("The time is " + now);
×
67
    logger.info(
×
68
        "Delete time interval is : OLDER THAN " + QUEUED_WORKFLOW_AGE_LIMIT_HOURS + " HOURS");
69
    logger.info("Deleting queued workflows older than or equal to " + removeTime);
×
70

71
    logger.info(
×
72
        queuedWorkflowRepository.deleteByTempRepresentation_RetrievedOnLessThanEqual(removeTime)
×
73
            + " Old queued workflows removed");
74
  }
×
75

76
  /**
77
   * Scheduled function to delete old temporary directories.
78
   *
79
   * <p>Will scan each temporary directory (graphviz, RO, git), searching for files exceeding a
80
   * specified threshold.
81
   *
82
   * <p>It scans the first level directories, i.e. it does not recursively scans directories. So it
83
   * will delete any RO or Git repository directories that exceed the threshold. Similarly, it will
84
   * delete any graph (svg, png, etc) that also exceed it.
85
   *
86
   * <p>Errors logged through Logger. Settings in Spring application properties file.
87
   *
88
   * @since 1.4.6
89
   */
90
  @Scheduled(cron = "${cron.clearTmpDir}")
91
  public void clearTmpDir() {
92
    // Temporary files used for graphviz, RO, and git may be stored in different
93
    // locations, so we will collect all of them here.
94
    List<String> temporaryDirectories = Stream.of(graphvizStorage, gitStorage).distinct().toList();
×
95
    temporaryDirectories.forEach(this::clearDirectory);
×
96
  }
×
97

98
  /**
99
   * For a given temporary directory, scans it (not recursively) for files and directories exceeding
100
   * the age limit threshold.
101
   *
102
   * @since 1.4.6
103
   * @see <a
104
   *     href="https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/filefilter/AgeFileFilter.html">https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/filefilter/AgeFileFilter.html</a>
105
   * @param temporaryDirectory temporary directory
106
   */
107
  private void clearDirectory(String temporaryDirectory) {
108
    final Instant cutoff = Instant.now().minus(Duration.ofDays(TMP_DIR_AGE_LIMIT_DAYS));
×
109

110
    File temporaryDirectoryFile = new File(temporaryDirectory);
×
111
    String[] files = temporaryDirectoryFile.list(new AgeFileFilter(Date.from(cutoff)));
×
112

113
    if (files != null && files.length > 0) {
×
114
      for (String fileName : files) {
×
115
        File fileToDelete = new File(temporaryDirectoryFile, fileName);
×
116
        try {
117
          FileUtils.forceDelete(fileToDelete);
×
118
        } catch (IOException e) {
×
119
          // Here we probably have a more serious case. Since the Git repository, RO directory, or
120
          // graphs are
121
          // not expected to be in use, and the application must have access, I/O errors are not
122
          // expected and
123
          // must be treated as errors.
124
          logger.error(
×
125
              String.format(
×
126
                  "Failed to delete old temporary file or directory [%s]: %s",
127
                  fileToDelete.getAbsolutePath(), e.getMessage()),
×
128
              e);
129
        }
×
130
      }
131
    }
132
  }
×
133
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc