add is_active()

This commit is contained in:
Xuan Son Nguyen 2025-11-20 16:26:31 +01:00
parent d0ea9e0830
commit 5805ca7960
2 changed files with 15 additions and 3 deletions

View File

@ -303,7 +303,7 @@ void server_models::unload(const std::string & name) {
std::lock_guard<std::mutex> lk(mutex);
auto it = mapping.find(name);
if (it != mapping.end()) {
if (it->second.meta.status == SERVER_MODEL_STATUS_LOADED || it->second.meta.status == SERVER_MODEL_STATUS_LOADING) {
if (it->second.meta.is_active()) {
SRV_INF("unloading model instance name=%s\n", name.c_str());
subprocess_destroy(it->second.subproc.get());
// status change will be handled by the managing thread
@ -318,7 +318,7 @@ void server_models::unload_all() {
{
std::lock_guard<std::mutex> lk(mutex);
for (auto & [name, inst] : mapping) {
if (inst.meta.status == SERVER_MODEL_STATUS_LOADED || inst.meta.status == SERVER_MODEL_STATUS_LOADING) {
if (inst.meta.is_active()) {
SRV_INF("unloading model instance name=%s\n", name.c_str());
subprocess_destroy(inst.subproc.get());
// status change will be handled by the managing thread
@ -359,7 +359,7 @@ void server_models::ensure_model_loaded(const std::string & name) {
if (!meta.has_value()) {
throw std::runtime_error("model name=" + name + " is not found");
}
if (meta->status == SERVER_MODEL_STATUS_LOADED || meta->status == SERVER_MODEL_STATUS_LOADING) {
if (meta->is_active()) {
return; // already loaded
}
SRV_INF("model name=%s is not loaded, loading...\n", name.c_str());

View File

@ -11,7 +11,16 @@
#include <functional>
#include <memory>
/**
* state diagram:
*
* UNLOADED LOADING LOADED
*
*
* FAILED
*/
enum server_model_status {
// TODO: also add downloading state
SERVER_MODEL_STATUS_UNLOADED,
SERVER_MODEL_STATUS_LOADING,
SERVER_MODEL_STATUS_LOADED,
@ -49,6 +58,9 @@ struct server_model_meta {
bool in_cache = false; // if true, use -hf; use -m otherwise
int port = 0;
server_model_status status = SERVER_MODEL_STATUS_UNLOADED;
bool is_active() const {
return status == SERVER_MODEL_STATUS_LOADED || status == SERVER_MODEL_STATUS_LOADING;
}
};
struct server_models {