001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.service;
016    
017    import org.apache.hadoop.conf.Configuration;
018    import org.apache.oozie.command.coord.CoordPurgeCommand;
019    import org.apache.oozie.command.wf.PurgeCommand;
020    import org.apache.oozie.service.CallableQueueService;
021    import org.apache.oozie.service.SchedulerService;
022    import org.apache.oozie.service.Service;
023    import org.apache.oozie.service.Services;
024    
025    /**
026     * The PurgeService schedules purging of completed jobs and associated action older than a specified age.
027     */
028    public 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        /**
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        private static final String COORD_PURGE_LIMIT = CONF_PREFIX + "coord.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.
045         */
046        static class PurgeRunnable implements Runnable {
047            private int olderThan;
048            private int coordOlderThan;
049            private int limit;
050    
051            public PurgeRunnable(int olderThan, int coordOlderThan, int limit) {
052                this.olderThan = olderThan;
053                this.coordOlderThan = coordOlderThan;
054                this.limit = limit;
055            }
056    
057            public void run() {
058                Services.get().get(CallableQueueService.class).queue(new PurgeCommand(olderThan, limit));
059                Services.get().get(CallableQueueService.class).queue(new CoordPurgeCommand(coordOlderThan, limit));
060            }
061    
062        }
063    
064        /**
065         * Initializes the {@link PurgeService}.
066         *
067         * @param services services instance.
068         */
069        @Override
070        public void init(Services services) {
071            Configuration conf = services.getConf();
072            Runnable purgeJobsRunnable = new PurgeRunnable(conf.getInt(
073                    CONF_OLDER_THAN, 30), conf.getInt(COORD_CONF_OLDER_THAN, 7),
074                                          conf.getInt(COORD_PURGE_LIMIT, 100));
075            services.get(SchedulerService.class).schedule(purgeJobsRunnable, 10, conf.getInt(CONF_PURGE_INTERVAL, 3600),
076                                                          SchedulerService.Unit.SEC);
077        }
078    
079        /**
080         * Destroy the Purge Jobs Service.
081         */
082        @Override
083        public void destroy() {
084        }
085    
086        /**
087         * Return the public interface for the purge jobs service.
088         *
089         * @return {@link PurgeService}.
090         */
091        @Override
092        public Class<? extends Service> getInterface() {
093            return PurgeService.class;
094        }
095    }