Files
OrcaSlicer/src/slic3r/Utils/Moonraker.cpp
T
packerlschupfer d59155b27f Moonraker: pass print=true in upload — fix Upload & Print race (fixes #14945) (#15032)
* Moonraker: pass print=true in upload — fixes Upload & Print race with power-on-upload

Closes #14945.

Upload & Print on the Moonraker (Klipper) host type failed with
HTTP 503 "Klippy Host not connected" on any printer that Moonraker
powers up in response to an upload (the [power] on_when_upload_queued
feature). The file landed on disk, the print never started, and the
user hit an error dialog.

Root cause: after POST /server/files/upload succeeds we immediately
fire POST /printer/print/start. On a cold printer that Moonraker just
powered up, Klippy is still coming up when /printer/print/start
arrives, so Moonraker returns 503.

Fix: add `print=true` to the upload multipart form. Moonraker's own
upload endpoint queues the print inside the upload response — and
when a [power] device with on_when_upload_queued is configured, it
powers the printer on and waits for Klippy READY before starting.
That's the whole point of the power-on-upload feature; our second
POST was defeating it.

Also read `result.print_started` from the upload response — when
true, skip our explicit /printer/print/start (Moonraker handled it);
when false (older Moonraker or buddy-fork that ignores the print
flag), fall back to the explicit call so the existing behaviour is
preserved for those servers.

Reporter and root-cause identification: @RubenOllesch.

(cherry picked from commit bd442155ba)

* Moonraker: treat print_queued as Moonraker owning the print

Reading only result.print_started missed the exact case this PR set out to
fix. Moonraker's upload response carries two flags:

    print_started : it began the print immediately
    print_queued  : it accepted the job but has not started it yet

The power-on path (`[power] on_when_upload_queued`) is the second one:
Moonraker queues the job, powers the printer up and waits for Klippy to
report READY, so it answers print_started=false, print_queued=true.

With only print_started read, moonraker_started_print stayed false, the
fallback fired, and our explicit /printer/print/start hit the same not-ready
Klippy that produced the original 503 — i.e. the fix did not fix #14945 for
the configuration that reported it.

Verified the response schema against Moonraker v0.11.0 (API 1.5.0); an
upload with print=true on a ready printer returns:

    {"action": "create_file", "item": {...},
     "print_started": true, "print_queued": false}

Both fields are present, so reading print_queued is safe on this version and
the `false` default keeps older hosts on the existing fallback path.

Caught by @raistlin7447 in review of #15032; the fix is their suggestion.

(cherry picked from commit 43e8eff003)

* Moonraker: read the upload reply's fields at top level

Moonraker's FileUploadHandler writes the upload result straight to the
response instead of wrapping it in {"result": ...} like the endpoints
registered through register_endpoint. item.path, print_started and
print_queued are therefore top-level keys. Reading them under result.
silently fell back to the local filename and to "not started", so the
explicit /printer/print/start still ran after every upload.

Also correct the comments on when Moonraker queues a job instead of
starting it, and on what it renames on upload.
2026-10-10 09:27:05 -03:00

340 lines
15 KiB
C++

#include "Moonraker.hpp"
#include <exception>
#include <sstream>
#include <boost/format.hpp>
#include <boost/log/trivial.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <wx/string.h>
#include <utility>
#include <wx/arrstr.h>
#include "PrintHost.hpp"
#include "libslic3r/PrintConfig.hpp"
#include "slic3r/GUI/I18N.hpp"
#include "slic3r/GUI/format.hpp"
#include "Http.hpp"
namespace pt = boost::property_tree;
namespace Slic3r {
Moonraker::Moonraker(DynamicPrintConfig *config)
: m_host(config->opt_string("print_host"))
, m_apikey(config->opt_string("printhost_apikey"))
, m_cafile(config->opt_string("printhost_cafile"))
, m_ssl_revoke_best_effort(config->opt_bool("printhost_ssl_ignore_revoke"))
{}
const char* Moonraker::get_name() const { return "Moonraker"; }
wxString Moonraker::get_test_ok_msg() const
{
return _(L("Connection to Moonraker is working correctly."));
}
wxString Moonraker::get_test_failed_msg(wxString &msg) const
{
return GUI::format_wxstr("%s: %s", _L("Could not connect to Moonraker"), msg);
}
std::string Moonraker::make_url(const std::string &path) const
{
if (m_host.find("http://") == 0 || m_host.find("https://") == 0) {
if (m_host.back() == '/')
return (boost::format("%1%%2%") % m_host % path).str();
return (boost::format("%1%/%2%") % m_host % path).str();
}
return (boost::format("http://%1%/%2%") % m_host % path).str();
}
void Moonraker::set_auth(Http &http) const
{
//ORCA: Moonraker accepts unauthenticated requests by default; X-Api-Key is the only auth header
// defined by the Moonraker spec. HTTP Basic / Digest do NOT belong here even if the user
// filled the user/password fields — those are PrusaLink/OctoPrint conventions.
if (!m_apikey.empty())
http.header("X-Api-Key", m_apikey);
if (!m_cafile.empty())
http.ca_file(m_cafile);
}
bool Moonraker::test(wxString &msg) const
{
//ORCA: Moonraker's /server/info returns
// { "result": { "klippy_state": "ready|startup|shutdown|error|disconnected", ... } }
// We treat the connection as healthy as long as the envelope is valid and `klippy_state`
// is present — matching the OctoPrint/PrusaLink convention of "can I reach this host?".
// Klipper state (idle, error, etc.) is surfaced to the log but does not gate the test:
// buddy-fork firmwares legitimately report non-`ready` states at idle, and any real upload
// problem will surface a contextual error at upload() time anyway.
const char *name = get_name();
bool res = true;
auto url = make_url("server/info");
BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Get server info at: %2%") % name % url;
auto http = Http::get(std::move(url));
set_auth(http);
http.on_error([&](std::string body, std::string error, unsigned status) {
BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error getting server info: %2%, HTTP %3%, body: `%4%`")
% name % error % status % body;
res = false;
msg = format_error(body, error, status);
})
.on_complete([&](std::string body, unsigned) {
BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: /server/info body: %2%") % name % body;
try {
std::stringstream ss(body);
pt::ptree ptree;
pt::read_json(ss, ptree);
const auto klippy_state = ptree.get_optional<std::string>("result.klippy_state");
if (!klippy_state) {
//ORCA: response wasn't shaped like a Moonraker /server/info reply — likely an OctoPrint
// or PrusaLink host the user mis-selected as Moonraker, or a totally different
// service. Treat as a connection failure with a clear hint.
res = false;
msg = _L("The host responded but it doesn't look like Moonraker (missing result.klippy_state).");
return;
}
BOOST_LOG_TRIVIAL(info) << boost::format("%1%: klippy_state = %2%") % name % (*klippy_state);
} catch (const std::exception &ex) {
res = false;
msg = GUI::format_wxstr(_L("Could not parse Moonraker server response: %s"), ex.what());
}
})
#ifdef WIN32
.ssl_revoke_best_effort(m_ssl_revoke_best_effort)
#endif
.perform_sync();
return res;
}
bool Moonraker::get_storage(wxArrayString &storage_path, wxArrayString &storage_name) const
{
//ORCA: GET /server/files/roots enumerates Moonraker's storage roots (default "gcodes" plus any
// configured extras like "config", "logs", "timelapse"). Only roots with permissions
// including "rw" or "rwd" can receive uploads; we filter to those so the UI dropdown only
// offers usable destinations. The base class returns false (no per-host storage); returning
// true here populates the storage picker in PrintHostDialogs's send-to-print dialog.
// Failures (404 — older Moonraker, or a buddy-fork that doesn't implement the endpoint)
// gracefully degrade to false so upload() falls back to the hardcoded "gcodes" default.
const char *name = get_name();
bool got_any = false;
auto url = make_url("server/files/roots");
BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Enumerating storage roots at: %2%") % name % url;
auto http = Http::get(std::move(url));
set_auth(http);
http.on_error([&](std::string body, std::string error, unsigned status) {
//ORCA: /server/files/roots is optional in the Moonraker spec and absent on older versions
// and slimmer shims (e.g. Prusa-Firmware-Buddy 0.8.x prusalink-shim returns 501). A
// missing endpoint here is benign — upload() silently falls back to the hardcoded
// "gcodes" root — so don't pollute the log at warning level for it. Other HTTP
// errors still warn.
if (status == 404 || status == 501) {
BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: /server/files/roots not implemented (HTTP %2%); upload() will fall back to the \"gcodes\" root.")
% name % status;
} else {
BOOST_LOG_TRIVIAL(warning) << boost::format("%1%: Could not enumerate roots: %2%, HTTP %3%, body: `%4%`")
% name % error % status % body;
}
})
.on_complete([&](std::string body, unsigned) {
BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: /server/files/roots body: %2%") % name % body;
try {
std::stringstream ss(body);
pt::ptree ptree;
pt::read_json(ss, ptree);
const auto result_node = ptree.get_child_optional("result");
if (!result_node)
return;
for (const auto &child : *result_node) {
const std::string &root = child.second.get<std::string>("name", "");
const std::string &perms = child.second.get<std::string>("permissions", "");
if (root.empty() || perms.find('w') == std::string::npos)
continue;
storage_path.Add(wxString::FromUTF8(root));
storage_name.Add(wxString::FromUTF8(root));
got_any = true;
}
} catch (const std::exception &ex) {
BOOST_LOG_TRIVIAL(warning) << boost::format("%1%: Could not parse roots: %2%") % name % ex.what();
}
})
#ifdef WIN32
.ssl_revoke_best_effort(m_ssl_revoke_best_effort)
#endif
.perform_sync();
return got_any;
}
bool Moonraker::start_print(wxString &error_msg, const std::string &filename) const
{
//ORCA: POST /printer/print/start with JSON body { "filename": "<name>.gcode" }.
// `filename` is the item.path /server/files/upload returned (the storage-relative
// path inside `root`, no leading slash, with extension), or the original filename
// when the reply had none. Build the body via property_tree so that special characters
// in the filename (quotes, backslashes) are properly escaped.
const char *name = get_name();
bool res = true;
auto url = make_url("printer/print/start");
pt::ptree body_tree;
body_tree.put("filename", filename);
std::ostringstream body_ss;
pt::write_json(body_ss, body_tree, /*pretty=*/false);
std::string body = body_ss.str();
BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Starting print of %2% at %3%") % name % filename % url;
auto http = Http::post(std::move(url));
set_auth(http);
http.header("Content-Type", "application/json")
.set_post_body(body)
.on_complete([&](std::string body, unsigned status) {
BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: print/start HTTP %2%: %3%") % name % status % body;
})
.on_error([&](std::string body, std::string error, unsigned status) {
BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error starting print at %2%: %3%, HTTP %4%, body: `%5%`")
% name % url % error % status % body;
res = false;
error_msg = format_error(body, error, status);
})
#ifdef WIN32
.ssl_revoke_best_effort(m_ssl_revoke_best_effort)
#endif
.perform_sync();
return res;
}
bool Moonraker::upload(PrintHostUpload upload_data, ProgressFn progress_fn, ErrorFn error_fn, InfoFn info_fn) const
{
// POST /server/files/upload with `print=true` so Moonraker starts the print
// itself (issue #14945). It reports back in two fields: print_started when it
// began the print immediately, print_queued when it could not start it (Klippy
// not ready, or another print running) but put the job in its queue
// ([file_manager] queue_gcode_uploads). That covers the power-on case: a
// [power <device>] section with on_when_job_queued switches the printer on, and
// with [job_queue] load_on_startup the queue runs the job once Klippy is READY.
// Either flag means Moonraker owns the print and we must NOT issue our own
// /printer/print/start. When both are false (no job queue, a non-G-code upload,
// or a server that ignores `print`) we fall back to the explicit start below.
wxString test_msg;
if (!test(test_msg)) {
error_fn(std::move(test_msg));
return false;
}
const char *name = get_name();
const auto upload_filename = upload_data.upload_path.filename();
const auto upload_parent_path = upload_data.upload_path.parent_path();
//ORCA: upload_data.storage is plumbed from the (future) per-printer storage dropdown. When unset,
// fall back to the Moonraker-standard "gcodes" root. Reading it through here means a UI
// addition later (storage picker) needs no change to this method.
const std::string root = upload_data.storage.empty() ? std::string("gcodes") : upload_data.storage;
const bool want_start = upload_data.post_action == PrintHostPostUploadAction::StartPrint;
std::string url = make_url("server/files/upload");
bool result = true;
std::string uploaded_path;
bool moonraker_started_print = false;
//ORCA: gcode inside a .gcode.3mf is index-coded (Metadata/plate_<N>.gcode), so the upload names the
// plate via a 1-based `plateindex` (set only in the .3mf path, see Plater::send_gcode_legacy);
// servers that don't use it ignore the unknown form field.
const std::string plateindex = upload_data.extended("plateindex");
BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Uploading file %2% to %3% (root=%4%, filename=%5%, plateindex=%6%, start_print=%7%)")
% name
% upload_data.source_path
% url
% root
% upload_filename.string()
% (plateindex.empty() ? "-" : plateindex)
% (want_start ? "true" : "false");
auto http = Http::post(std::move(url));
set_auth(http);
http.form_add("root", root);
if (!plateindex.empty())
http.form_add("plateindex", plateindex);
http.form_add("print", want_start ? "true" : "false");
http.form_add_file("file", upload_data.source_path.string(), upload_filename.string())
.on_complete([&](std::string body, unsigned status) {
BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: upload HTTP %2%: %3%") % name % status % body;
try {
std::stringstream ss(body);
pt::ptree ptree;
pt::read_json(ss, ptree);
//ORCA: unlike the other endpoints (compare result.klippy_state in test()), the upload
// answers with a bare object, not a {"result": ...} envelope: Moonraker's
// FileUploadHandler writes finalize_upload()'s dict straight to the response, so
// item, print_started and print_queued are top-level. Reading them under result.
// silently took the fallbacks below on every upload.
//ORCA: Moonraker confirms the storage-relative path in item.path. We pass exactly
// that string to /printer/print/start so any server-side renaming (.ufp stored as
// .gcode, surrounding whitespace and leading slashes stripped) is respected.
const auto stored_path = ptree.get_optional<std::string>("item.path");
if (stored_path) {
uploaded_path = *stored_path;
} else {
//ORCA: fallback if the server response omits item.path (older Moonraker, or
// a buddy-fork that returns a slimmer envelope). Use the original filename.
uploaded_path = upload_filename.string();
BOOST_LOG_TRIVIAL(warning) << boost::format(
"%1%: upload response missing item.path, falling back to original filename `%2%`")
% name % uploaded_path;
}
moonraker_started_print = ptree.get<bool>("print_started", false) ||
ptree.get<bool>("print_queued", false);
} catch (const std::exception &ex) {
BOOST_LOG_TRIVIAL(warning) << boost::format(
"%1%: could not parse upload response (%2%); falling back to original filename")
% name % ex.what();
uploaded_path = upload_filename.string();
}
})
.on_error([&](std::string body, std::string error, unsigned status) {
BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Error uploading to %2%: %3%, HTTP %4%, body: `%5%`")
% name % url % error % status % body;
error_fn(format_error(body, error, status));
result = false;
})
.on_progress([&](Http::Progress progress, bool &cancel) {
progress_fn(std::move(progress), cancel);
if (cancel) {
BOOST_LOG_TRIVIAL(info) << name << ": Upload canceled";
result = false;
}
})
#ifdef WIN32
.ssl_revoke_best_effort(m_ssl_revoke_best_effort)
#endif
.perform_sync();
if (!result)
return false;
// Fallback only when Moonraker neither started nor queued the print. If the
// printer cannot start it, the error from this request is what the user sees.
if (want_start && !uploaded_path.empty() && !moonraker_started_print) {
wxString start_msg;
if (!start_print(start_msg, uploaded_path)) {
error_fn(std::move(start_msg));
return false;
}
}
return true;
}
}