9701: Merge branch '9463-change-arvput-use-collection-class' into 9701-collection...
[arvados.git] / sdk / go / dispatch / dispatch.go
index 4b66c23b7c8a6c5291d9d6cfa3aaafb0c7a66883..4987c01055203e262b2534e2f001b200c7de21eb 100644 (file)
@@ -22,19 +22,10 @@ const (
        Cancelled = arvados.ContainerStateCancelled
 )
 
-type apiClientAuthorization struct {
-       UUID     string `json:"uuid"`
-       APIToken string `json:"api_token"`
-}
-
-type apiClientAuthorizationList struct {
-       Items []apiClientAuthorization `json:"items"`
-}
-
 // Dispatcher holds the state of the dispatcher
 type Dispatcher struct {
        // The Arvados client
-       Arv arvadosclient.ArvadosClient
+       Arv *arvadosclient.ArvadosClient
 
        // When a new queued container appears and is either already owned by
        // this dispatcher or is successfully locked, the dispatcher will call
@@ -58,7 +49,7 @@ type Dispatcher struct {
 
        mineMutex  sync.Mutex
        mineMap    map[string]chan arvados.Container
-       Auth       apiClientAuthorization
+       Auth       arvados.APIClientAuthorization
        containers chan arvados.Container
 }
 
@@ -187,7 +178,7 @@ func (dispatcher *Dispatcher) handleUpdate(container arvados.Container) {
 
        if container.State == Queued && container.Priority > 0 {
                // Try to take the lock
-               if err := dispatcher.UpdateState(container.UUID, Locked); err != nil {
+               if err := dispatcher.Lock(container.UUID); err != nil {
                        return
                }
                container.State = Locked
@@ -212,6 +203,24 @@ func (dispatcher *Dispatcher) UpdateState(uuid string, newState arvados.Containe
        return err
 }
 
+// Lock makes the lock API call which updates the state of a container to Locked.
+func (dispatcher *Dispatcher) Lock(uuid string) error {
+       err := dispatcher.Arv.Call("POST", "containers", uuid, "lock", nil, nil)
+       if err != nil {
+               log.Printf("Error locking container %s: %q", uuid, err)
+       }
+       return err
+}
+
+// Unlock makes the unlock API call which updates the state of a container to Queued.
+func (dispatcher *Dispatcher) Unlock(uuid string) error {
+       err := dispatcher.Arv.Call("POST", "containers", uuid, "unlock", nil, nil)
+       if err != nil {
+               log.Printf("Error unlocking container %s: %q", uuid, err)
+       }
+       return err
+}
+
 // RunDispatcher runs the main loop of the dispatcher until receiving a message
 // on the dispatcher.DoneProcessing channel.  It also installs a signal handler
 // to terminate gracefully on SIGINT, SIGTERM or SIGQUIT.