github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/vm/instance_mgr.go (about)

     1  package vm
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	"github.com/machinefi/w3bstream/pkg/depends/kit/logr"
     9  	"github.com/machinefi/w3bstream/pkg/depends/x/mapx"
    10  	"github.com/machinefi/w3bstream/pkg/enums"
    11  	"github.com/machinefi/w3bstream/pkg/types"
    12  	"github.com/machinefi/w3bstream/pkg/types/wasm"
    13  )
    14  
    15  var instances = mapx.New[types.SFID, wasm.Instance]()
    16  
    17  var (
    18  	ErrNotFound = errors.New("instance not found")
    19  )
    20  
    21  func AddInstanceByID(ctx context.Context, id types.SFID, i wasm.Instance) {
    22  	ctx, l := logr.Start(ctx, "vm.AddInstanceByID")
    23  	defer l.End()
    24  
    25  	instances.Store(id, i)
    26  	l.WithValues("instance", id).Info("instance created")
    27  }
    28  
    29  func DelInstance(ctx context.Context, id types.SFID) error {
    30  	ctx, l := logr.Start(ctx, "vm.DelInstance")
    31  	defer l.End()
    32  
    33  	i, _ := instances.LoadAndRemove(id)
    34  	if i == nil {
    35  		return ErrNotFound
    36  	}
    37  	return i.Stop(ctx)
    38  }
    39  
    40  func StartInstance(ctx context.Context, id types.SFID) error {
    41  	ctx, l := logr.Start(ctx, "vm.StartInstance")
    42  	defer l.End()
    43  
    44  	l = l.WithValues("instance", id)
    45  
    46  	i, ok := instances.Load(id)
    47  	if !ok {
    48  		l.Error(ErrNotFound)
    49  		return ErrNotFound
    50  	}
    51  
    52  	if i.State() == enums.INSTANCE_STATE__STARTED {
    53  		return nil
    54  	}
    55  
    56  	if err := i.Start(ctx); err != nil {
    57  		l.Error(err)
    58  		return err
    59  	}
    60  	l.Info("started")
    61  	return nil
    62  }
    63  
    64  func StopInstance(ctx context.Context, id types.SFID) error {
    65  	ctx, l := logr.Start(ctx, "vm.StopInstance")
    66  	defer l.End()
    67  
    68  	l = l.WithValues("instance", id)
    69  
    70  	i, ok := instances.Load(id)
    71  	if !ok {
    72  		l.Warn(ErrNotFound)
    73  		return ErrNotFound
    74  	}
    75  	if err := i.Stop(ctx); err != nil {
    76  		l.Error(err)
    77  		return err
    78  	}
    79  	l.Info("stopped")
    80  	return nil
    81  }
    82  
    83  func GetInstanceState(id types.SFID) (enums.InstanceState, bool) {
    84  	i, ok := instances.Load(id)
    85  	if !ok {
    86  		return enums.INSTANCE_STATE_UNKNOWN, false
    87  	}
    88  	return i.State(), true
    89  }
    90  
    91  func GetConsumer(id types.SFID) wasm.Instance {
    92  	i, ok := instances.Load(id)
    93  	if !ok || i == nil {
    94  		return nil
    95  	}
    96  	return i
    97  }
    98  
    99  func FetchInstances() map[types.SFID]enums.InstanceState {
   100  	ret := map[types.SFID]enums.InstanceState{}
   101  	instances.Range(func(k types.SFID, v wasm.Instance) bool {
   102  		ret[k] = v.State()
   103  		return true
   104  	})
   105  	return ret
   106  }