head	1.1;
access;
symbols
	HELM_PEER_PORT_BRANCH:1.1.0.2;
locks; strict;
comment	@# @;


1.1
date	2001.02.05.14.55.20;	author leonardr;	state dead;
branches
	1.1.2.1;
next	;

1.1.2.1
date	2001.02.05.14.55.20;	author leonardr;	state Exp;
branches;
next	1.1.2.2;

1.1.2.2
date	2001.02.05.20.20.12;	author leonardr;	state Exp;
branches;
next	1.1.2.3;

1.1.2.3
date	2001.02.09.16.03.43;	author leonardr;	state Exp;
branches;
next	1.1.2.4;

1.1.2.4
date	2001.02.15.16.23.42;	author leonardr;	state dead;
branches;
next	;


desc
@@


1.1
log
@file TigrisTaskRunner.java was initially added on branch HELM_PEER_PORT_BRANCH.
@
text
@@


1.1.2.1
log
@As long as we're rebuilding the directory structure, we might as well sort out the tool package.
@
text
@a0 194
/* ================================================================
 * Copyright (c) 2000 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet (http://www.collab.net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of CollabNet.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */


package org.tigris.helm.tool.task;

import java.util.*;

import org.apache.turbine.util.Log;

import org.tigris.helm.om.Task;

//import org.tigris.helm.tool.TigrisUpdate;
//import org.tigris.helm.tool.HelmEmailer;

/**
 * TigrisTaskRunner runs as a thread 
 *
 * <p>Access to the program is synchronized, so only one access at a
 * time. Hopefully this will prevent data corruption. 
 */
public class TigrisTaskRunner extends Thread
{
    private volatile boolean stopNow;

    public static List instances = new ArrayList();
    
    public static int stopThreads()
    {
        int threads = 0;
        for (Iterator i = instances.iterator(); i.hasNext();)
        {
            TigrisTaskRunner tr = (TigrisTaskRunner)i.next();
            tr.sendStopSignal();
            threads++;
        }
        return threads;
    }

    public static int numberOfInstances()
    {
        return instances.size();
    }
    
    public TigrisTaskRunner()
    {
        instances.add(this);
        Log.info("Initializing TigrisTaskRunner. Current number of threads: " +
                 numberOfInstances());
    }

    public void sendStopSignal()
    {
        stopNow = true;
    }
    
    public void run()
    {        
        List tasksToRun = null;
        stopNow = false;
        while (!stopNow)
        {
            try 
            {            
                tasksToRun = Task.getTasks();
                if (tasksToRun.isEmpty())
                {
                    try 
                    {
                        sleep(5000);
                    } catch (InterruptedException e) {
                        //Do nothing.
                    }
                } else { 
                    logTasks(tasksToRun);
                    //Run each task.                
                    try {
                        for (Iterator i = tasksToRun.iterator(); i.hasNext();)
                        {
                            if (stopNow) break;
                            Task t = (Task)i.next();
                            Task.runTask(t);
                            if (stopNow)
                            {
                                Log.debug("Interrupting task running because thread was stopped.");
                                break;
                            }
                        }                        
                  } catch (TaskException e) {
                        //Send email to the host admin(s).
                        String subject = "Update script failed.";
                        String body = 
                            "Execution of the following script failed: \n\n" 
                            + e.getMessage();
                    
                        //TODO: HelmEmailer emailer = new HelmEmailer();
                        //emailer.sendError(subject, body);
                    }
                }
            } catch (Exception e) {
                Log.info("Exception while running tasks: " , e);
            }
        }
        instances.remove(this);
        Log.info("TigrisTaskRunner shutting down. New number of threads: " 
                 + numberOfInstances());
    }
    
    public void logTasks(List tasks)
    {
        Log.debug("--------------------------------------");
        Log.debug("TigrisTaskRunner has tasks to perform!");
        Log.debug("Task summary for this round:");
        int oldTypeNumber = 1;
        String oldType = "";
        for (Iterator i = tasks.iterator(); i.hasNext();)
        {                        
            Task t = (Task)i.next();
            String newType = t.getComponent() + "::" + t.getCommand();
            if (newType.equals(oldType))
            {
                oldTypeNumber++;
            } 
            else 
            {
                if (!oldType.equals(""))
                {
                    if (oldTypeNumber == 1)
                    {
                        Log.debug(oldType);
                    } 
                    else 
                    {
                        Log.debug(oldType + " (" + oldTypeNumber + "x)");
                    }
                }
                oldType = newType;
                oldTypeNumber = 1;
            }
            //Display the last set of tasks.
            if (oldTypeNumber == 1)
            {
                Log.debug(oldType);
            } else {
                Log.debug(oldType + " (" + oldTypeNumber + "x)");
            }
            
            Log.debug("--------------------------------------");
        }
    }
}
@


1.1.2.2
log
@TigrisUpdate reference begone!
@
text
@d56 1
@


1.1.2.3
log
@Added code that uses TemplateInterpolator and HelmEmailer.
@
text
@d56 1
a56 1
import org.tigris.helm.tool.HelmEmailer;
d138 2
a139 2
                        HelmEmailer emailer = new HelmEmailer();
                        emailer.sendError(subject, body);
@


1.1.2.4
log
@TigrisTaskRunner is now TaskRunner.
@
text
@@


