<commit_before>package org.ow2.proactive.rm.util.process;

import java.util.Collections;
import java.util.Map;

import org.apache.log4j.Logger;


/**
 * Utility class to use the ProcessTreeKiller with Environment cookies
 */
public class EnvironmentCookieBasedChildProcessKiller {

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

    private final String cookieName;
    private final String cookieValue;

    public EnvironmentCookieBasedChildProcessKiller(String cookieNameSuffix) {
        cookieValue = ProcessTreeKiller.createCookie();
        cookieName = "PROCESS_KILLER_COOKIE_" + cookieNameSuffix;

        logger.debug("Setting environment cookie " + cookieName + " to: " + cookieValue);
        Environment.setenv(cookieName, cookieValue, true);
    }

    public void killChildProcesses() {
        logger.debug("Node terminating");
        Map<String, String> environmentMap = Collections.singletonMap(cookieName, cookieValue);
        logger.debug("Killing all processes with environment: " + environmentMap);
        try {
            ProcessTreeKiller.get().kill(environmentMap);
        } catch (Throwable e) {
            logger.warn("Unable to kill children processes", e);
        }
    }

    /**
     * Register a shutdown hook to kill children processes
     */
    public static void registerKillChildProcessesOnShutdown(String cookieNameSuffix) {
        final EnvironmentCookieBasedChildProcessKiller processKiller = new EnvironmentCookieBasedChildProcessKiller(
            cookieNameSuffix);

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                processKiller.killChildProcesses();
            }
        });
    }

}<commit_msg>SCHEDULING-2209 Do not kill self with ChildProcessKiller utility<commit_after>package org.ow2.proactive.rm.util.process;

import org.apache.log4j.Logger;

import java.util.Collections;
import java.util.Map;


/**
 * Utility class to use the ProcessTreeKiller with Environment cookies
 */
public class EnvironmentCookieBasedChildProcessKiller {

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

    private final String cookieName;
    private final String cookieValue;

    public EnvironmentCookieBasedChildProcessKiller(String cookieNameSuffix) {
        cookieValue = ProcessTreeKiller.createCookie();
        cookieName = "PROCESS_KILLER_COOKIE_" + cookieNameSuffix;

        logger.debug("Setting environment cookie " + cookieName + " to: " + cookieValue);
        Environment.setenv(cookieName, cookieValue, true);
    }

    public void killChildProcesses() {
        Environment.unsetenv(cookieName); // do not kill current JVM
        Map<String, String> environmentMap = Collections.singletonMap(cookieName, cookieValue);
        logger.debug("Killing all processes with environment: " + environmentMap);
        try {
            ProcessTreeKiller.get().kill(environmentMap);
        } catch (Throwable e) {
            logger.warn("Unable to kill children processes", e);
        }
    }

    /**
     * Register a shutdown hook to kill children processes
     */
    public static void registerKillChildProcessesOnShutdown(String cookieNameSuffix) {
        final EnvironmentCookieBasedChildProcessKiller processKiller = new EnvironmentCookieBasedChildProcessKiller(
            cookieNameSuffix);

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                processKiller.killChildProcesses();
            }
        });
    }

}
{'fraction_non_alphanumeric': 0.0810589519650655, 'fraction_numerical': 0.002183406113537118, 'mean_word_length': 3.2321016166281753, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3639467', 'n_tokens_mistral': 939, 'n_tokens_neox': 908, 'n_words': 219}