@@ -66,9 +66,9 @@ func (s *service) CreateWorkload(id string, payload io.Reader, contentType strin
6666 return "" , err
6767 }
6868
69- statusPath := fmt .Sprintf ("%s/%s/status " , prefix , id )
69+ statusPath := fmt .Sprintf ("%s/%s/%s " , prefix , id , StatusInQueue )
7070 log .Debug (fmt .Sprintf ("setting status to %s" , StatusInQueue ))
71- if err := s .storage .Upload (statusPath , strings .NewReader (string ( StatusInQueue ) ), "text/plain" ); err != nil {
71+ if err := s .storage .Upload (statusPath , strings .NewReader ("" ), "text/plain" ); err != nil {
7272 return "" , err
7373 }
7474
@@ -77,32 +77,24 @@ func (s *service) CreateWorkload(id string, payload io.Reader, contentType strin
7777
7878// GetWorkload retrieves the status and result, if available, of a given workload
7979func (s * service ) GetWorkload (id string ) (GetWorkloadResponse , error ) {
80- prefix := s .workloadStoragePrefix ()
8180 log := s .logger .With (zap .String ("id" , id ))
8281
83- // download workload status
84- statusPath := fmt .Sprintf ("%s/%s/status" , prefix , id )
85- log .Debug ("downloading status file" , zap .String ("path" , statusPath ))
86- statusBuf , err := s .storage .Download (statusPath )
82+ status , err := s .getStatus (id )
8783 if err != nil {
8884 return GetWorkloadResponse {}, err
8985 }
9086
91- status := Status (statusBuf [:])
92- switch status {
93- case StatusFailed , StatusInProgress , StatusInQueue :
87+ if status != StatusCompleted {
9488 return GetWorkloadResponse {
9589 ID : id ,
9690 Status : status ,
9791 }, nil
98- case StatusCompleted : // continues execution after switch/case, below
99- default :
100- return GetWorkloadResponse {}, fmt .Errorf ("invalid workload status: %s" , status )
10192 }
10293
10394 // attempt to download user result
95+ prefix := s .workloadStoragePrefix ()
10496 resultPath := fmt .Sprintf ("%s/%s/result.json" , prefix , id )
105- log .Debug ("donwloading user result" , zap .String ("path" , resultPath ))
97+ log .Debug ("downloading user result" , zap .String ("path" , resultPath ))
10698 resultBuf , err := s .storage .Download (resultPath )
10799 if err != nil {
108100 return GetWorkloadResponse {}, err
@@ -127,6 +119,38 @@ func (s *service) GetWorkload(id string) (GetWorkloadResponse, error) {
127119 }, nil
128120}
129121
122+ func (s * service ) getStatus (id string ) (Status , error ) {
123+ prefix := s .workloadStoragePrefix ()
124+ log := s .logger .With (zap .String ("id" , id ))
125+
126+ // download workload status
127+ log .Debug ("checking status" , zap .String ("path" , fmt .Sprintf ("%s/%s/*" , prefix , id )))
128+ files , err := s .storage .List (fmt .Sprintf ("%s/%s" , prefix , id ))
129+ if err != nil {
130+ return "" , err
131+ }
132+
133+ // determine request status
134+ status := StatusInQueue
135+ for _ , file := range files {
136+ fileStatus := Status (file )
137+
138+ if ! fileStatus .Valid () {
139+ status = fileStatus
140+ return "" , fmt .Errorf ("invalid workload status: %s" , status )
141+ }
142+ if fileStatus == StatusInProgress {
143+ status = fileStatus
144+ }
145+ if fileStatus == StatusCompleted || fileStatus == StatusFailed {
146+ status = fileStatus
147+ break
148+ }
149+ }
150+
151+ return status , nil
152+ }
153+
130154func (s * service ) workloadStoragePrefix () string {
131155 return fmt .Sprintf ("%s/apis/%s/workloads" , s .clusterName , s .apiName )
132156}
0 commit comments