Class: HastCI::ApiClient
- Inherits:
-
Object
- Object
- HastCI::ApiClient
- Defined in:
- lib/hastci/api_client.rb
Constant Summary collapse
- OriginalNetHTTP =
Net::HTTP
Instance Method Summary collapse
- #ack(task_id:, status:, duration_s:, logs:) ⇒ Object
- #claim(run_id:, worker_id: @config.worker_id, batch: @config.claim_batch_size) ⇒ Object
- #disconnect! ⇒ Object
- #heartbeat(run_id:, worker_id: @config.worker_id) ⇒ Object
- #init_run(run_key: @config.run_key, worker_id: @config.worker_id, commit_sha: @config.commit_sha) ⇒ Object
-
#initialize(config:, sleeper: Kernel.method(:sleep), max_retries: nil, random: Random.new, http_class: OriginalNetHTTP) ⇒ ApiClient
constructor
A new instance of ApiClient.
- #run_status(run_id:) ⇒ Object
- #seed(run_id:, tasks:) ⇒ Object
Constructor Details
#initialize(config:, sleeper: Kernel.method(:sleep), max_retries: nil, random: Random.new, http_class: OriginalNetHTTP) ⇒ ApiClient
Returns a new instance of ApiClient.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/hastci/api_client.rb', line 33 def initialize(config:, sleeper: Kernel.method(:sleep), max_retries: nil, random: Random.new, http_class: OriginalNetHTTP) @config = config @max_retries = max_retries || config.api_max_retries || DEFAULT_MAX_RETRIES @sleeper = sleeper @random = random @http_class = http_class @base_url = URI.parse(config.api_base_url) @api_key = config.api_key @connections = {} @connection_mutexes = Hash.new { |h, k| h[k] = Mutex.new } @global_mutex = Mutex.new end |
Instance Method Details
#ack(task_id:, status:, duration_s:, logs:) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/hastci/api_client.rb', line 112 def ack(task_id:, status:, duration_s:, logs:) post_json("#{API_PATH_PREFIX}/tasks/#{task_id}/acknowledgment", { status: status.to_s, duration_s: duration_s, logs: logs }, pool: CONNECTION_ACK) end |
#claim(run_id:, worker_id: @config.worker_id, batch: @config.claim_batch_size) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/hastci/api_client.rb', line 97 def claim( run_id:, worker_id: @config.worker_id, batch: @config.claim_batch_size ) response = ensure_hash_response( post_json("#{API_PATH_PREFIX}/runs/#{run_id}/claims?batch=#{batch}", { worker_id: worker_id }), context: "#{API_PATH_PREFIX}/runs/:run_id/claims" ) build_claim_result(response) end |
#disconnect! ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/hastci/api_client.rb', line 49 def disconnect! @global_mutex.synchronize do @connections.each_value do |conn| conn&.finish if conn&.started? rescue IOError end @connections.clear end end |
#heartbeat(run_id:, worker_id: @config.worker_id) ⇒ Object
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/hastci/api_client.rb', line 120 def heartbeat( run_id:, worker_id: @config.worker_id ) post_json("#{API_PATH_PREFIX}/runs/#{run_id}/heartbeats", { worker_id: worker_id }, pool: CONNECTION_HEARTBEAT) nil end |
#init_run(run_key: @config.run_key, worker_id: @config.worker_id, commit_sha: @config.commit_sha) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/hastci/api_client.rb', line 59 def init_run( run_key: @config.run_key, worker_id: @config.worker_id, commit_sha: @config.commit_sha ) response = ensure_hash_response( post_json("#{API_PATH_PREFIX}/runs", { run_key: run_key, worker_id: worker_id, commit_sha: commit_sha }), context: "#{API_PATH_PREFIX}/runs" ) { run_id: fetch_required(response, "run_id", context: "#{API_PATH_PREFIX}/runs"), status: fetch_required(response, "status", context: "#{API_PATH_PREFIX}/runs").to_sym, role: fetch_required(response, "role", context: "#{API_PATH_PREFIX}/runs").to_sym } end |
#run_status(run_id:) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/hastci/api_client.rb', line 86 def run_status(run_id:) response = ensure_hash_response( get_json("#{API_PATH_PREFIX}/runs/#{run_id}"), context: "#{API_PATH_PREFIX}/runs/:run_id" ) { status: fetch_required(response, "status", context: "#{API_PATH_PREFIX}/runs/:run_id").to_sym } end |
#seed(run_id:, tasks:) ⇒ Object
80 81 82 83 84 |
# File 'lib/hastci/api_client.rb', line 80 def seed(run_id:, tasks:) post_json("#{API_PATH_PREFIX}/runs/#{run_id}/seed", { tasks: tasks.map { |name| {name: name} } }) end |