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