001/* 002 * (C) Copyright 2013 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 * Mariana Cedica 018 */ 019package org.nuxeo.ecm.platform.routing.core.listener; 020 021import java.util.HashSet; 022import java.util.Set; 023 024import org.nuxeo.ecm.core.api.repository.RepositoryManager; 025import org.nuxeo.ecm.core.event.Event; 026import org.nuxeo.ecm.core.event.EventBundle; 027import org.nuxeo.ecm.core.event.EventProducer; 028import org.nuxeo.ecm.core.event.PostCommitEventListener; 029import org.nuxeo.ecm.core.event.impl.EventContextImpl; 030import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService; 031import org.nuxeo.runtime.api.Framework; 032 033/** 034 * @since 5.8 035 */ 036public class DocumentRoutingWorkflowInstancesCleanup implements PostCommitEventListener { 037 038 public final static String CLEANUP_WORKFLOW_INSTANCES_PROPERTY = "nuxeo.routing.disable.cleanup.workflow.instances"; 039 040 public final static String CLEANUP_WORKFLOW_INSTANCES_BATCH_SIZE_PROPERTY = "nuxeo.routing.cleanup.workflow.instances.batch.size"; 041 042 public final static String CLEANUP_WORKFLOW_REPO_NAME_PROPERTY = "repositoryName"; 043 044 public final static String CLEANUP_WORKFLOW_EVENT_NAME = "workflowInstancesCleanup"; 045 046 @Override 047 public void handleEvent(EventBundle events) { 048 int batchSize = Integer.parseInt( 049 Framework.getProperty(CLEANUP_WORKFLOW_INSTANCES_BATCH_SIZE_PROPERTY, "100")); 050 DocumentRoutingService routing = Framework.getService(DocumentRoutingService.class); 051 RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class); 052 053 Set<String> repositoryNames = new HashSet<>(); 054 for (Event event : events) { 055 if (event.getContext().hasProperty(CLEANUP_WORKFLOW_REPO_NAME_PROPERTY)) { 056 String repositoryName = (String) event.getContext().getProperty(CLEANUP_WORKFLOW_REPO_NAME_PROPERTY); 057 repositoryNames.add(repositoryName); 058 } else { 059 repositoryNames.addAll(repositoryManager.getRepositoryNames()); 060 } 061 } 062 for (String repositoryName : repositoryNames) { 063 doCleanAndReschedule(batchSize, routing, repositoryName); 064 } 065 } 066 067 /** 068 * @since 7.1 069 */ 070 private void doCleanAndReschedule(int batchSize, DocumentRoutingService routing, String repositoryName) { 071 int cleanedUpWf = routing.doCleanupDoneAndCanceledRouteInstances(repositoryName, batchSize); 072 if (cleanedUpWf == batchSize) { 073 EventContextImpl eCtx = new EventContextImpl(); 074 eCtx.setProperty(CLEANUP_WORKFLOW_REPO_NAME_PROPERTY, repositoryName); 075 Event event = eCtx.newEvent(CLEANUP_WORKFLOW_EVENT_NAME); 076 EventProducer eventProducer = Framework.getService(EventProducer.class); 077 eventProducer.fireEvent(event); 078 } 079 } 080 081}