View Javadoc
1   /*
2    * Copyright (C) 2024 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
5    */
6   package org.tailormap.api.scheduling;
7   
8   import org.quartz.InterruptableJob;
9   import org.tailormap.api.admin.model.TaskProgressEvent;
10  
11  public interface Task {
12  
13    String TYPE_KEY = "type";
14    String DESCRIPTION_KEY = "description";
15    String UUID_KEY = "uuid";
16    String CRON_EXPRESSION_KEY = "cronExpression";
17    String PRIORITY_KEY = "priority";
18    String STATE_KEY = "state";
19    String LAST_RESULT_KEY = "lastResult";
20    String INTERRUPTABLE_KEY = "interruptable";
21    String EXECUTION_COUNT_KEY = "executionCount";
22    String EXECUTION_FINISHED_KEY = "lastExecutionFinished";
23  
24    /**
25     * Get the type of the task. Implement this method to return the key for the type of task. This must be a read-only
26     * property.
27     *
28     * @return the type of task
29     */
30    TaskType getType();
31  
32    /**
33     * Get the description of the task.
34     *
35     * @return the description
36     */
37    String getDescription();
38  
39    /**
40     * Set the description of the task. The default is a no-op, which means the description is read-only.
41     *
42     * @param description the description
43     */
44    default void setDescription(String description) {
45      // no-op
46    }
47  
48    /**
49     * Handle the task progress event. Override this method to handle the progress of the task, e.g. by emitting
50     * {@code ServerSentEvent}s. The default is a no-op, which means no progress events will be emitted.
51     *
52     * @param event the task progress event
53     */
54    default void taskProgress(TaskProgressEvent event) {
55      // no-op
56    }
57  
58    /**
59     * Determine if this task can be stopped on demand (the task must implement {@code InterruptableJob} for this to
60     * work).
61     *
62     * @return {@code true} if the task can be stopped on demand, false otherwise
63     * @see InterruptableJob
64     */
65    default boolean isInterruptable() {
66      return InterruptableJob.class.isAssignableFrom(this.getClass());
67    }
68  }