All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nextflow.util.ProcessHelper.groovy Maven / Gradle / Ivy

Go to download

A DSL modelled around the UNIX pipe concept, that simplifies writing parallel and scalable pipelines in a portable manner

The newest version!
package nextflow.util

import java.lang.management.ManagementFactory

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j

/**
 * Implements helper methods for {@link Process} objects
 *
 * @author Paolo Di Tommaso 
 */
@Slf4j
@CompileStatic
class ProcessHelper {

    static long pid(Process process) {
        try {
            // with Java 9 and later pid can be access with `.toHandle().getPid()` method
            final toHandle = Process.class.getMethod("toHandle")
            final handle = toHandle.invoke(process)
            return (Long) Class.forName("java.lang.ProcessHandle").getMethod("pid").invoke(handle)
        }
        catch (Exception e) {
            // ignore it and fallback on next
        }

        // Fallback for Java6+ on unix. This is known not to work on Windows.
        try {
            final pidField = process.class.getDeclaredField("pid")
            pidField.setAccessible(true)
            return pidField.getLong(process)
        }
        catch(Exception e) {
            throw new UnsupportedOperationException("Unable to access process pid for class: ${process.getClass().getName()}",e )
        }
    }

    static long selfPid() {
        try {
            Long.parseLong(ManagementFactory.getRuntimeMXBean().getName().split("@")[0])
        }
        catch(Exception e) {
            throw new UnsupportedOperationException("Unable to find current process pid",e)
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy