001/*
002 * (C) Copyright 2012 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 *     Florent Guillaume
016 */
017package org.nuxeo.ecm.admin.work;
018
019import java.io.Serializable;
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.jboss.seam.ScopeType;
027import org.jboss.seam.annotations.Name;
028import org.jboss.seam.annotations.Scope;
029import org.jboss.seam.annotations.web.RequestParameter;
030import org.nuxeo.ecm.core.work.SleepWork;
031import org.nuxeo.ecm.core.work.api.Work;
032import org.nuxeo.ecm.core.work.api.Work.State;
033import org.nuxeo.ecm.core.work.api.WorkManager;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Seam bean that wraps the {@link WorkManager} service to provide a JSF admin UI.
038 */
039@Name("workManagerAdmin")
040@Scope(ScopeType.PAGE)
041public class WorkManagerAdminBean implements Serializable {
042
043    private static final long serialVersionUID = 1L;
044
045    @RequestParameter("queueId")
046    protected String queueId;
047
048    protected WorkManager getWorkManager() {
049        return Framework.getLocalService(WorkManager.class);
050    }
051
052    public List<Map<String, Object>> getWorkQueuesInfo() {
053        List<Map<String, Object>> info = new ArrayList<Map<String, Object>>();
054        WorkManager workManager = getWorkManager();
055        List<String> workQueueIds = workManager.getWorkQueueIds();
056        Collections.sort(workQueueIds);
057        for (String queueId : workQueueIds) {
058            List<Work> running = workManager.listWork(queueId, State.RUNNING);
059            int scheduled = workManager.getQueueSize(queueId, State.SCHEDULED);
060            int completed = workManager.getQueueSize(queueId, State.COMPLETED);
061            Map<String, Object> map = new HashMap<String, Object>();
062            info.add(map);
063            map.put("id", queueId);
064            map.put("scheduled", scheduled);
065            map.put("running", running.size());
066            map.put("completed", completed);
067            map.put("runningWorks", running);
068        }
069        return info;
070    }
071
072    public long getCurrentTimeMillis() {
073        return System.currentTimeMillis();
074    }
075
076    public String clearQueueCompletedWork() {
077        getWorkManager().clearCompletedWork(queueId);
078        return null;
079    }
080
081    public String clearAllCompletedWork() {
082        getWorkManager().clearCompletedWork(0);
083        return null;
084    }
085
086    public String startTestWork() {
087        getWorkManager().schedule(new SleepWork(10000));
088        return null;
089    }
090
091}