001/* 002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Florent Guillaume 018 */ 019package org.nuxeo.ecm.core.versioning; 020 021import org.apache.commons.lang.StringUtils; 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.nuxeo.ecm.core.CoreService; 025import org.nuxeo.ecm.core.event.EventBundle; 026import org.nuxeo.ecm.core.event.PostCommitEventListener; 027import org.nuxeo.runtime.api.Framework; 028import org.nuxeo.runtime.services.config.ConfigurationService; 029 030/** 031 * Asynchronous listener that calls the orphan versions cleanup service. Designed to be called periodically by a 032 * scheduler. 033 * 034 * @since 9.1 035 */ 036public class OrphanVersionCleanupListener implements PostCommitEventListener { 037 038 private static final Log log = LogFactory.getLog(OrphanVersionCleanupListener.class); 039 040 public static final long DEFAULT_COMMIT_SIZE = 1000; 041 042 /** 043 * Configuration property for the maximum number of orphan versions to delete in one transaction. Default is 044 * {@value #DEFAULT_COMMIT_SIZE}. 045 */ 046 public static final String DEFAULT_COMMIT_SIZE_PROP = "org.nuxeo.orphanVersionsCleanup.commitSize"; 047 048 /** 049 * Gets the maximum number of orphan versions to delete in one transaction. 050 */ 051 protected long getCommitSize() { 052 ConfigurationService configurationService = Framework.getService(ConfigurationService.class); 053 String commitSize = configurationService.getProperty(DEFAULT_COMMIT_SIZE_PROP); 054 if (StringUtils.isBlank(commitSize)) { 055 return DEFAULT_COMMIT_SIZE; 056 } 057 try { 058 return Long.parseLong(commitSize); 059 } catch (NumberFormatException e) { 060 log.error("Invalid configuration property " + DEFAULT_COMMIT_SIZE_PROP, e); 061 return DEFAULT_COMMIT_SIZE; 062 } 063 } 064 065 @Override 066 public void handleEvent(EventBundle events) { 067 CoreService coreService = Framework.getService(CoreService.class); 068 if (coreService == null) { 069 // CoreService failed to start, no need to go further 070 return; 071 } 072 log.debug("Starting orphan versions cleanup"); 073 long n = coreService.cleanupOrphanVersions(getCommitSize()); 074 log.debug("Number of orphan versions deleted: " + n); 075 } 076 077}