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        if (Framework.isBooleanPropertyTrue(CLEANUP_WORKFLOW_INSTANCES_PROPERTY)) {
049            return;
050        }
051        int batchSize = Integer.parseInt(
052                Framework.getProperty(CLEANUP_WORKFLOW_INSTANCES_BATCH_SIZE_PROPERTY, "100"));
053        DocumentRoutingService routing = Framework.getService(DocumentRoutingService.class);
054        RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class);
055
056        Set<String> repositoryNames = new HashSet<>();
057        for (Event event : events) {
058            if (event.getContext().hasProperty(CLEANUP_WORKFLOW_REPO_NAME_PROPERTY)) {
059                String repositoryName = (String) event.getContext().getProperty(CLEANUP_WORKFLOW_REPO_NAME_PROPERTY);
060                repositoryNames.add(repositoryName);
061            } else {
062                repositoryNames.addAll(repositoryManager.getRepositoryNames());
063            }
064        }
065        for (String repositoryName : repositoryNames) {
066            doCleanAndReschedule(batchSize, routing, repositoryName);
067        }
068    }
069
070    /**
071     * @since 7.1
072     */
073    private void doCleanAndReschedule(int batchSize, DocumentRoutingService routing, String repositoryName) {
074        int cleanedUpWf = routing.doCleanupDoneAndCanceledRouteInstances(repositoryName, batchSize);
075        if (cleanedUpWf == batchSize) {
076            EventContextImpl eCtx = new EventContextImpl();
077            eCtx.setProperty(CLEANUP_WORKFLOW_REPO_NAME_PROPERTY, repositoryName);
078            Event event = eCtx.newEvent(CLEANUP_WORKFLOW_EVENT_NAME);
079            EventProducer eventProducer = Framework.getService(EventProducer.class);
080            eventProducer.fireEvent(event);
081        }
082    }
083
084}