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 */
018package org.apache.oozie.service;
019
020import org.apache.hadoop.conf.Configuration;
021import org.apache.oozie.command.PurgeXCommand;
022
023/**
024 * The PurgeService schedules purging of completed jobs and associated action older than a specified age for workflow, coordinator and bundle.
025 */
026public class PurgeService implements Service {
027
028    public static final String CONF_PREFIX = Service.CONF_PREFIX + "PurgeService.";
029    /**
030     * Age of completed jobs to be deleted, in days.
031     */
032    public static final String CONF_OLDER_THAN = CONF_PREFIX + "older.than";
033    public static final String COORD_CONF_OLDER_THAN = CONF_PREFIX + "coord.older.than";
034    public static final String BUNDLE_CONF_OLDER_THAN = CONF_PREFIX + "bundle.older.than";
035    public static final String PURGE_OLD_COORD_ACTION = CONF_PREFIX + "purge.old.coord.action";
036    /**
037     * Time interval, in seconds, at which the purge jobs service will be scheduled to run.
038     */
039    public static final String CONF_PURGE_INTERVAL = CONF_PREFIX + "purge.interval";
040    public static final String PURGE_LIMIT = CONF_PREFIX + "purge.limit";
041
042    /**
043     * PurgeRunnable is the runnable which is scheduled to run at the configured interval. PurgeCommand is queued to
044     * remove completed jobs and associated actions older than the configured age for workflow, coordinator and bundle.
045     */
046    static class PurgeRunnable implements Runnable {
047        private int wfOlderThan;
048        private int coordOlderThan;
049        private int bundleOlderThan;
050        private int limit;
051        private boolean purgeOldCoordAction = false;
052
053        public PurgeRunnable(int wfOlderThan, int coordOlderThan, int bundleOlderThan, int limit) {
054            this.wfOlderThan = wfOlderThan;
055            this.coordOlderThan = coordOlderThan;
056            this.bundleOlderThan = bundleOlderThan;
057            this.limit = limit;
058        }
059
060        public PurgeRunnable(int wfOlderThan, int coordOlderThan, int bundleOlderThan, int limit,
061                             boolean purgeOldCoordAction) {
062            this.wfOlderThan = wfOlderThan;
063            this.coordOlderThan = coordOlderThan;
064            this.bundleOlderThan = bundleOlderThan;
065            this.limit = limit;
066            this.purgeOldCoordAction = purgeOldCoordAction;
067        }
068
069        public void run() {
070            // Only queue the purge command if this is the leader
071            if (Services.get().get(JobsConcurrencyService.class).isLeader()) {
072                Services.get().get(CallableQueueService.class).queue(
073                        new PurgeXCommand(wfOlderThan, coordOlderThan, bundleOlderThan, limit, purgeOldCoordAction));
074            }
075        }
076
077    }
078
079    /**
080     * Initializes the {@link PurgeService}.
081     *
082     * @param services services instance.
083     */
084    @Override
085    public void init(Services services) {
086        Configuration conf = services.getConf();
087        Runnable purgeJobsRunnable = new PurgeRunnable(conf.getInt(
088                CONF_OLDER_THAN, 30), conf.getInt(COORD_CONF_OLDER_THAN, 7), conf.getInt(BUNDLE_CONF_OLDER_THAN, 7),
089                                      conf.getInt(PURGE_LIMIT, 100), conf.getBoolean(PURGE_OLD_COORD_ACTION, false));
090        services.get(SchedulerService.class).schedule(purgeJobsRunnable, 10, conf.getInt(CONF_PURGE_INTERVAL, 3600),
091                                                      SchedulerService.Unit.SEC);
092    }
093
094    /**
095     * Destroy the Purge Jobs Service.
096     */
097    @Override
098    public void destroy() {
099    }
100
101    /**
102     * Return the public interface for the purge jobs service.
103     *
104     * @return {@link PurgeService}.
105     */
106    @Override
107    public Class<? extends Service> getInterface() {
108        return PurgeService.class;
109    }
110}