mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-28 19:16:33 +00:00
Brings in the 73 upstream commits since b13a482 (2026-10-20 to 2026-11-05):
cancellation of running server jobs (#2190), recipe based model management
and idempotent model loading (#2176, #2188, #2217, #2221), ADetailer in the
server (#2164), generation metadata returned with the core results (#2206,
which changes generate_image and generate_video), integrated GPUs in the
automatic memory planning (#2228), discrete GPUs preferred in auto-fit
(#2259), Vulkan capacity checks retried after reclaiming memory (#2256),
the layer split fix across CUDA devices (#2262), RPC devices in
--list-devices (#2182), new models and many speed-ups (FLUX.3, Fibo 2.0,
MiniMax-H3, P-H3-Image, MOSS, Z-Image control-net, INT8 CUDA fusions),
Node 24 and ccache in the workflows, a CUTLASS submodule for the CUDA build,
and the ggml sync to 135ce1e.
Conflicts resolved by hand in 25 files, keeping both sides:
- include/stable-diffusion.h, src/stable-diffusion.cpp,
src/core/ggml_runner.cpp, src/pipeline/diffusion_engine.cpp,
src/pipeline/image.cpp: upstream's result API stays (generate_image and
generate_video fill a result out-parameter, free_sd_image_generation_result
releases it); the fork's classified errors, exception barrier and busy
check wrap it, so a failed call still records its reason and clears the
result. memory_guard, disable_backend_fallback and multi_gpu follow
upstream's last sd_ctx_params_t field. A stop on request is logged as
information, not as an error.
- src/core/backend_fit.cpp: integrated GPUs are enumerated as upstream now
does; which device becomes the main one is still decided by the fork's
rules (discrete before integrated, a card seen by both CUDA and Vulkan
counts once), which cover the case of #2259 as well.
- src/core/ggml_extend_backend.cpp, CMakeLists.txt: the NVIDIA driver is
still loaded at run time (sd::cuda_driver; the toolkit provides headers
only), so upstream's cuda_compiled_arch asks the compute capability
through it and not through <cuda.h>; the CUTLASS include of upstream
stays.
- src/model_manager.cpp: the CPU memory guard and upstream's capacity
rechecks (#2183, #2256) both apply.
- examples/server (runtime, main, async_jobs, routes_*): the context keeper
is still the only owner of the context, and ServerRuntime has no context
or mutex of its own. Upstream's model catalog (--model-dir, the
/sdcpp/v1/models routes, recipes) asks the keeper to load and unload
(ContextKeeper::load, unload, has_model), so a model loaded from the
catalog gets the same recovery ladder as the one given with -m, built from
the options of the new model. ADetailer runs with the context and the
options of the profile in use. The server still starts without a model.
- Cancellation: every job has a CancelToken that the keeper registers only
while the job generates. POST /sdcpp/v1/jobs/{id}/cancel answers at once
when called without ?wait=, as upstream does (the job can still be
generating, with cancel_requested true); with ?wait=N it waits for the end
of the job and answers 200, or 202 if it is still ending, which is what
the fork's guardian expects. cancel_requested is always in the job JSON.
- examples/common/common.{h,cpp}: upstream's --list-devices with RPC devices
and --taesd-preview-only next to the fork's --profile, --memory-guard,
--multi-gpu and --disable-backend-fallback.
- .github/workflows/build.yml: upstream's Node 24, ccache and CUDA header
triggers next to the fork's CPU Docker variant, lowercase image names,
combined CUDA and Vulkan Windows zip and Italian release notes.
examples/cli and examples/server CMakeLists: profile.cpp and recovery.cpp
are added to upstream's source lists.
- docs/backend.md, docs/build.md, examples/server/api.md: the sections of
both sides are kept. The paragraph on how a layer split assigns blocks is
the fork's, which is what layer_split_partition.cpp does (the first device
takes blocks until its free memory is used up), not upstream's
"proportional" wording.
The ggml submodule moves from dc99877 to 135ce1e, the frontend submodule to
fa93429 and the new thirdparty/cutlass submodule is added, all at upstream's
pins. A checkout needs git submodule update --init --recursive.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
581 lines
22 KiB
C++
581 lines
22 KiB
C++
// Extracted from main.cpp during server refactor.
|
|
|
|
#include "async_jobs.h"
|
|
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
#include <thread>
|
|
|
|
#include "common/log.h"
|
|
#include "common/media_io.h"
|
|
#include "common/resource_owners.hpp"
|
|
|
|
class JobCancellationMonitor {
|
|
std::mutex mutex_;
|
|
std::condition_variable cv_;
|
|
bool done_ = false;
|
|
std::thread thread_;
|
|
|
|
public:
|
|
// The keeper reaches a context only while this job's attempt is generating on it, so the stop cannot get to
|
|
// another request, nor to a context that has been released. The monitor must not outlive keeper or job.
|
|
JobCancellationMonitor(ContextKeeper& keeper, AsyncGenerationJob& job)
|
|
: thread_([this, &keeper, &job]() {
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
while (!done_) {
|
|
if (job.cancel.requested.load()) {
|
|
// Each generation pass resets the library flag, including ADetailer.
|
|
keeper.cancel(job.cancel);
|
|
}
|
|
cv_.wait_for(lock, std::chrono::milliseconds(10), [this]() { return done_; });
|
|
}
|
|
}) {}
|
|
|
|
~JobCancellationMonitor() {
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
done_ = true;
|
|
}
|
|
cv_.notify_one();
|
|
thread_.join();
|
|
}
|
|
};
|
|
|
|
static void preview_callback(int step, int frame_count, sd_image_t* frames, bool is_noisy, void* data) {
|
|
(void)is_noisy;
|
|
if (frame_count <= 0 || frames == nullptr || frames[0].data == nullptr) {
|
|
return;
|
|
}
|
|
|
|
AsyncGenerationJob* job = static_cast<AsyncGenerationJob*>(data);
|
|
const sd_image_t& frame = frames[0];
|
|
const sd_preview_info_t info = sd_get_preview_info();
|
|
|
|
auto image_bytes = encode_image_to_vector(EncodedImageFormat::PNG,
|
|
frame.data,
|
|
frame.width,
|
|
frame.height,
|
|
frame.channel,
|
|
"",
|
|
80);
|
|
if (image_bytes.empty()) {
|
|
return;
|
|
}
|
|
|
|
std::string b64 = base64_encode(image_bytes);
|
|
std::lock_guard<std::mutex> lock(job->preview_mutex);
|
|
job->preview_b64 = std::move(b64);
|
|
job->preview_step = step < 0 ? -step : step;
|
|
job->preview_pass = info.sample_pass;
|
|
job->preview_total_steps = info.total_steps;
|
|
}
|
|
|
|
static void clear_preview_callback() {
|
|
sd_set_preview_callback(nullptr, PREVIEW_NONE, 1, false, false, nullptr);
|
|
}
|
|
|
|
static void set_preview_callback(AsyncGenerationJob& job) {
|
|
preview_t mode = str_to_preview(job.kind == AsyncJobKind::ImgGen
|
|
? job.img_gen.preview_mode.c_str()
|
|
: job.vid_gen.preview_mode.c_str());
|
|
if (mode == PREVIEW_COUNT || mode == PREVIEW_NONE) {
|
|
return;
|
|
}
|
|
int interval = job.kind == AsyncJobKind::ImgGen
|
|
? job.img_gen.preview_interval
|
|
: job.vid_gen.preview_interval;
|
|
if (interval <= 0) {
|
|
interval = 1;
|
|
}
|
|
sd_set_preview_callback(preview_callback, mode, interval, true, false, &job);
|
|
}
|
|
|
|
const char* async_job_kind_name(AsyncJobKind kind) {
|
|
switch (kind) {
|
|
case AsyncJobKind::ImgGen:
|
|
return "img_gen";
|
|
case AsyncJobKind::VidGen:
|
|
return "vid_gen";
|
|
default:
|
|
return "img_gen";
|
|
}
|
|
}
|
|
|
|
const char* async_job_status_name(AsyncJobStatus status) {
|
|
switch (status) {
|
|
case AsyncJobStatus::Queued:
|
|
return "queued";
|
|
case AsyncJobStatus::Generating:
|
|
return "generating";
|
|
case AsyncJobStatus::Completed:
|
|
return "completed";
|
|
case AsyncJobStatus::Failed:
|
|
return "failed";
|
|
case AsyncJobStatus::Cancelled:
|
|
return "cancelled";
|
|
default:
|
|
return "failed";
|
|
}
|
|
}
|
|
|
|
void purge_expired_jobs(AsyncJobManager& manager) {
|
|
const int64_t now = unix_timestamp_now_ms();
|
|
|
|
for (auto it = manager.expired_jobs.begin(); it != manager.expired_jobs.end();) {
|
|
if (it->second <= now) {
|
|
it = manager.expired_jobs.erase(it);
|
|
} else {
|
|
++it;
|
|
}
|
|
}
|
|
|
|
for (auto it = manager.jobs.begin(); it != manager.jobs.end();) {
|
|
const auto& job = it->second;
|
|
if (job->completed_at == 0) {
|
|
++it;
|
|
continue;
|
|
}
|
|
|
|
int64_t ttl_seconds = job->status == AsyncJobStatus::Completed
|
|
? manager.completed_ttl_seconds
|
|
: manager.failed_ttl_seconds;
|
|
if (now - job->completed_at >= ttl_seconds * 1000) {
|
|
manager.expired_jobs[job->id] = now + std::max<int64_t>(ttl_seconds, 60) * 1000;
|
|
it = manager.jobs.erase(it);
|
|
} else {
|
|
++it;
|
|
}
|
|
}
|
|
}
|
|
|
|
size_t count_pending_jobs(const AsyncJobManager& manager) {
|
|
size_t pending = 0;
|
|
for (const auto& entry : manager.jobs) {
|
|
if (entry.second->status == AsyncJobStatus::Queued ||
|
|
entry.second->status == AsyncJobStatus::Generating) {
|
|
++pending;
|
|
}
|
|
}
|
|
return pending;
|
|
}
|
|
|
|
std::string make_async_job_id(AsyncJobManager& manager) {
|
|
std::ostringstream oss;
|
|
oss << "job_" << std::hex << unix_timestamp_now_ms() << "_" << std::setw(8)
|
|
<< std::setfill('0') << manager.next_id++;
|
|
return oss.str();
|
|
}
|
|
|
|
bool cancel_queued_job(AsyncJobManager& manager, AsyncGenerationJob& job) {
|
|
auto new_end = std::remove(manager.queue.begin(), manager.queue.end(), job.id);
|
|
if (new_end == manager.queue.end()) {
|
|
return false;
|
|
}
|
|
|
|
manager.queue.erase(new_end, manager.queue.end());
|
|
job.cancel.requested.store(true);
|
|
job.status = AsyncJobStatus::Cancelled;
|
|
job.completed_at = unix_timestamp_now_ms();
|
|
job.result_images_b64.clear();
|
|
job.result_generation_info = nullptr;
|
|
job.result_media_b64.clear();
|
|
job.result_media_mime_type.clear();
|
|
job.result_frame_count = 0;
|
|
job.result_fps = 0;
|
|
job.error_code = "cancelled";
|
|
job.error_message = "job cancelled by client";
|
|
return true;
|
|
}
|
|
|
|
bool cancel_generating_job(ServerRuntime& runtime,
|
|
const std::shared_ptr<AsyncGenerationJob>& job,
|
|
std::chrono::milliseconds wait) {
|
|
constexpr std::chrono::milliseconds kRepeatInterval(100);
|
|
|
|
AsyncJobManager& manager = *runtime.async_job_manager;
|
|
const auto deadline = std::chrono::steady_clock::now() + wait;
|
|
for (;;) {
|
|
runtime.keeper->cancel(job->cancel);
|
|
|
|
std::unique_lock<std::mutex> lock(manager.mutex);
|
|
const auto next = std::min(deadline, std::chrono::steady_clock::now() + kRepeatInterval);
|
|
if (manager.finished_cv.wait_until(lock, next, [&]() { return job->status != AsyncJobStatus::Generating; })) {
|
|
return true;
|
|
}
|
|
if (std::chrono::steady_clock::now() >= deadline) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
json make_async_job_json(const AsyncJobManager& manager, const AsyncGenerationJob& job) {
|
|
json result;
|
|
result["id"] = job.id;
|
|
result["kind"] = async_job_kind_name(job.kind);
|
|
result["status"] = async_job_status_name(job.status);
|
|
result["cancel_requested"] = job.cancel.requested.load();
|
|
result["created"] = job.created_at;
|
|
result["started"] = job.started_at == 0 ? json(nullptr) : json(job.started_at);
|
|
result["completed"] = job.completed_at == 0 ? json(nullptr) : json(job.completed_at);
|
|
result["queue_position"] = 0;
|
|
|
|
if (job.status == AsyncJobStatus::Queued) {
|
|
size_t position = 1;
|
|
for (const auto& queued_id : manager.queue) {
|
|
if (queued_id == job.id) {
|
|
result["queue_position"] = position;
|
|
break;
|
|
}
|
|
++position;
|
|
}
|
|
}
|
|
|
|
if (job.status == AsyncJobStatus::Completed) {
|
|
if (job.kind == AsyncJobKind::VidGen) {
|
|
result["result"] = {
|
|
{"output_format", job.vid_gen.output_format},
|
|
{"mime_type", job.result_media_mime_type},
|
|
{"fps", job.result_fps},
|
|
{"frame_count", job.result_frame_count},
|
|
{"b64_json", job.result_media_b64},
|
|
{"generation_info", job.result_generation_info},
|
|
};
|
|
} else {
|
|
json images = json::array();
|
|
for (size_t i = 0; i < job.result_images_b64.size(); ++i) {
|
|
images.push_back({{"index", i}, {"b64_json", job.result_images_b64[i]}, {"generation_info", job.result_generation_info.at(i)}});
|
|
}
|
|
result["result"] = {
|
|
{"output_format", job.img_gen.output_format},
|
|
{"images", images},
|
|
};
|
|
}
|
|
result["error"] = nullptr;
|
|
} else if (job.status == AsyncJobStatus::Failed ||
|
|
job.status == AsyncJobStatus::Cancelled) {
|
|
result["result"] = nullptr;
|
|
result["error"] = {
|
|
{"code",
|
|
job.error_code.empty()
|
|
? (job.status == AsyncJobStatus::Cancelled ? "cancelled" : "generation_failed")
|
|
: job.error_code},
|
|
{"message", job.error_message},
|
|
};
|
|
} else {
|
|
result["result"] = nullptr;
|
|
result["error"] = nullptr;
|
|
}
|
|
|
|
if (job.status == AsyncJobStatus::Generating) {
|
|
std::lock_guard<std::mutex> lock(job.preview_mutex);
|
|
if (!job.preview_b64.empty()) {
|
|
result["preview"] = {
|
|
{"pass", job.preview_pass},
|
|
{"step", job.preview_step},
|
|
{"total_steps", job.preview_total_steps},
|
|
{"b64_json", job.preview_b64},
|
|
};
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
static bool fail_with(GenerationFailure& failure, const char* message) {
|
|
failure = GenerationFailure();
|
|
failure.code = SD_ERROR_UNKNOWN;
|
|
failure.message = message;
|
|
return false;
|
|
}
|
|
|
|
static bool fail_cancelled(GenerationFailure& failure) {
|
|
failure = GenerationFailure();
|
|
failure.code = SD_ERROR_CANCELLED;
|
|
failure.message = "the generation was cancelled";
|
|
return false;
|
|
}
|
|
|
|
// Runs the attempt on the keeper's context. While it runs, a stop the client asks for reaches the library however
|
|
// many generation passes the attempt makes.
|
|
static bool run_job(ServerRuntime& runtime,
|
|
AsyncGenerationJob& job,
|
|
const ContextKeeper::Attempt& attempt,
|
|
GenerationFailure& failure) {
|
|
JobCancellationMonitor cancellation(*runtime.keeper, job);
|
|
return runtime.keeper->run(attempt, failure, &job.cancel);
|
|
}
|
|
|
|
bool execute_img_gen_job(ServerRuntime& runtime,
|
|
AsyncGenerationJob& job,
|
|
std::vector<std::string>& output_images,
|
|
json& output_info,
|
|
GenerationFailure& failure) {
|
|
output_info = json::array();
|
|
const sd_img_gen_params_t params = job.img_gen.to_sd_img_gen_params_t();
|
|
|
|
SDImageVec results;
|
|
bool adetailer_failed = false;
|
|
const bool generated = run_job(
|
|
runtime,
|
|
job,
|
|
[&](const ContextKeeper::ContextView& view) {
|
|
sd_img_gen_params_t attempt_params = params;
|
|
if (view.force_vae_tiling) {
|
|
attempt_params.vae_tiling_params.enabled = true;
|
|
}
|
|
adetailer_failed = false;
|
|
set_preview_callback(job);
|
|
sd_image_generation_result_t* produced = nullptr;
|
|
const bool ok = generate_image(view.ctx, &attempt_params, &produced);
|
|
clear_preview_callback();
|
|
if (!ok) {
|
|
return false;
|
|
}
|
|
results.adopt(produced, view.params->lora_model_dir);
|
|
if (job.cancel.requested.load()) {
|
|
return true;
|
|
}
|
|
if (!results.empty() && !apply_server_adetailer(runtime, view.ctx, *view.params, job.img_gen, attempt_params, results)) {
|
|
adetailer_failed = true;
|
|
return false;
|
|
}
|
|
return true;
|
|
},
|
|
failure);
|
|
if (!generated) {
|
|
if (adetailer_failed && failure.code != SD_ERROR_CANCELLED) {
|
|
failure.message = failure.message.empty() ? "ADetailer failed" : "ADetailer failed: " + failure.message;
|
|
}
|
|
return false;
|
|
}
|
|
if (job.cancel.requested.load()) {
|
|
return fail_cancelled(failure);
|
|
}
|
|
|
|
const int num_results = results.count();
|
|
if (num_results <= 0) {
|
|
return fail_with(failure, "generate_image returned no results");
|
|
}
|
|
|
|
EncodedImageFormat encoded_format = EncodedImageFormat::PNG;
|
|
if (job.img_gen.output_format == "jpeg") {
|
|
encoded_format = EncodedImageFormat::JPEG;
|
|
} else if (job.img_gen.output_format == "webp") {
|
|
encoded_format = EncodedImageFormat::WEBP;
|
|
}
|
|
|
|
for (int i = 0; i < num_results; ++i) {
|
|
if (results[i].data == nullptr) {
|
|
continue;
|
|
}
|
|
|
|
const std::string metadata = job.img_gen.gen_params.embed_image_metadata
|
|
? results.generation_info(i)
|
|
: "";
|
|
auto image_bytes = encode_image_to_vector(encoded_format,
|
|
results[i].data,
|
|
results[i].width,
|
|
results[i].height,
|
|
results[i].channel,
|
|
metadata,
|
|
job.img_gen.output_compression);
|
|
if (image_bytes.empty()) {
|
|
continue;
|
|
}
|
|
output_images.push_back(base64_encode(image_bytes));
|
|
output_info.push_back(json::parse(results.generation_info(i)));
|
|
}
|
|
|
|
if (output_images.empty()) {
|
|
return fail_with(failure, "generate_image returned empty encoded outputs");
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool execute_vid_gen_job(ServerRuntime& runtime,
|
|
AsyncGenerationJob& job,
|
|
std::string& output_media_b64,
|
|
std::string& output_media_mime_type,
|
|
int& output_frame_count,
|
|
int& output_fps,
|
|
json& output_info,
|
|
GenerationFailure& failure) {
|
|
const sd_vid_gen_params_t params = job.vid_gen.to_sd_vid_gen_params_t();
|
|
std::string str_params;
|
|
|
|
SDImageVec results;
|
|
int num_results = 0;
|
|
sd_audio_t* generated_audio = nullptr;
|
|
|
|
const bool generated = run_job(
|
|
runtime,
|
|
job,
|
|
[&](const ContextKeeper::ContextView& view) {
|
|
sd_vid_gen_params_t attempt_params = params;
|
|
if (view.force_vae_tiling) {
|
|
attempt_params.vae_tiling_params.enabled = true;
|
|
}
|
|
set_preview_callback(job);
|
|
sd_video_generation_result_t* produced = nullptr;
|
|
const bool ok = generate_video(view.ctx, &attempt_params, &produced);
|
|
clear_preview_callback();
|
|
if (!ok) {
|
|
return false;
|
|
}
|
|
output_fps = produced->fps;
|
|
generated_audio = produced->audio;
|
|
produced->audio = nullptr;
|
|
results.adopt_video(produced, view.params->lora_model_dir);
|
|
return true;
|
|
},
|
|
failure);
|
|
if (!generated) {
|
|
return false;
|
|
}
|
|
|
|
num_results = results.count();
|
|
if (job.cancel.requested.load()) {
|
|
free_sd_audio(generated_audio);
|
|
return fail_cancelled(failure);
|
|
}
|
|
if (num_results <= 0) {
|
|
free_sd_audio(generated_audio);
|
|
return fail_with(failure, "generate_video returned no results");
|
|
}
|
|
json info = json::parse(results.generation_info(0), nullptr, false);
|
|
output_info = info.is_discarded() ? json::object() : std::move(info);
|
|
if (job.vid_gen.gen_params.embed_image_metadata) {
|
|
str_params = results.generation_info(0);
|
|
}
|
|
|
|
std::vector<uint8_t> video_bytes = create_video_from_sd_images_to_vector(job.vid_gen.output_format,
|
|
results.data(),
|
|
num_results,
|
|
output_fps,
|
|
job.vid_gen.output_compression,
|
|
generated_audio,
|
|
str_params);
|
|
free_sd_audio(generated_audio);
|
|
if (video_bytes.empty()) {
|
|
return fail_with(failure, "failed to encode generated video container");
|
|
}
|
|
|
|
output_media_b64 = base64_encode(video_bytes);
|
|
output_media_mime_type = video_mime_type(job.vid_gen.output_format);
|
|
output_frame_count = num_results;
|
|
return true;
|
|
}
|
|
|
|
void async_job_worker(ServerRuntime& runtime) {
|
|
AsyncJobManager& manager = *runtime.async_job_manager;
|
|
|
|
while (true) {
|
|
std::shared_ptr<AsyncGenerationJob> job;
|
|
{
|
|
std::unique_lock<std::mutex> lock(manager.mutex);
|
|
manager.cv.wait(lock, [&]() { return manager.stop || !manager.queue.empty(); });
|
|
|
|
if (manager.stop && manager.queue.empty()) {
|
|
break;
|
|
}
|
|
|
|
purge_expired_jobs(manager);
|
|
if (manager.queue.empty()) {
|
|
continue;
|
|
}
|
|
|
|
const std::string job_id = manager.queue.front();
|
|
manager.queue.pop_front();
|
|
|
|
auto it = manager.jobs.find(job_id);
|
|
if (it == manager.jobs.end()) {
|
|
continue;
|
|
}
|
|
|
|
job = it->second;
|
|
job->status = AsyncJobStatus::Generating;
|
|
job->started_at = unix_timestamp_now_ms();
|
|
}
|
|
|
|
std::vector<std::string> output_images;
|
|
json output_info;
|
|
std::string output_media_b64;
|
|
std::string output_media_mime_type;
|
|
int output_frame_count = 0;
|
|
int output_fps = 0;
|
|
GenerationFailure failure;
|
|
bool ok = false;
|
|
|
|
try {
|
|
if (job->kind == AsyncJobKind::ImgGen) {
|
|
ok = execute_img_gen_job(runtime, *job, output_images, output_info, failure);
|
|
} else if (job->kind == AsyncJobKind::VidGen) {
|
|
ok = execute_vid_gen_job(runtime,
|
|
*job,
|
|
output_media_b64,
|
|
output_media_mime_type,
|
|
output_frame_count,
|
|
output_fps,
|
|
output_info,
|
|
failure);
|
|
} else {
|
|
fail_with(failure, "unsupported job kind");
|
|
}
|
|
} catch (const std::exception& error) {
|
|
// An exception must not end the only thread that serves the queue.
|
|
ok = false;
|
|
failure = failure_from_exception(error);
|
|
LOG_ERROR("job %s stopped by an exception: %s", job->id.c_str(), error.what());
|
|
} catch (...) {
|
|
ok = false;
|
|
fail_with(failure, "unexpected error while generating");
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(manager.mutex);
|
|
auto it = manager.jobs.find(job->id);
|
|
if (it == manager.jobs.end()) {
|
|
continue;
|
|
}
|
|
|
|
job->completed_at = unix_timestamp_now_ms();
|
|
if (job->cancel.requested.load()) {
|
|
job->status = AsyncJobStatus::Cancelled;
|
|
job->error_code = "cancelled";
|
|
job->error_message = "job cancelled by client";
|
|
LOG_INFO("job %s cancelled", job->id.c_str());
|
|
} else if (ok) {
|
|
job->status = AsyncJobStatus::Completed;
|
|
job->result_images_b64 = std::move(output_images);
|
|
job->result_generation_info = std::move(output_info);
|
|
job->result_media_b64 = std::move(output_media_b64);
|
|
job->result_media_mime_type = std::move(output_media_mime_type);
|
|
job->result_frame_count = output_frame_count;
|
|
job->result_fps = output_fps;
|
|
job->error_code.clear();
|
|
job->error_message.clear();
|
|
} else {
|
|
const bool cancelled = failure.code == SD_ERROR_CANCELLED;
|
|
job->status = cancelled ? AsyncJobStatus::Cancelled : AsyncJobStatus::Failed;
|
|
job->error_code = failure_code_name(failure);
|
|
job->error_message = cancelled ? "job cancelled by client"
|
|
: (failure.message.empty() ? "unknown generation error" : failure.message);
|
|
job->result_images_b64.clear();
|
|
job->result_generation_info = nullptr;
|
|
job->result_media_b64.clear();
|
|
job->result_media_mime_type.clear();
|
|
job->result_frame_count = 0;
|
|
job->result_fps = 0;
|
|
if (cancelled) {
|
|
LOG_INFO("job %s cancelled", job->id.c_str());
|
|
}
|
|
}
|
|
|
|
purge_expired_jobs(manager);
|
|
manager.finished_cv.notify_all();
|
|
}
|
|
}
|
|
}
|