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