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    package org.apache.oozie.service;
019    
020    import org.apache.hadoop.conf.Configuration;
021    import 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     */
026    public 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        /**
036         * Time interval, in seconds, at which the purge jobs service will be scheduled to run.
037         */
038        public static final String CONF_PURGE_INTERVAL = CONF_PREFIX + "purge.interval";
039        public static final String PURGE_LIMIT = CONF_PREFIX + "purge.limit";
040    
041        /**
042         * PurgeRunnable is the runnable which is scheduled to run at the configured interval. PurgeCommand is queued to
043         * remove completed jobs and associated actions older than the configured age for workflow, coordinator and bundle.
044         */
045        static class PurgeRunnable implements Runnable {
046            private int wfOlderThan;
047            private int coordOlderThan;
048            private int bundleOlderThan;
049            private int limit;
050    
051            public PurgeRunnable(int wfOlderThan, int coordOlderThan, int bundleOlderThan, int limit) {
052                this.wfOlderThan = wfOlderThan;
053                this.coordOlderThan = coordOlderThan;
054                this.bundleOlderThan = bundleOlderThan;
055                this.limit = limit;
056            }
057    
058            public void run() {
059                    Services.get().get(CallableQueueService.class).queue(
060                            new PurgeXCommand(wfOlderThan, coordOlderThan, bundleOlderThan, limit));
061            }
062    
063        }
064    
065        /**
066         * Initializes the {@link PurgeService}.
067         *
068         * @param services services instance.
069         */
070        @Override
071        public void init(Services services) {
072            Configuration conf = services.getConf();
073            Runnable purgeJobsRunnable = new PurgeRunnable(conf.getInt(
074                    CONF_OLDER_THAN, 30), conf.getInt(COORD_CONF_OLDER_THAN, 7), conf.getInt(BUNDLE_CONF_OLDER_THAN, 7),
075                                          conf.getInt(PURGE_LIMIT, 100));
076            services.get(SchedulerService.class).schedule(purgeJobsRunnable, 10, conf.getInt(CONF_PURGE_INTERVAL, 3600),
077                                                          SchedulerService.Unit.SEC);
078        }
079    
080        /**
081         * Destroy the Purge Jobs Service.
082         */
083        @Override
084        public void destroy() {
085        }
086    
087        /**
088         * Return the public interface for the purge jobs service.
089         *
090         * @return {@link PurgeService}.
091         */
092        @Override
093        public Class<? extends Service> getInterface() {
094            return PurgeService.class;
095        }
096    }