mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-06-23 04:10:25 +00:00
b44780df95
CI / lint (push) Has been cancelled
CI / test (agent) (push) Has been cancelled
CI / test (cli) (push) Has been cancelled
CI / test (cmd) (push) Has been cancelled
CI / test (internal) (push) Has been cancelled
CI / test (manager, true) (push) Has been cancelled
CI / test (pkg) (push) Has been cancelled
CI / upload-coverage (push) Has been cancelled
* feat: Enhance OCI image extraction to return algorithm and requirements paths, and add deferred cleanup for temporary files. Signed-off-by: Sammy Oina <sammyoina@gmail.com> * feat: implement deterministic zipping and enhance checksum verification for resources Signed-off-by: Sammy Oina <sammyoina@gmail.com> * feat: Update component build sources, add gRPC health checks to the CVM server, and refine algorithm argument handling and documentation. Signed-off-by: Sammy Oina <sammyoina@gmail.com> * docs: Update remote resources testing guide with `sudo` for KBS, algorithm result saving, `requirements.txt`, and `algo-args` for RVPS. Signed-off-by: Sammy Oina <sammyoina@gmail.com> * refactor: Explicitly ignore `stderr.Write` return values and add minor whitespace in tests. Signed-off-by: Sammy Oina <sammyoina@gmail.com> * test: add comprehensive error path and edge case tests for file, zip, OCI, and agent components. Signed-off-by: Sammy Oina <sammyoina@gmail.com> * feat: Add mutexes for thread-safe algorithm execution and expand recognized data file extensions to include common archive formats. Signed-off-by: Sammy Oina <sammyoina@gmail.com> * feat: Add OCI extraction tests for Python algorithms and multi-layer datasets, refactor algorithm execution for testability, and enhance algorithm stop and error handling tests. Signed-off-by: Sammy Oina <sammyoina@gmail.com> * test: Add error assertions to OCI extraction test helpers and remove an unused mock exec command. Signed-off-by: Sammy Oina <sammyoina@gmail.com> * test: Improve error handling test coverage for algorithm execution and OCI resource extraction. Signed-off-by: Sammy Oina <sammyoina@gmail.com> * fix: Improve algorithm process termination, enhance computation error handling, and add concurrency safety to agent service. Signed-off-by: Sammy Oina <sammyoina@gmail.com> --------- Signed-off-by: Sammy Oina <sammyoina@gmail.com>
84 lines
1.7 KiB
Go
84 lines
1.7 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package wasm
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"os/exec"
|
|
"sync"
|
|
|
|
"github.com/ultravioletrs/cocos/agent/algorithm"
|
|
"github.com/ultravioletrs/cocos/agent/algorithm/logging"
|
|
"github.com/ultravioletrs/cocos/agent/events"
|
|
)
|
|
|
|
var execCommand = exec.Command
|
|
|
|
const wasmRuntime = "wasmedge"
|
|
|
|
var mapDirOption = []string{"--dir", ".:" + algorithm.ResultsDir}
|
|
|
|
var _ algorithm.Algorithm = (*wasm)(nil)
|
|
|
|
type wasm struct {
|
|
algoFile string
|
|
stderr io.Writer
|
|
stdout io.Writer
|
|
args []string
|
|
cmd *exec.Cmd
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func NewAlgorithm(logger *slog.Logger, eventsSvc events.Service, args []string, algoFile, cmpID string) algorithm.Algorithm {
|
|
return &wasm{
|
|
algoFile: algoFile,
|
|
stderr: &logging.Stderr{Logger: logger, EventSvc: eventsSvc, CmpID: cmpID},
|
|
stdout: &logging.Stdout{Logger: logger},
|
|
args: args,
|
|
}
|
|
}
|
|
|
|
func (w *wasm) Run() error {
|
|
args := append(mapDirOption, w.algoFile)
|
|
args = append(args, w.args...)
|
|
w.mu.Lock()
|
|
w.cmd = execCommand(wasmRuntime, args...)
|
|
w.cmd.Stderr = w.stderr
|
|
w.cmd.Stdout = w.stdout
|
|
|
|
if err := w.cmd.Start(); err != nil {
|
|
w.mu.Unlock()
|
|
return fmt.Errorf("error starting algorithm: %v", err)
|
|
}
|
|
w.mu.Unlock()
|
|
|
|
if err := w.cmd.Wait(); err != nil {
|
|
return fmt.Errorf("algorithm execution error: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (w *wasm) Stop() error {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
|
|
if w.cmd == nil {
|
|
return nil
|
|
}
|
|
|
|
if w.cmd.Process == nil {
|
|
return nil
|
|
}
|
|
|
|
if err := w.cmd.Process.Kill(); err != nil && !errors.Is(err, os.ErrProcessDone) {
|
|
return fmt.Errorf("error stopping algorithm: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|