001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.service;
020
021import org.apache.hadoop.conf.Configuration;
022import org.apache.oozie.command.PurgeXCommand;
023
024/**
025 * The PurgeService schedules purging of completed jobs and associated action older than a specified age for workflow, coordinator and bundle.
026 */
027public class PurgeService implements Service {
028
029    public static final String CONF_PREFIX = Service.CONF_PREFIX + "PurgeService.";
030    /**
031     * Age of completed jobs to be deleted, in days.
032     */
033    public static final String CONF_OLDER_THAN = CONF_PREFIX + "older.than";
034    public static final String COORD_CONF_OLDER_THAN = CONF_PREFIX + "coord.older.than";
035    public static final String BUNDLE_CONF_OLDER_THAN = CONF_PREFIX + "bundle.older.than";
036    public static final String PURGE_OLD_COORD_ACTION = CONF_PREFIX + "purge.old.coord.action";
037    /**
038     * Time interval, in seconds, at which the purge jobs service will be scheduled to run.
039     */
040    public static final String CONF_PURGE_INTERVAL = CONF_PREFIX + "purge.interval";
041    public static final String PURGE_LIMIT = CONF_PREFIX + "purge.limit";
042    public static final String PURGE_COMMAND_ENABLED = CONF_PREFIX + "enable.command.line";
043
044    /**
045     * PurgeRunnable is the runnable which is scheduled to run at the configured interval. PurgeCommand is queued to
046     * remove completed jobs and associated actions older than the configured age for workflow, coordinator and bundle.
047     */
048    static class PurgeRunnable implements Runnable {
049        private int wfOlderThan;
050        private int coordOlderThan;
051        private int bundleOlderThan;
052        private int limit;
053        private boolean purgeOldCoordAction = false;
054
055        public PurgeRunnable(int wfOlderThan, int coordOlderThan, int bundleOlderThan, int limit) {
056            this.wfOlderThan = wfOlderThan;
057            this.coordOlderThan = coordOlderThan;
058            this.bundleOlderThan = bundleOlderThan;
059            this.limit = limit;
060        }
061
062        public PurgeRunnable(int wfOlderThan, int coordOlderThan, int bundleOlderThan, int limit,
063                             boolean purgeOldCoordAction) {
064            this.wfOlderThan = wfOlderThan;
065            this.coordOlderThan = coordOlderThan;
066            this.bundleOlderThan = bundleOlderThan;
067            this.limit = limit;
068            this.purgeOldCoordAction = purgeOldCoordAction;
069        }
070
071        public void run() {
072            // Only queue the purge command if this is the leader
073            if (Services.get().get(JobsConcurrencyService.class).isLeader()) {
074                Services.get().get(CallableQueueService.class).queue(
075                        new PurgeXCommand(wfOlderThan, coordOlderThan, bundleOlderThan, limit, purgeOldCoordAction));
076            }
077        }
078
079    }
080
081    /**
082     * Initializes the {@link PurgeService}.
083     *
084     * @param services services instance.
085     */
086    @Override
087    public void init(Services services) {
088        Configuration conf = services.getConf();
089        Runnable purgeJobsRunnable = new PurgeRunnable(ConfigurationService.getInt(conf, CONF_OLDER_THAN),
090                ConfigurationService.getInt(conf, COORD_CONF_OLDER_THAN),
091                ConfigurationService.getInt(conf, BUNDLE_CONF_OLDER_THAN),
092                ConfigurationService.getInt(conf, PURGE_LIMIT),
093                ConfigurationService.getBoolean(conf, PURGE_OLD_COORD_ACTION));
094        services.get(SchedulerService.class).schedule(purgeJobsRunnable, 10,
095                ConfigurationService.getInt(conf, CONF_PURGE_INTERVAL), SchedulerService.Unit.SEC);
096    }
097
098    /**
099     * Destroy the Purge Jobs Service.
100     */
101    @Override
102    public void destroy() {
103    }
104
105    /**
106     * Return the public interface for the purge jobs service.
107     *
108     * @return {@link PurgeService}.
109     */
110    @Override
111    public Class<? extends Service> getInterface() {
112        return PurgeService.class;
113    }
114}