From 5976c751048100e813edd638632927794df072f2 Mon Sep 17 00:00:00 2001 From: Lucas Di Pentima Date: Wed, 22 Mar 2017 15:01:48 -0300 Subject: [PATCH] 10218: Get the container record directly from the API server, instead of Marshaling runner.Container. --- services/crunch-run/crunchrun.go | 30 +++++++++++++++++---------- services/crunch-run/crunchrun_test.go | 20 ++++++++++++++++++ 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/services/crunch-run/crunchrun.go b/services/crunch-run/crunchrun.go index a3338ec87f..63b63a16a2 100644 --- a/services/crunch-run/crunchrun.go +++ b/services/crunch-run/crunchrun.go @@ -33,6 +33,7 @@ type IArvadosClient interface { Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error Call(method, resourceType, uuid, action string, parameters arvadosclient.Dict, output interface{}) error + CallRaw(method string, resourceType string, uuid string, action string, parameters arvadosclient.Dict) (reader io.ReadCloser, err error) Discovery(key string) (interface{}, error) } @@ -128,7 +129,6 @@ func (runner *ContainerRunner) SetupSignals() { go func(sig chan os.Signal) { <-sig - log.Print("signal handler calling runner.stop()") runner.stop() signal.Stop(sig) }(runner.SigChan) @@ -566,19 +566,27 @@ func (runner *ContainerRunner) LogContainerRecord() (err error) { "container", runner.LogCollection.Open("container.json"), } - logger := log.New(w, "container", 0) - - // Convert container record to pretty-printed JSON []byte - rec, err := json.MarshalIndent(runner.Container, "", " ") + // Get Container record JSON from the API Server + reader, err := runner.ArvClient.CallRaw("GET", "containers", runner.Container.UUID, "", nil) if err != nil { - return fmt.Errorf("While converting container record to JSON: %v", err) + return fmt.Errorf("While retrieving container record from the API server: %v", err) } - - // Write JSON record line-by-line - for _, line := range strings.Split(string(rec), "\n") { - logger.Println(line) + // Read the API server response as []byte + json_bytes, err := ioutil.ReadAll(reader) + if err != nil { + return fmt.Errorf("While reading container record API server response: %v", err) + } + // Decode the JSON []byte + var cr map[string]interface{} + if err = json.Unmarshal(json_bytes, &cr); err != nil { + return fmt.Errorf("While decoding the container record JSON response: %v", err) + } + // Re-encode it using indentation to improve readability + enc := json.NewEncoder(w) + enc.SetIndent("", " ") + if err = enc.Encode(cr); err != nil { + return fmt.Errorf("While logging the JSON container record: %v", err) } - err = w.Close() if err != nil { return fmt.Errorf("While closing container.json log: %v", err) diff --git a/services/crunch-run/crunchrun_test.go b/services/crunch-run/crunchrun_test.go index c26830da55..9b12dd96fd 100644 --- a/services/crunch-run/crunchrun_test.go +++ b/services/crunch-run/crunchrun_test.go @@ -184,6 +184,21 @@ func (client *ArvTestClient) Call(method, resourceType, uuid, action string, par } } +func (client *ArvTestClient) CallRaw(method, resourceType, uuid, action string, + parameters arvadosclient.Dict) (reader io.ReadCloser, err error) { + j := []byte(`{ + "command": ["sleep", "1"], + "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122", + "cwd": ".", + "environment": {}, + "mounts": {"/tmp": {"kind": "tmp"} }, + "output_path": "/tmp", + "priority": 1, + "runtime_constraints": {} + }`) + return ioutil.NopCloser(bytes.NewReader(j)), nil +} + func (client *ArvTestClient) Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error { if resourceType == "collections" { if uuid == hwPDH { @@ -322,6 +337,11 @@ func (ArvErrorTestClient) Call(method, resourceType, uuid, action string, parame return errors.New("ArvError") } +func (ArvErrorTestClient) CallRaw(method, resourceType, uuid, action string, + parameters arvadosclient.Dict) (reader io.ReadCloser, err error) { + return nil, errors.New("ArvError") +} + func (ArvErrorTestClient) Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error { return errors.New("ArvError") } -- 2.30.2