Skip to content

Commit

Permalink
add support for suspend/resume engines (#99)
Browse files Browse the repository at this point in the history
* add support for suspend/resume engines

* fix comment

* start/stop eng

* cleanup error checks

* changelog and version
  • Loading branch information
rsulli-rai authored Sep 14, 2023
1 parent 9b7c655 commit dbf4478
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v0.5.11-alpha
* Add ability to start and stop engines.

## v0.5.10-alpha
* `X-Request-Id` is generated for every HTTP request.
* Stringified `HTTPError` includes the `X-Request-Id` instead of all headers from the response.
Expand Down
64 changes: 64 additions & 0 deletions examples/stop_engine/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2022 RelationalAI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"log"
"os"

"github.com/jessevdk/go-flags"
"github.com/relationalai/rai-sdk-go/rai"
)

type Options struct {
Engine string `short:"e" long:"engine" required:"true" description:"engine name"`
Profile string `long:"profile" default:"default" description:"config profile"`
}

func run(opts *Options) error {
client, err := rai.NewClientFromConfig(opts.Profile)
if err != nil {
return err
}
rsp, err := client.GetEngine(opts.Engine)
if err != nil {
return err
}

switch rsp.State {
case "PROVISIONED":
err := client.StopEngine(opts.Engine)
if err != nil {
return err
}
case "SUSPENDED":
err := client.StartEngine(opts.Engine)
if err != nil {
return err
}
}

return nil
}

func main() {
var opts Options
if _, err := flags.ParseArgs(&opts, os.Args); err != nil {
os.Exit(1)
}
if err := run(&opts); err != nil {
log.Fatal(err)
}
}
14 changes: 14 additions & 0 deletions rai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,20 @@ func (c *Client) ListEngines(filters ...interface{}) ([]Engine, error) {
return result.Engines, nil
}

func (c *Client) StartEngine(engineName string) error {
var result interface{}
data := &SuspendEngineRequest{Suspend: false}
uri := makePath(PathEngine, engineName)
return c.Patch(uri, nil, data, &result)
}

func (c *Client) StopEngine(engineName string) error {
var result interface{}
data := &SuspendEngineRequest{Suspend: true}
uri := makePath(PathEngine, engineName)
return c.Patch(uri, nil, data, &result)
}

//
// OAuth Clients
//
Expand Down
32 changes: 32 additions & 0 deletions rai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,38 @@ func TestEngine(t *testing.T) {
assert.Nil(t, err)
engine = findEngine(engines, test.engineName)
assert.Nil(t, engine)

// resume the test engine
err = client.StopEngine(test.engineName)
assert.Nil(t, err)

waitOrFail := func(state string) {
const maxWaitTime = 600 // seconds
const duration = 5 // seconds
eng := &Engine{}
waitTime := 0
for !isTerminalState(eng.State, state) {
time.Sleep(duration * time.Second)
waitTime += duration
eng, err = client.GetEngine(test.engineName)
assert.Nil(t, err)
assert.Less(t, waitTime, maxWaitTime, "Failed waiting for engine state change: %s", eng.State)
}
}
waitOrFail("SUSPENDED")

// check status
engine, err = client.GetEngine(test.engineName)
assert.Nil(t, err)
assert.NotNil(t, engine)
assert.Equal(t, "SUSPENDED", engine.State)

// suspend the test engine
err = client.StartEngine(test.engineName)
assert.Nil(t, err)

waitOrFail("PROVISIONED")

}

// Test transaction execution.
Expand Down
4 changes: 4 additions & 0 deletions rai/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ type createEngineResponse struct {
Engine Engine `json:"compute"`
}

type SuspendEngineRequest struct {
Suspend bool `json:"suspend"`
}

type createOAuthClientRequest struct {
Name string `json:"name"`
Permissions []string `json:"permissions"`
Expand Down
2 changes: 1 addition & 1 deletion rai/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

package rai

const Version = "0.5.10-alpha"
const Version = "0.5.11-alpha"

0 comments on commit dbf4478

Please sign in to comment.