|
| 1 | +require "placeos-core-client" |
| 2 | +require "placeos-driver/proxy/system" |
| 3 | + |
| 4 | +require "./application" |
| 5 | + |
| 6 | +module PlaceOS::Api |
| 7 | + class BuildMonitor < Application |
| 8 | + base "/api/engine/v2/build/" |
| 9 | + |
| 10 | + # Scopes |
| 11 | + ############################################################################################### |
| 12 | + |
| 13 | + before_action :can_read, only: [:monitor] |
| 14 | + before_action :can_write, only: [:cancel] |
| 15 | + |
| 16 | + before_action :check_admin, only: [:cancel] |
| 17 | + |
| 18 | + ############################################################################################### |
| 19 | + |
| 20 | + @[AC::Route::GET("/monitor")] |
| 21 | + def monitor( |
| 22 | + @[AC::Param::Info(name: "state", description: "state of job to return. One of [pending,running,cancelled error,done]. Defaults to 'pending'", example: "pending")] |
| 23 | + state : PlaceOS::Core::Client::State = PlaceOS::Core::Client::State::Pending, |
| 24 | + ) : Array(TaskStatus) | String |
| 25 | + code, result = self.class.monitor_jobs(state, request_id) |
| 26 | + if code < 500 |
| 27 | + render status: code, json: Array(TaskStatus).from_json(result) |
| 28 | + else |
| 29 | + render status: code, text: result |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + @[AC::Route::DELETE("/cancel/:job")] |
| 34 | + def cancel( |
| 35 | + @[AC::Param::Info(name: "job", description: "ID of previously submitted compilation job")] |
| 36 | + job : String, |
| 37 | + ) : CancelStatus |
| 38 | + code, result = self.class.cancel_job(job, request_id) |
| 39 | + render status: code, json: CancelStatus.from_json(result) |
| 40 | + end |
| 41 | + |
| 42 | + def self.monitor_jobs(state : PlaceOS::Core::Client::State, request_id : String) |
| 43 | + details = RemoteDriver.default_discovery.node_hash |
| 44 | + |
| 45 | + promises = details.map do |core_id, uri| |
| 46 | + Promise.defer { |
| 47 | + core_for(uri, request_id) do |core_client| |
| 48 | + core_client.monitor_jobs(state) |
| 49 | + end |
| 50 | + }.catch { |error| |
| 51 | + Log.error(exception: error) { { |
| 52 | + message: "failure to request a build service job status", |
| 53 | + core_uri: uri.to_s, |
| 54 | + core_id: core_id, |
| 55 | + state: state.to_s, |
| 56 | + } } |
| 57 | + {500, error.message || "failure to request a build service job status"} |
| 58 | + } |
| 59 | + end |
| 60 | + |
| 61 | + Promise.race(promises).get |
| 62 | + end |
| 63 | + |
| 64 | + def self.cancel_job(job : String, request_id : String) |
| 65 | + details = RemoteDriver.default_discovery.node_hash |
| 66 | + |
| 67 | + promises = details.map do |core_id, uri| |
| 68 | + Promise.defer { |
| 69 | + core_for(uri, request_id) do |core_client| |
| 70 | + core_client.cancel_job(job) |
| 71 | + end |
| 72 | + }.catch { |error| |
| 73 | + Log.error(exception: error) { { |
| 74 | + message: "failure to request a cancellation of build service job", |
| 75 | + core_uri: uri.to_s, |
| 76 | + core_id: core_id, |
| 77 | + job: job, |
| 78 | + } } |
| 79 | + {500, {status: "error", message: error.message || "failure to request a cancellation of build service job"}.to_json} |
| 80 | + } |
| 81 | + end |
| 82 | + |
| 83 | + Promise.race(promises).get |
| 84 | + end |
| 85 | + |
| 86 | + def self.core_for(uri, request_id : String? = nil, & : Core::Client -> V) forall V |
| 87 | + Core::Client.client(uri: uri, request_id: request_id) do |client| |
| 88 | + yield client |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + enum State |
| 93 | + Pending |
| 94 | + Running |
| 95 | + Cancelled |
| 96 | + Error |
| 97 | + Done |
| 98 | + |
| 99 | + def to_s(io : IO) : Nil |
| 100 | + io << (member_name || value.to_s).downcase |
| 101 | + end |
| 102 | + |
| 103 | + def to_s : String |
| 104 | + String.build { |io| to_s(io) } |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + record TaskStatus, state : State, id : String, message : String, |
| 109 | + driver : String, repo : String, branch : String, commit : String, timestamp : Time do |
| 110 | + include JSON::Serializable |
| 111 | + end |
| 112 | + |
| 113 | + record CancelStatus, status : String, message : String do |
| 114 | + include JSON::Serializable |
| 115 | + end |
| 116 | + end |
| 117 | +end |
0 commit comments