<commit_before>/*
 * ProActive Parallel Suite(TM):
 * The Open Source library for parallel and distributed
 * Workflows & Scheduling, Orchestration, Cloud Automation
 * and Big Data Analysis on Enterprise Grids & Clouds.
 *
 * Copyright (c) 2007 - 2017 ActiveEon
 * Contact: <EMAIL>
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation: version 3 of
 * the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 */
package org.ow2.proactive.resourcemanager.selection;

import java.util.List;
import java.util.concurrent.Callable;

import org.apache.log4j.Logger;
import org.objectweb.proactive.api.PAFuture;
import org.objectweb.proactive.core.ProActiveTimeoutException;
import org.objectweb.proactive.core.mop.MOP;
import org.objectweb.proactive.core.node.Node;
import org.ow2.proactive.resourcemanager.core.properties.PAResourceManagerProperties;
import org.ow2.proactive.resourcemanager.rmnode.RMNode;
import org.ow2.proactive.scripting.ScriptException;
import org.ow2.proactive.scripting.ScriptResult;
import org.ow2.proactive.scripting.SelectionScript;
import org.ow2.proactive.utils.Criteria;


public class ScriptExecutor implements Callable<Node> {

    private final static Logger logger = Logger.getLogger(ScriptExecutor.class);

    private final Criteria criteria;

    private RMNode rmnode;

    private SelectionManager manager;

    private List<SelectionScript> selectionScriptList;

    public ScriptExecutor(RMNode rmnode, Criteria criteria, SelectionManager manager) {
        this.rmnode = rmnode;
        this.manager = manager;
        this.criteria = criteria;
        this.selectionScriptList = criteria.getScripts();
    }

    public Node call() throws Exception {
        SelectionManager.maybeSetLoggingContext(criteria);
        try {
            return executeScripts();
        } finally {
            SelectionManager.unsetLoggingContext();
        }
    }

    /**
     * Runs selection scripts and process the results
     * returns node if it matches, null otherwise
     */
    private Node executeScripts() {
        boolean selectionScriptSpecified = selectionScriptList != null && selectionScriptList.size() > 0;
        boolean nodeMatch = true;
        ScriptException exception = null;

        if (selectionScriptSpecified) {
            // initializing parallel script execution
            for (SelectionScript script : selectionScriptList) {
                if (manager.isPassed(script, rmnode)) {
                    // already executed static script
                    logger.info(rmnode.getNodeURL() + " : " + script.hashCode() + " skipping script execution");
                    continue;
                }

                logger.info(rmnode.getNodeURL() + " : " + script.hashCode() + " executing");
                try {
                    ScriptResult<Boolean> scriptResult = rmnode.executeScript(script, criteria.getBindings());

                    // processing the results
                    if (!MOP.isReifiedObject(scriptResult) && scriptResult.getException() != null) {
                        // could not create script execution handler
                        // probably the node id down
                        logger.warn(rmnode.getNodeURL() + " : " + script.hashCode() + " exception",
                                    scriptResult.getException());
                        logger.warn(rmnode.getNodeURL() + " : pinging the node");
                        rmnode.getNodeSource().pingNode(rmnode.getNode());

                        nodeMatch = false;<commit_msg>Decrease the level of log for selection script skipping task (#2802)<commit_after>/*
 * ProActive Parallel Suite(TM):
 * The Open Source library for parallel and distributed
 * Workflows & Scheduling, Orchestration, Cloud Automation
 * and Big Data Analysis on Enterprise Grids & Clouds.
 *
 * Copyright (c) 2007 - 2017 ActiveEon
 * Contact: <EMAIL>
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation: version 3 of
 * the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 */
package org.ow2.proactive.resourcemanager.selection;

import java.util.List;
import java.util.concurrent.Callable;

import org.apache.log4j.Logger;
import org.objectweb.proactive.api.PAFuture;
import org.objectweb.proactive.core.ProActiveTimeoutException;
import org.objectweb.proactive.core.mop.MOP;
import org.objectweb.proactive.core.node.Node;
import org.ow2.proactive.resourcemanager.core.properties.PAResourceManagerProperties;
import org.ow2.proactive.resourcemanager.rmnode.RMNode;
import org.ow2.proactive.scripting.ScriptException;
import org.ow2.proactive.scripting.ScriptResult;
import org.ow2.proactive.scripting.SelectionScript;
import org.ow2.proactive.utils.Criteria;


public class ScriptExecutor implements Callable<Node> {

    private final static Logger logger = Logger.getLogger(ScriptExecutor.class);

    private final Criteria criteria;

    private RMNode rmnode;

    private SelectionManager manager;

    private List<SelectionScript> selectionScriptList;

    public ScriptExecutor(RMNode rmnode, Criteria criteria, SelectionManager manager) {
        this.rmnode = rmnode;
        this.manager = manager;
        this.criteria = criteria;
        this.selectionScriptList = criteria.getScripts();
    }

    public Node call() throws Exception {
        SelectionManager.maybeSetLoggingContext(criteria);
        try {
            return executeScripts();
        } finally {
            SelectionManager.unsetLoggingContext();
        }
    }

    /**
     * Runs selection scripts and process the results
     * returns node if it matches, null otherwise
     */
    private Node executeScripts() {
        boolean selectionScriptSpecified = selectionScriptList != null && selectionScriptList.size() > 0;
        boolean nodeMatch = true;
        ScriptException exception = null;

        if (selectionScriptSpecified) {
            // initializing parallel script execution
            for (SelectionScript script : selectionScriptList) {
                if (manager.isPassed(script, rmnode)) {
                    // already executed static script
                    logger.debug(rmnode.getNodeURL() + " : " + script.hashCode() + " skipping script execution");
                    continue;
                }

                logger.info(rmnode.getNodeURL() + " : " + script.hashCode() + " executing");
                try {
                    ScriptResult<Boolean> scriptResult = rmnode.executeScript(script, criteria.getBindings());

                    // processing the results
                    if (!MOP.isReifiedObject(scriptResult) && scriptResult.getException() != null) {
                        // could not create script execution handler
                        // probably the node id down
                        logger.warn(rmnode.getNodeURL() + " : " + script.hashCode() + " exception",
                                    scriptResult.getException());
                        logger.warn(rmnode.getNodeURL() + " : pinging the node");
                        rmnode.getNodeSource().pingNode(rmnode.getNode());

                        nodeMatch = false;
{'fraction_non_alphanumeric': 0.08425215723873442, 'fraction_numerical': 0.0052732502396931925, 'mean_word_length': 2.6778316438959893, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1798103', 'n_tokens_mistral': 2128, 'n_tokens_neox': 2037, 'n_words': 637}