Skip to content

Commit

Permalink
fix: moved context timeout to inside function, added constants for ti…
Browse files Browse the repository at this point in the history
…meout values
  • Loading branch information
ishaansehgal99 committed Sep 11, 2023
1 parent 3f6e271 commit b4b8401
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions pkg/inference/preset-llama2-inferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
)

const (
Preset2ATimeout = 10
Preset2BTimeout = 20
Preset2CTimeout = 30
RegistryName = "aimodelsregistry.azurecr.io"
PresetSetModelllama2AChatImage = RegistryName + "/llama-2-7b-chat:latest"
PresetSetModelllama2BChatImage = RegistryName + "/llama-2-13b-chat:latest"
Expand Down Expand Up @@ -98,10 +101,7 @@ func CreateLLAMA2APresetModel(ctx context.Context, workspaceObj *kdmv1alpha1.Wor
return err
}

ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()

if err := checkResourceStatus(ctxWithTimeout, depObj, kubeClient); err != nil {
if err := checkResourceStatus(depObj, kubeClient, Preset2ATimeout); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -136,10 +136,7 @@ func CreateLLAMA2BPresetModel(ctx context.Context, workspaceObj *kdmv1alpha1.Wor
return err
}

ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 20*time.Minute)
defer cancel()

if err := checkResourceStatus(ctxWithTimeout, depObj, kubeClient); err != nil {
if err := checkResourceStatus(depObj, kubeClient, Preset2BTimeout); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -175,27 +172,26 @@ func CreateLLAMA2CPresetModel(ctx context.Context, workspaceObj *kdmv1alpha1.Wor
return err
}

ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()

if err := checkResourceStatus(ctxWithTimeout, depObj, kubeClient); err != nil {
if err := checkResourceStatus(depObj, kubeClient, Preset2CTimeout); err != nil {
return err
}
return nil
}

func checkResourceStatus(ctx context.Context, obj client.Object, kubeClient client.Client) error {
func checkResourceStatus(obj client.Object, kubeClient client.Client, timeoutDuration int) error {
klog.InfoS("checkResourceStatus", "resource", obj.GetName())

// Use Context for timeout
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutDuration)*time.Minute)
defer cancel()

ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()

// Use context for timeout
timeoutChan := ctx.Done()

for {
select {
case <-timeoutChan:
return fmt.Errorf("check resource status timed out. resource %s is not ready", obj.GetName())
case <-ctx.Done():
return ctx.Err()

case <-ticker.C:
key := client.ObjectKey{
Expand Down

0 comments on commit b4b8401

Please sign in to comment.