head	1.13;
access;
symbols
	TIGRIS_0_9_2_4:1.10.2.1
	TIGRIS_0_9_2_3:1.10.2.1
	HELM_PEER_PORT_BRANCH:1.12.0.2
	TURBINE_PEER:1.12
	TIGRIS_0_9_2_1:1.12
	TIGRIS_0_9_2:1.10.0.2
	TIGRIS_0_9_0:1.5.0.4
	TIGRIS_0_8_4:1.5.0.2;
locks; strict;
comment	@# @;


1.13
date	2001.02.21.03.23.57;	author edk;	state dead;
branches;
next	1.12;

1.12
date	2001.01.26.18.29.09;	author edk;	state Exp;
branches
	1.12.2.1;
next	1.11;

1.11
date	2001.01.26.05.24.08;	author edk;	state dead;
branches;
next	1.10;

1.10
date	2001.01.17.03.21.31;	author dlr;	state Exp;
branches
	1.10.2.1;
next	1.9;

1.9
date	2001.01.15.22.04.44;	author leonardr;	state Exp;
branches;
next	1.8;

1.8
date	2001.01.09.19.37.37;	author leonardr;	state Exp;
branches;
next	1.7;

1.7
date	2001.01.06.01.23.00;	author leonardr;	state Exp;
branches;
next	1.6;

1.6
date	2001.01.05.01.10.56;	author dlr;	state Exp;
branches;
next	1.5;

1.5
date	2000.12.03.05.58.35;	author edk;	state Exp;
branches;
next	1.4;

1.4
date	2000.12.03.04.25.34;	author edk;	state Exp;
branches;
next	1.3;

1.3
date	2000.12.02.22.29.08;	author leonardr;	state Exp;
branches;
next	1.2;

1.2
date	2000.12.01.19.46.19;	author leonardr;	state Exp;
branches;
next	1.1;

1.1
date	2000.12.01.19.05.21;	author leonardr;	state Exp;
branches;
next	;

1.10.2.1
date	2001.01.30.00.36.21;	author leonardr;	state Exp;
branches;
next	;

1.12.2.1
date	2001.01.28.01.02.19;	author dlr;	state dead;
branches;
next	1.12.2.2;

1.12.2.2
date	2001.02.03.00.07.33;	author leonardr;	state Exp;
branches;
next	1.12.2.3;

1.12.2.3
date	2001.02.05.14.55.20;	author leonardr;	state dead;
branches;
next	;


desc
@@


