10218: Get the container record directly from the API server, instead of Marshaling...
authorLucas Di Pentima <lucas@curoverse.com>
Wed, 22 Mar 2017 18:01:48 +0000 (15:01 -0300)
committerLucas Di Pentima <lucas@curoverse.com>
Wed, 22 Mar 2017 18:01:48 +0000 (15:01 -0300)
services/crunch-run/crunchrun.go
services/crunch-run/crunchrun_test.go

index a3338ec87feec5049370cfcab2433cb6962953e8..63b63a16a235fb0dff91e4263853c0623e05a2e5 100644 (file)
@@ -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)
index c26830da552560c6427b8dcf8f30d82fb690bef3..9b12dd96fd2680b3d91c83a7fd7e5adadab2a1e1 100644 (file)
@@ -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")
 }