<commit_before>/*
 * The Gemma project
 * 
 * Copyright (c) 2007 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package ubic.gemma.grid.javaspaces;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springmodules.javaspaces.gigaspaces.GigaSpacesTemplate;

import ubic.gemma.util.progress.TaskRunningService;
import ubic.gemma.util.progress.grid.javaspaces.SpacesProgressAppender;

/**
 * Tasks extending this are to be executed in a JavaSpaces environment. This base class serves as starting point for all
 * tasks, providing common functionality such as logging.
 * 
 * @author keshav
 * @version $Id$
 */
public abstract class BaseSpacesTask implements SpacesTask {

    protected GigaSpacesTemplate gigaSpacesTemplate = null;

    protected String taskId;

    private Log log = LogFactory.getLog( this.getClass() );

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
     */
    public void afterPropertiesSet() throws Exception {
        this.taskId = TaskRunningService.generateTaskId();
    }

    /*
     * (non-Javadoc)
     * 
     * @see ubic.gemma.grid.javaspaces.SpacesTask#getTaskId()
     */
    public String getTaskId() {
        return this.taskId;
    }

    /**
     * Creates a new progress appender for tasks. Typically, this method should be called at the beginning of the
     * execute method of the task.
     * 
     * @param clazz
     * @return {@link SpacesProgressAppender}
     */
    public SpacesProgressAppender initProgressAppender( Class<? extends SpacesTask> clazz ) {

        log.info( "Executing task " + clazz.getSimpleName() + " with id " + this.taskId );

        log.debug( "Current Thread: " + Thread.currentThread().getName() + " Authentication: "
                + SecurityContextHolder.getContext().getAuthentication() );

        Logger logger = LogManager.getLogger( "ubic.gemma" );
        logger.setLevel( Level.INFO );

        Logger baseCodeLogger = LogManager.getLogger( "ubic.basecode" );
        baseCodeLogger.setLevel( Level.INFO );

        if ( gigaSpacesTemplate == null )
            throw new RuntimeException( "Will not be able to log information for the task " + clazz.getSimpleName() );

        SpacesProgressAppender javaSpacesAppender = new SpacesProgressAppender( gigaSpacesTemplate, taskId );

        /* Add the appender to the gemma logger */
        if ( !logger.isAttached( javaSpacesAppender ) ) {
            logger.addAppender( javaSpacesAppender );
        }

        /* Add the appender to the basecode logger */
        if ( !baseCodeLogger.isAttached( javaSpacesAppender ) ) {
            baseCodeLogger.addAppender( javaSpacesAppender );
        }
        return javaSpacesAppender;
    }

    /**
     * @param gigaSpacesTemplate
     */
    public void setGigaSpacesTemplate( GigaSpacesTemplate gigaSpacesTemplate ) {
        this.gigaSpacesTemplate = gigaSpacesTemplate;
    }

    /*
     * (non-Javadoc)
     * 
     * @see ubic.gemma.grid.javaspaces.SpacesTask#setTaskId(java.lang.String)
     */
    public void setTaskId( String taskId ) {
        this.taskId = taskId;
    }

    /**
     * Removes the {@link SpacesProgressAppender} from the list of appenders, and removes the progress entry from the
     * space. Typically, this is called after at the end of a task's execute method.<commit_msg>another attempt at fixing grid fifo issue<commit_after>/*
 * The Gemma project
 * 
 * Copyright (c) 2007 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package ubic.gemma.grid.javaspaces;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springmodules.javaspaces.gigaspaces.GigaSpacesTemplate;

import ubic.gemma.util.progress.TaskRunningService;
import ubic.gemma.util.progress.grid.javaspaces.SpacesProgressAppender;

/**
 * Tasks extending this are to be executed in a JavaSpaces environment. This base class serves as starting point for all
 * tasks, providing common functionality such as logging.
 * 
 * @author keshav
 * @version $Id$
 */
public abstract class BaseSpacesTask implements SpacesTask {

    protected GigaSpacesTemplate gigaSpacesTemplate = null;

    protected String taskId;

    private Log log = LogFactory.getLog( this.getClass() );

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
     */
    public void afterPropertiesSet() throws Exception {
        this.taskId = TaskRunningService.generateTaskId();
    }

    /*
     * (non-Javadoc)
     * 
     * @see ubic.gemma.grid.javaspaces.SpacesTask#getTaskId()
     */
    public String getTaskId() {
        return this.taskId;
    }

    /**
     * Creates a new progress appender for tasks. Typically, this method should be called at the beginning of the
     * execute method of the task.
     * 
     * @param clazz
     * @return {@link SpacesProgressAppender}
     */
    public SpacesProgressAppender initProgressAppender( Class<? extends SpacesTask> clazz ) {

        log.info( "Executing task " + clazz.getSimpleName() + " with id " + this.taskId );

        log.debug( "Current Thread: " + Thread.currentThread().getName() + " Authentication: "
                + SecurityContextHolder.getContext().getAuthentication() );

        Logger logger = LogManager.getLogger( "ubic.gemma" );
        logger.setLevel( Level.INFO );

        Logger baseCodeLogger = LogManager.getLogger( "ubic.basecode" );
        baseCodeLogger.setLevel( Level.INFO );

        if ( gigaSpacesTemplate == null )
            throw new RuntimeException( "Will not be able to log information for the task " + clazz.getSimpleName() );

        SpacesProgressAppender javaSpacesAppender = new SpacesProgressAppender( gigaSpacesTemplate, taskId );

        /* Add the appender to the gemma logger */
        if ( !logger.isAttached( javaSpacesAppender ) ) {
            logger.addAppender( javaSpacesAppender );
        }

        /* Add the appender to the basecode logger */
        if ( !baseCodeLogger.isAttached( javaSpacesAppender ) ) {
            baseCodeLogger.addAppender( javaSpacesAppender );
        }
        return javaSpacesAppender;
    }

    /**
     * @param gigaSpacesTemplate
     */
    public void setGigaSpacesTemplate( GigaSpacesTemplate gigaSpacesTemplate ) {
        this.gigaSpacesTemplate = gigaSpacesTemplate;
        this.gigaSpacesTemplate.setFifo( true );
    }

    /*
     * (non-Javadoc)
     * 
     * @see ubic.gemma.grid.javaspaces.SpacesTask#setTaskId(java.lang.String)
     */
    public void setTaskId( String taskId ) {
        this.taskId = taskId;
    }

    /**
     * Removes the {@link SpacesProgressAppender} from the list of appenders, and removes the progress entry from the
     * space. Typically, this is called after at the end of a task's execute method.
{'fraction_non_alphanumeric': 0.0905167246133557, 'fraction_numerical': 0.002637573432442153, 'mean_word_length': 3.0653021442495128, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6988900', 'n_tokens_mistral': 2389, 'n_tokens_neox': 2326, 'n_words': 675}