1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "github.com/sirupsen/logrus"
17 // remoteRunner handles the starting and stopping of a crunch-run
18 // process on a remote machine.
19 type remoteRunner struct {
22 envJSON json.RawMessage
25 timeoutTERM time.Duration
26 timeoutSignal time.Duration
27 onUnkillable func(uuid string) // callback invoked when giving up on SIGTERM
28 onKilled func(uuid string) // callback invoked when process exits after SIGTERM
29 logger logrus.FieldLogger
31 stopping bool // true if Stop() has been called
32 givenup bool // true if timeoutTERM has been reached
33 closed chan struct{} // channel is closed if Close() has been called
36 // newRemoteRunner returns a new remoteRunner. Caller should ensure
37 // Close() is called to release resources.
38 func newRemoteRunner(uuid string, wkr *worker) *remoteRunner {
39 // Send the instance type record as a JSON doc so crunch-run
41 var instJSON bytes.Buffer
42 enc := json.NewEncoder(&instJSON)
43 enc.SetIndent("", " ")
44 if err := enc.Encode(wkr.instType); err != nil {
47 env := map[string]string{
48 "ARVADOS_API_HOST": wkr.wp.arvClient.APIHost,
49 "ARVADOS_API_TOKEN": wkr.wp.arvClient.AuthToken,
50 "InstanceType": instJSON.String(),
52 if wkr.wp.arvClient.Insecure {
53 env["ARVADOS_API_HOST_INSECURE"] = "1"
55 envJSON, err := json.Marshal(env)
61 executor: wkr.executor,
63 runnerCmd: wkr.wp.runnerCmd,
64 remoteUser: wkr.instance.RemoteUser(),
65 timeoutTERM: wkr.wp.timeoutTERM,
66 timeoutSignal: wkr.wp.timeoutSignal,
67 onUnkillable: wkr.onUnkillable,
68 onKilled: wkr.onKilled,
69 logger: wkr.logger.WithField("ContainerUUID", uuid),
70 closed: make(chan struct{}),
75 // Start a crunch-run process on the remote host.
77 // Start does not return any error encountered. The caller should
78 // assume the remote process _might_ have started, at least until it
79 // probes the worker and finds otherwise.
80 func (rr *remoteRunner) Start() {
81 cmd := rr.runnerCmd + " --detach --stdin-env '" + rr.uuid + "'"
82 if rr.remoteUser != "root" {
85 stdin := bytes.NewBuffer(rr.envJSON)
86 stdout, stderr, err := rr.executor.Execute(nil, cmd, stdin)
88 rr.logger.WithField("stdout", string(stdout)).
89 WithField("stderr", string(stderr)).
91 Error("error starting crunch-run process")
94 rr.logger.Info("crunch-run process started")
97 // Close abandons the remote process (if any) and releases
98 // resources. Close must not be called more than once.
99 func (rr *remoteRunner) Close() {
103 // Kill starts a background task to kill the remote process, first
104 // trying SIGTERM until reaching timeoutTERM, then calling
107 // SIGKILL is not used. It would merely kill the crunch-run supervisor
108 // and thereby make the docker container, arv-mount, etc. invisible to
109 // us without actually stopping them.
111 // Once Kill has been called, calling it again has no effect.
112 func (rr *remoteRunner) Kill(reason string) {
117 rr.logger.WithField("Reason", reason).Info("killing crunch-run process")
119 termDeadline := time.Now().Add(rr.timeoutTERM)
120 t := time.NewTicker(rr.timeoutSignal)
126 case time.Now().After(termDeadline):
127 rr.logger.Debug("giving up")
129 rr.onUnkillable(rr.uuid)
132 rr.kill(syscall.SIGTERM)
138 func (rr *remoteRunner) kill(sig syscall.Signal) {
139 logger := rr.logger.WithField("Signal", int(sig))
140 logger.Info("sending signal")
141 cmd := fmt.Sprintf(rr.runnerCmd+" --kill %d %s", sig, rr.uuid)
142 if rr.remoteUser != "root" {
145 stdout, stderr, err := rr.executor.Execute(nil, cmd, nil)
147 logger.WithFields(logrus.Fields{
148 "stderr": string(stderr),
149 "stdout": string(stdout),
151 }).Info("kill attempt unsuccessful")
157 func (rr *remoteRunner) isClosed() bool {