001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Mariana Cedica
016 */
017package org.nuxeo.ecm.platform.routing.core.listener;
018
019import org.nuxeo.ecm.core.api.repository.RepositoryManager;
020import org.nuxeo.ecm.core.event.Event;
021import org.nuxeo.ecm.core.event.EventListener;
022import org.nuxeo.ecm.core.event.EventProducer;
023import org.nuxeo.ecm.core.event.impl.EventContextImpl;
024import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
025import org.nuxeo.runtime.api.Framework;
026
027/**
028 * @since 5.8
029 */
030public class DocumentRoutingWorkflowInstancesCleanup implements EventListener {
031
032    public final static String CLEANUP_WORKFLOW_INSTANCES_PROPERTY = "nuxeo.routing.disable.cleanup.workflow.instances";
033
034    public final static String CLEANUP_WORKFLOW_INSTANCES_BATCH_SIZE_PROPERTY = "nuxeo.routing.cleanup.workflow.instances.batch.size";
035
036    public final static String CLEANUP_WORKFLOW_REPO_NAME_PROPERTY = "repositoryName";
037
038    public final static String CLEANUP_WORKFLOW_EVENT_NAME = "workflowInstancesCleanup";
039
040    @Override
041    public void handleEvent(Event event) {
042        if (!CLEANUP_WORKFLOW_EVENT_NAME.equals(event.getName())
043                || Framework.isBooleanPropertyTrue(CLEANUP_WORKFLOW_INSTANCES_PROPERTY)) {
044            return;
045        }
046
047        int batchSize = Integer.parseInt(Framework.getProperty(CLEANUP_WORKFLOW_INSTANCES_BATCH_SIZE_PROPERTY, "100"));
048        DocumentRoutingService routing = Framework.getLocalService(DocumentRoutingService.class);
049        RepositoryManager repositoryManager = Framework.getLocalService(RepositoryManager.class);
050
051        if (event.getContext().hasProperty(CLEANUP_WORKFLOW_REPO_NAME_PROPERTY)) {
052            doCleanAndReschedule(batchSize, routing,
053                    event.getContext().getProperty(CLEANUP_WORKFLOW_REPO_NAME_PROPERTY).toString());
054        } else {
055            for (String repositoryName : repositoryManager.getRepositoryNames()) {
056                doCleanAndReschedule(batchSize, routing, repositoryName);
057            }
058        }
059    }
060
061    /**
062     * @since 7.1
063     */
064    private void doCleanAndReschedule(int batchSize, DocumentRoutingService routing, String repositoryName) {
065        int cleanedUpWf = routing.doCleanupDoneAndCanceledRouteInstances(repositoryName, batchSize);
066        if (cleanedUpWf == batchSize) {
067            EventContextImpl eCtx = new EventContextImpl();
068            eCtx.setProperty(CLEANUP_WORKFLOW_REPO_NAME_PROPERTY, repositoryName);
069            Event event = eCtx.newEvent(CLEANUP_WORKFLOW_EVENT_NAME);
070            EventProducer eventProducer = Framework.getService(EventProducer.class);
071            eventProducer.fireEvent(event);
072        }
073    }
074
075}