View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.fileupload.service;
23  
24  import org.opencastproject.fileupload.api.FileUploadService;
25  import org.opencastproject.fileupload.api.job.FileUploadJob;
26  
27  import org.quartz.Job;
28  import org.quartz.JobDetail;
29  import org.quartz.JobExecutionContext;
30  import org.quartz.JobExecutionException;
31  import org.quartz.Trigger;
32  import org.quartz.TriggerUtils;
33  import org.quartz.impl.StdSchedulerFactory;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  import java.io.IOException;
38  import java.util.Date;
39  
40  /** Clear for outdated Job of type {@link FileUploadJob}. */
41  public class FileUploadServiceCleaner {
42  
43    /** Log facility */
44    private static final Logger logger = LoggerFactory.getLogger(FileUploadServiceCleaner.class);
45  
46    private static final String JOB_NAME = "mh-file-upload-cleaner-job";
47    private static final String JOB_GROUP = "mh-file-upload-cleaner-job-group";
48    private static final String TRIGGER_NAME = "mh-file-upload-cleaner-trigger";
49    private static final String TRIGGER_GROUP = "mh-file-upload-cleaner-trigger-group";
50    private static final String JOB_PARAM_PARENT = "parent";
51  
52    private final org.quartz.Scheduler quartz;
53  
54    private FileUploadService fileUploadService;
55  
56    protected FileUploadServiceCleaner(FileUploadService fileUploadService) {
57      this.fileUploadService = fileUploadService;
58      try {
59        quartz = new StdSchedulerFactory().getScheduler();
60        quartz.start();
61        // create and set the job. To actually run it call schedule(..)
62        final JobDetail job = new JobDetail(JOB_NAME, JOB_GROUP, Runner.class);
63        job.setDurability(false);
64        job.setVolatility(true);
65        job.getJobDataMap().put(JOB_PARAM_PARENT, this);
66        quartz.addJob(job, true);
67      } catch (org.quartz.SchedulerException e) {
68        throw new RuntimeException(e);
69      }
70    }
71  
72    public FileUploadService getFileUploadService() {
73      return fileUploadService;
74    }
75  
76    /**
77     * Set the schedule and start or restart the scheduler.
78     */
79    public void schedule() {
80      logger.debug("File upload job cleaner is run every hour.");
81      try {
82        final Trigger trigger = TriggerUtils.makeHourlyTrigger();
83        trigger.setStartTime(new Date());
84        trigger.setName(TRIGGER_NAME);
85        trigger.setGroup(TRIGGER_GROUP);
86        trigger.setJobName(JOB_NAME);
87        trigger.setJobGroup(JOB_GROUP);
88        if (quartz.getTriggersOfJob(JOB_NAME, JOB_GROUP).length == 0) {
89          quartz.scheduleJob(trigger);
90        } else {
91          quartz.rescheduleJob(TRIGGER_NAME, TRIGGER_GROUP, trigger);
92        }
93      } catch (Exception e) {
94        logger.error("Error scheduling Quartz job", e);
95      }
96    }
97  
98    /** Shutdown the scheduler. */
99    public void shutdown() {
100     try {
101       quartz.shutdown();
102     } catch (org.quartz.SchedulerException ignore) {
103     }
104   }
105 
106   // just to make sure Quartz is being shut down...
107   @Override
108   protected void finalize() throws Throwable {
109     super.finalize();
110     shutdown();
111   }
112 
113   // --
114 
115   /** Quartz work horse. */
116   public static class Runner implements Job {
117 
118     @Override
119     public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
120       logger.info("Start file upload service cleaner");
121       try {
122         execute((FileUploadServiceCleaner) jobExecutionContext.getJobDetail().getJobDataMap().get(JOB_PARAM_PARENT));
123       } catch (Exception e) {
124         throw new JobExecutionException("An error occurred while cleaning file upload jobs", e);
125       }
126       logger.info("Finished file upload service cleaner");
127     }
128 
129     private void execute(FileUploadServiceCleaner fileUploadServiceCleaner) {
130       try {
131         fileUploadServiceCleaner.getFileUploadService().cleanOutdatedJobs();
132       } catch (IOException e) {
133         logger.warn("Unable to clean outdated jobs: {}", e.getMessage());
134       }
135     }
136 
137   }
138 
139 }