Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/server/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1839,12 +1839,19 @@ the working directory's ./dgl directory unless --dgl-dir is set.
| POST /sdcpp/v1/dgl/script | Saves UTF-8 source with the given name. Pass previous_source to update an existing script. A conflict returns HTTP 409. |
| POST /sdcpp/v1/dgl/jobs | Queues the source string and returns a job ID with HTTP 202 |
| GET /sdcpp/v1/dgl/jobs/{id} | Returns the job status, error or result with tensor descriptions and inline encoded media |
| POST /sdcpp/v1/dgl/jobs/{id}/cancel | Cancels a queued job or requests cancellation of a running job. Send `Content-Type: application/json`. |
| GET /sdcpp/v1/dgl/jobs/{id}/outputs/{index}?frame=0 | Returns a PNG of an image output or a video frame. Output and frame indexes start at zero. |

Scripts share the component cache and execution settings used for generation.
Model reads are restricted to the configured model directories. Script writes
are restricted to output/ in the server's working directory.

Cancellation returns the current job status with HTTP 200. A queued job
becomes `cancelled` immediately. A running job sets the `cancel_requested`
flag. Poll the status endpoint until it reports `cancelled`. Execution stops
at a DGL cancellation check, so an operation currently running may have to
finish first. A terminal job returns its current status.

### Encoded DGL results

The existing job result has `type`, `outputs` and `media`. `outputs` retains
Expand Down
21 changes: 17 additions & 4 deletions examples/server/async_jobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <sstream>
#include <string_view>
#include <thread>
#include <utility>

#include "common/log.h"
#include "common/media_io.h"
Expand All @@ -25,18 +26,24 @@ class JobCancellationMonitor {
public:
// The caller holds sd_ctx_mutex until this monitor has joined, keeping the
// context alive and preventing cancellation from reaching another request.
JobCancellationMonitor(sd_ctx_t* ctx, const AsyncGenerationJob& job)
: thread_([this, ctx, &job]() {
template <typename Cancel>
JobCancellationMonitor(const AsyncGenerationJob& job, Cancel cancel)
: thread_([this, &job, cancel = std::move(cancel)]() {
std::unique_lock<std::mutex> lock(mutex_);
while (!done_) {
if (job.cancel_requested.load()) {
// Each generation pass resets the library flag, including ADetailer.
sd_cancel_generation(ctx, SD_CANCEL_ALL);
cancel();
}
cv_.wait_for(lock, std::chrono::milliseconds(10), [this]() { return done_; });
}
}) {}

JobCancellationMonitor(sd_ctx_t* ctx, const AsyncGenerationJob& job)
: JobCancellationMonitor(job, [ctx]() {
// Each generation pass resets the library flag, including ADetailer.
sd_cancel_generation(ctx, SD_CANCEL_ALL);
}) {}

~JobCancellationMonitor() {
{
std::lock_guard<std::mutex> lock(mutex_);
Expand Down Expand Up @@ -233,6 +240,7 @@ bool cancel_queued_job(AsyncJobManager& manager, AsyncGenerationJob& job) {
manager.queue.erase(new_end, manager.queue.end());
job.model.reset();
job.request_body = nullptr;
job.dgl_source.clear();
job.cancel_requested.store(true);
job.status = AsyncJobStatus::Cancelled;
job.completed_at = unix_timestamp_now_ms();
Expand Down Expand Up @@ -689,6 +697,11 @@ static bool execute_dgl_job(ServerRuntime& runtime,
sd_dgl_result_t* raw_result = nullptr;
{
std::lock_guard<std::mutex> lock(*runtime.sd_ctx_mutex);
if (job.cancel_requested.load())
return false;
JobCancellationMonitor cancellation(job, [ctx = runtime.sd_ctx]() {
sd_dgl_ctx_cancel(sd_ctx_get_dgl_ctx(ctx));
});
update_job_progress(job, "executing", "dgl");
if (sd_dgl_run(sd_ctx_get_dgl_ctx(runtime.sd_ctx), source.c_str(), nullptr, 0, &raw_result) != SD_STATUS_OK) {
error_message = sd_dgl_error();
Expand Down
17 changes: 17 additions & 0 deletions examples/server/routes_dgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,23 @@ void register_dgl_endpoints(httplib::Server& svr, ServerRuntime& runtime) {
send_json(res, 200, make_async_job_json(manager, *job));
}));

svr.Post(R"(/sdcpp/v1/dgl/jobs/([A-Za-z0-9_-]+)/cancel)", local_dgl_route([&runtime](const Request& req, Response& res) {
auto& manager = *runtime.async_job_manager;
std::lock_guard<std::mutex> lock(manager.mutex);
auto job = find_dgl_job(manager, req.matches[1].str(), res);
if (!job)
return;
if (job->status == AsyncJobStatus::Queued) {
if (!cancel_queued_job(manager, *job)) {
send_error(res, 409, "Job queue state changed before cancellation");
return;
}
} else if (job->status == AsyncJobStatus::Generating) {
job->cancel_requested.store(true);
}
send_json(res, 200, make_async_job_json(manager, *job));
}));

svr.Get(R"(/sdcpp/v1/dgl/jobs/([A-Za-z0-9_-]+)/outputs/([0-9]+))", local_dgl_route([&runtime](const Request& req, Response& res) {
auto& manager = *runtime.async_job_manager;
std::shared_ptr<sd_dgl_result_t> result;
Expand Down
Loading