1.13
log
@delete stuff from the head which is obsolete in the branch (more changes
are still needed for a clean compile)
@
text
@/* ================================================================
 * 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;

import java.sql.Connection;
import java.util.*;

import org.apache.turbine.services.db.TurbineDB;
import org.apache.turbine.util.db.pool.DBConnection;

import org.joist.database.DataManager;

import org.webmacro.util.Log;
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
{
    static public Log log = new Log("TaskRun", "Tigris Task Runner");
    static private boolean debug = true;

    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("Creating new TigrisTaskRunner. Current number of threads: " +
                 numberOfInstances());
    }

    public void sendStopSignal()
    {
        stopNow = true;
    }
    
    public void run()
    {        
        List tasksToRun = null;
        DBConnection dbConn = null;
        Connection connection = null;
        DataManager dMgr = new DataManager();
        stopNow = false;
        while (!stopNow)
        {
            try 
            {
                dbConn = TurbineDB.getConnection();
                connection = dbConn.getConnection();
                tasksToRun = TigrisTask.getTasks(connection, dMgr);
            
                if (tasksToRun.isEmpty())
                {
                    try 
                    {
                        sleep(5000);
                    } catch (InterruptedException e) {
                        //Do nothing.
                    }
                } else { 
                    //Run each task.
                    log.info("--------------------------------------");
                    log.info("TigrisTaskRunner has tasks to perform!");
                    log.info("Task summary for this round:");
                    int oldTypeNumber = 1;
                    String oldType = "";
                    for (Iterator i = tasksToRun.iterator(); i.hasNext();)
                    {                        
                        TigrisTask t = (TigrisTask)i.next();
                        try {
                            String newType = t.getComponent() + "::" + 
                                t.getCommand();
                            if (newType.equals(oldType))
                            {
                                oldTypeNumber++;
                            } else {
                                if (!oldType.equals(""))
                                {
                                    if (oldTypeNumber == 1)
                                    {
                                        log.info(oldType);
                                    } else {
                                        log.info(oldType + " (" + oldTypeNumber
                                                 + "x)");
                                    }
                                }
                                oldType = newType;
                                oldTypeNumber = 1;
                            }
                        } catch (com.workingdogs.village.DataSetException e) {
                            log.info("(Couldn't get command and/or/component "
                                    +"for some reason)");
                        }
                    }
                    //Display the last set of tasks.
                    if (oldTypeNumber == 1)
                    {
                        log.info(oldType);
                    } else {
                        log.info(oldType + " (" + oldTypeNumber + "x)");
                    }


                    log.info("--------------------------------------");
                
                    try {
                        for (Iterator i = tasksToRun.iterator(); i.hasNext();)
                        {
                            if (stopNow) break;
                            TigrisTask t = (TigrisTask)i.next();
                            t.runTask();
                        }
                    } catch (UpdateException 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();
                    
                        HelmEmailer emailer = new 
                            HelmEmailer(connection, new DataManager());
                        emailer.sendError(subject, body);
                    }
                }
            } catch (Exception e) {
                log.exception(e);
            } finally {
                // Return the connection to the pool.
                if (dbConn != null)
                {
                    try
                    {
                        TurbineDB.releaseConnection(dbConn);
                    }
                    catch (Exception e)
                    {
                        throw new RuntimeException(e.getMessage());
                    }
                }
            }
        }
        instances.remove(this);
        log.info("TigrisTaskRunner shutting down. Current number of threads: " 
                 + numberOfInstances());
    }
}
@


1.12
log
@readding all the files as part of the process of returning to the way
things were as of tuesday
@
text
@@


1.12.2.1
log
@Porting to the Turbine Peer system.  -dlr and leonardr
@
text
@@


1.12.2.2
log
@Ported task system to peers.
@
text
@d50 1
d53 2
a54 1
import org.apache.turbine.util.Log;
d56 1
a56 1
import org.tigris.helm.om.Task;
d58 3
a60 2
//import org.tigris.helm.tool.TigrisUpdate;
//import org.tigris.helm.tool.HelmEmailer;
d70 3
d97 1
a97 1
        Log.info("Initializing TigrisTaskRunner. Current number of threads: " +
d109 3
d116 5
a120 2
            {            
                tasksToRun = Task.getTasks();
d130 45
a174 2
                    logTasks(tasksToRun);
                    //Run each task.                
d179 4
a182 9
                            Task t = (Task)i.next();
                            Task.runTask(t);
                            if (stopNow)
                            {
                                Log.debug("Interrupting task running because thread was stopped.");
                                break;
                            }
                        }                        
                  } catch (TaskException e) {
d189 3
a191 2
                        //TODO: HelmEmailer emailer = new HelmEmailer();
                        //emailer.sendError(subject, body);
d195 4
a198 26
                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(""))
d200 1
a200 1
                    if (oldTypeNumber == 1)
d202 3
a204 3
                        Log.debug(oldType);
                    } 
                    else 
d206 1
a206 1
                        Log.debug(oldType + " (" + oldTypeNumber + "x)");
a208 9
                oldType = newType;
                oldTypeNumber = 1;
            }
            //Display the last set of tasks.
            if (oldTypeNumber == 1)
            {
                Log.debug(oldType);
            } else {
                Log.debug(oldType + " (" + oldTypeNumber + "x)");
a209 2
            
            Log.debug("--------------------------------------");
d211 3
@


1.12.2.3
log
@As long as we're rebuilding the directory structure, we might as well sort out the tool package.
@
text
@@


1.11
log
@revert this stuff to its state as of 2001/01/24 12:42:00 GMT -- with
the following exceptions, which have some different modifications so
they'll work with changes which have happened in joist:

ProjectNewsEdit.java
SkinManager.java
SkinStyleElementValueManager.java
StyleElementManager.java
StyleManager.java

it compiles.  i dunno if it works, though.
@
text
@@


1.10
log
@Cleanup of long lines, excessive whitespace, and incorrect comments.
@
text
@@


1.10.2.1
log
@Porting changes from HEAD into branch.
@
text
@d126 2
a127 1
                    } catch (InterruptedException e) {                        //Do nothing.
@


1.9
log
@When large numbers of similar tasks are being run, display 'Helm::MemberDelete (3x)' instead of 'Helm::MemberDelete\nHelm::MemberDelete\nHelm::MemberDelete' in the log.
@
text
@d97 2
a98 1
        log.info("Creating new TigrisTaskRunner. Current number of threads: " + numberOfInstances());
d140 2
a141 1
                            String newType = t.getComponent() + "::" + t.getCommand();
d152 2
a153 1
                                        log.info(oldType + " (" + oldTypeNumber + "x)");
d191 1
a191 1
                        emailer.sendError(subject, body);                        
d212 2
a213 1
        log.info("TigrisTaskRunner shutting down. Current number of threads: " + numberOfInstances());
d215 1
a215 2
} /* end class QueueHandler */

