github.com/apache/beam/sdks/v2@v2.48.2/go/test/integration/internal/jars/run_unix.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one or more
     2  // contributor license agreements.  See the NOTICE file distributed with
     3  // this work for additional information regarding copyright ownership.
     4  // The ASF licenses this file to You under the Apache License, Version 2.0
     5  // (the "License"); you may not use this file except in compliance with
     6  // the License.  You may obtain a copy of the License at
     7  //
     8  //    http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  // Match build constraints of imported package golang.org/x/sys/unix.
    17  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
    18  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos
    19  
    20  package jars
    21  
    22  import (
    23  	"fmt"
    24  	"os/exec"
    25  	"time"
    26  )
    27  
    28  // getTimeoutRunner is an OS-specific branch for determining what behavior to use for Run. This
    29  // Unix specific version handles timeout.
    30  func getTimeoutRunner() runCallback {
    31  	_, err := exec.LookPath("timeout")
    32  	if err != nil {
    33  		// Wrap run with Unix-specific error handling for missing timeout command.
    34  		return func(dur time.Duration, jar string, args ...string) (Process, error) {
    35  			// Currently, we hard-fail here if a duration is provided but timeout is unsupported. If
    36  			// we ever decide to soft-fail instead, this is the code to change.
    37  			if dur != 0 {
    38  				return nil, fmt.Errorf("cannot run jar: duration parameter provided but 'timeout' command not installed: %w", err)
    39  			}
    40  			return run(dur, jar, args...)
    41  		}
    42  	}
    43  
    44  	// Path for a supported timeout, just use the default run function.
    45  	return run
    46  }
    47  
    48  // run starts up a jar, and wraps it in "timeout" only if a duration is provided. Processes are
    49  // returned wrapped as Unix processes that provide graceful shutdown for unix specifically.
    50  func run(dur time.Duration, jar string, args ...string) (Process, error) {
    51  	var cmdArr []string
    52  	if dur != 0 {
    53  		durStr := fmt.Sprintf("%.2fm", dur.Minutes())
    54  		cmdArr = append(cmdArr, "timeout", durStr)
    55  	}
    56  	cmdArr = append(cmdArr, "java", "-jar", jar)
    57  	cmdArr = append(cmdArr, args...)
    58  
    59  	cmd := exec.Command(cmdArr[0], cmdArr[1:]...)
    60  	err := cmd.Start()
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return &UnixProcess{proc: cmd.Process}, nil
    65  }