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