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