@


1.8
log
@Removed extraneous debug message.
@
text
@d17 1
a17 1
 * software developed by CollabNet (http://www.Collab.Net/)."
d32 1
a32 1
 * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
d133 2
d136 1
a136 1
                    {
d139 17
a155 1
                            log.info(t.getComponent() + "::" + t.getCommand());
d161 9
@


1.7
log
@You can now stop and restart the TigrisTaskRunner thread from the StaticUtils servlet.
@
text
@a125 1
                        log.info("Sleeping.");
@


1.6
log
@Migrated to latest Turbine CVS to avoid connection release bug (fixed long ago in Turbine CVS--we just had a bunk version).
@
text
@d73 32
d111 2
a112 1
        while (true)
d126 1
d149 1
d182 2
@


1.5
log
@my last commit was unnecessary; but this is.  avoid connection leaks.
@
text
@d53 1
a53 1
import org.apache.turbine.services.db.PoolBrokerService;
d83 1
a83 1
                dbConn = PoolBrokerService.getInstance().getConnection();
d138 1
a138 2
                        PoolBrokerService.getInstance()
                            .releaseConnection(dbConn);
@


1.4
log
@make more of an effort to release connections
@
text
@a85 3
            } catch (Exception e) {
                log.exception(e);
            }
d87 1
a87 3
            if (tasksToRun.isEmpty())
            {
                try 
d89 5
a93 16
                    sleep(5000);
                } catch (InterruptedException e) {
                    //Do nothing.
                }
            } else { 
                //Run each task.
                log.info("--------------------------------------");
                log.info("TigrisTaskRunner has tasks to perform!");
                log.info("Task summary for this round:");
                for (Iterator i = tasksToRun.iterator(); i.hasNext();)
                {
                    TigrisTask t = (TigrisTask)i.next();
                    try {
                        log.info(t.getComponent() + "::" + t.getCommand());
                    } catch (com.workingdogs.village.DataSetException e) {
                        log.info("(Couldn't get command and/or/component for some reason)");
d95 5
a99 4
                }
                log.info("--------------------------------------");
                
                try {
d103 6
a108 1
                        t.runTask();
d110 14
a123 6
                } catch (UpdateException 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();
d125 12
a136 7
                    HelmEmailer emailer = new 
                        HelmEmailer(connection, new DataManager());
                    emailer.sendError(subject, body);                        
                } finally {
                    // Return the connection to the pool.
                    RuntimeException re = null;
                    if (dbConn != null)
d138 2
a139 9
                        try
                        {
                            PoolBrokerService.getInstance()
                                .releaseConnection(dbConn);
                        }
                        catch (Exception e)
                        {
                            re = new RuntimeException(e.getMessage());
                        }
d141 3
a143 7
                    if (connection != null) {
                        try {
                            connection.close();
                        } catch (Exception e) {
                            System.err.println("got an exception trying to " +
                                    "close connection: " + e.getMessage());
                        }
a144 2
                    if (re != null)
                        throw re;
@


1.3
log
@Get a new connection every time to avoid timeouts.
@
text
@d132 1
d142 1
a142 1
                            throw new RuntimeException(e.getMessage());
d145 10
@


1.2
log
@Logging cleanup.
@
text
@a76 1
        //Note that once we have this connection, we never release it.
d83 2
a84 5
                if (connection == null)
                {
                    dbConn = PoolBrokerService.getInstance().getConnection();
                    connection = dbConn.getConnection();
                }
d130 15
a144 1
                }               
@


1.1
log
@My side of the changes edk made.
@
text
@d104 14
@

