Class ::nsmcp::QuestionJobPoller

::nsmcp::QuestionJobPoller[i] create ...

Defined in /usr/local/ns/tcl/nsmcp/question-backend.tcl

Class Relations

  • class: ::nx::Class[i]
  • superclass: ::nsmcp::Object[i]
::nx::Class create ::nsmcp::QuestionJobPoller \
     -superclass ::nsmcp::Object

Methods (to be applied on the object)

  • wait (scripted, public)

     nsmcp::QuestionJobPoller[i] wait name json deadline emit poll [ check ]

    Wait for a background tool job while reporting its progress. Poll retained handles within the question deadline, emit status events and honor cancellation; do not restart the original job.

    Parameters:
    name (required)
    Name of the configured or registered item to resolve.
    json (required)
    Serialized JSON supplied by the backend transport.
    deadline (required)
    Absolute deadline in clock milliseconds.
    emit (required)
    Event callback prefix; returning false requests cancellation.
    poll (required)
    Callback prefix invoked with a retained background-job handle.
    check (optional)
    Optional callback used to check cancellation or other turn constraints.
    Returns:
    The final tool-result JSON, or the current JSON when it is not a pollable job.

    Testcases:
    No testcase defined.
    set delay 0.1
    while {1} {
        set value [ns_json parse $json]
        if {[dict exists $value isError] && [dict get $value isError]} {
            return $json
        }
        if {![dict exists $value structuredContent jobHandle]} {
            return $json
        }
        set job [dict get $value structuredContent]
        set handle [dict get $job jobHandle]
        if {![regexp {^[0-9a-f]{64}$} $handle] || ![dict exists $job status]} {
            return $json
        }
        set status [dict get $job status]
        set event [dict create type job name $name jobHandle $handle status $status]
        if {[dict exists $job progress]} {
            dict set event progress [dict get $job progress]
        }
        if {![uplevel #0 [list {*}$emit $event]]} {
            return -code error -errorcode {NSMCP QUESTION CANCELLED} "Question cancelled; background jobHandle: $handle"
        }
        if {$status ni {queued running}} {
            return $json
        }
        if {[clock milliseconds] >= $deadline} {
            error "Question timed out waiting for background jobHandle $handle; use log_results with this handle to continue"
        }
        if {$check ne ""} {
            uplevel #0 $check
        }
        ns_sleep [expr {min($delay,max(0.001,($deadline-[clock milliseconds])/1000.0))}]
        set json [uplevel #0 [list {*}$poll $handle]]
        set delay [expr {min(1.0,$delay*1.5)}]
    }