Skip to content

Commit 5805ca7

Browse files
committed
add is_active()
1 parent d0ea9e0 commit 5805ca7

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

tools/server/server-models.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ void server_models::unload(const std::string & name) {
303303
std::lock_guard<std::mutex> lk(mutex);
304304
auto it = mapping.find(name);
305305
if (it != mapping.end()) {
306-
if (it->second.meta.status == SERVER_MODEL_STATUS_LOADED || it->second.meta.status == SERVER_MODEL_STATUS_LOADING) {
306+
if (it->second.meta.is_active()) {
307307
SRV_INF("unloading model instance name=%s\n", name.c_str());
308308
subprocess_destroy(it->second.subproc.get());
309309
// status change will be handled by the managing thread
@@ -318,7 +318,7 @@ void server_models::unload_all() {
318318
{
319319
std::lock_guard<std::mutex> lk(mutex);
320320
for (auto & [name, inst] : mapping) {
321-
if (inst.meta.status == SERVER_MODEL_STATUS_LOADED || inst.meta.status == SERVER_MODEL_STATUS_LOADING) {
321+
if (inst.meta.is_active()) {
322322
SRV_INF("unloading model instance name=%s\n", name.c_str());
323323
subprocess_destroy(inst.subproc.get());
324324
// status change will be handled by the managing thread
@@ -359,7 +359,7 @@ void server_models::ensure_model_loaded(const std::string & name) {
359359
if (!meta.has_value()) {
360360
throw std::runtime_error("model name=" + name + " is not found");
361361
}
362-
if (meta->status == SERVER_MODEL_STATUS_LOADED || meta->status == SERVER_MODEL_STATUS_LOADING) {
362+
if (meta->is_active()) {
363363
return; // already loaded
364364
}
365365
SRV_INF("model name=%s is not loaded, loading...\n", name.c_str());

tools/server/server-models.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@
1111
#include <functional>
1212
#include <memory>
1313

14+
/**
15+
* state diagram:
16+
*
17+
* UNLOADED ──► LOADING ──► LOADED
18+
* ▲ │
19+
* │ │
20+
* FAILED ◄───────┘
21+
*/
1422
enum server_model_status {
23+
// TODO: also add downloading state
1524
SERVER_MODEL_STATUS_UNLOADED,
1625
SERVER_MODEL_STATUS_LOADING,
1726
SERVER_MODEL_STATUS_LOADED,
@@ -49,6 +58,9 @@ struct server_model_meta {
4958
bool in_cache = false; // if true, use -hf; use -m otherwise
5059
int port = 0;
5160
server_model_status status = SERVER_MODEL_STATUS_UNLOADED;
61+
bool is_active() const {
62+
return status == SERVER_MODEL_STATUS_LOADED || status == SERVER_MODEL_STATUS_LOADING;
63+
}
5264
};
5365

5466
struct server_models {

0 commit comments

Comments
 (0)