dor-services-client 3.4.0 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc6f22107403e5fc4222c05ca9bebfd64bb927946558ec947dc9a267aca259c5
4
- data.tar.gz: 8be38410f5ce1372134c2f2a705424453a4fb2fd3e99f21814ee5b052c015dca
3
+ metadata.gz: 6e698ec62b7dcceba6c335032d1ccf993c02bee7945cf5865c617c41bb00fa9e
4
+ data.tar.gz: 290957e4e5d96d0b44ffe4e9ccdfcf921947a49e809433139a979f5090176fe6
5
5
  SHA512:
6
- metadata.gz: e80c475af39123c7e4d6b397a03e2a603878e61800d9b2bc8a4e8c18c4c5f0b941bf2edec35e522378f29ca8f46b738f9196c6e2e476ff4b2fbe6dab68258e08
7
- data.tar.gz: 2f050d33ff044fa0bf4f76ea57d75022b96ba44794d2d7384c43ae8485b0d1e47150ae839d271a367832d2cec2d728aa401e60b9832c3061c2d26a5076b4c6f5
6
+ metadata.gz: cd790382965aa0d8ead2bbcc73a699ddce26bdb7f623932753208ead6bb8484f36668e94bde69474cace9e40e376629ca5f38cd1dfad0d82294de791fcce6cdb
7
+ data.tar.gz: 3ca07906e522ce83ae5046147b752100d02914b2b1fb8ca511c1bbdb8b100dc11faafa9c6593b21b6128f429167acca1cc96e5f5425acbe3b05561e8bf7411a6
data/README.md CHANGED
@@ -127,6 +127,25 @@ object_client.workspace.reset
127
127
  object_client.embargo.update(embargo_date: date_string, requesting_user: username_string)
128
128
  ```
129
129
 
130
+ ## Asynchonous results
131
+
132
+ Some operations are asynchronous and they return a `Location` header that displays the
133
+ result of the job. These jobs can be monitored by using `AsyncResult`.
134
+
135
+ ```
136
+ background_result_url = virtual_objects_client.create(virtual_objects: [{ parent_id: '', child_ids: [''] }])
137
+ result = AsyncResult.new(url: background_result_url)
138
+
139
+ # Checks the result one time
140
+ result.complete?
141
+
142
+ # Poll until complete
143
+ result.wait_until_complete
144
+
145
+ result.errors
146
+ # => [{ 'druid:foo' => ['druid:bar'] }]
147
+ ```
148
+
130
149
  ## Development
131
150
 
132
151
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'timeout'
4
+
5
+ module Dor
6
+ module Services
7
+ class Client
8
+ # A helper for monitoring asynchonous jobs
9
+ class AsyncResult
10
+ # @param [String] url the url of the background result
11
+ def initialize(url:)
12
+ @url = url
13
+ end
14
+
15
+ # @param [Integer] seconds_between_requests (3) how many seconds between polling requests
16
+ # @param [Integer] timeout_in_seconds (180) timeout after this many seconds
17
+ # @return true if successful false if unsuccessful.
18
+ def wait_until_complete(seconds_between_requests: 3, timeout_in_seconds: 180)
19
+ poll_until_complete(seconds_between_requests, timeout_in_seconds)
20
+ errors.nil?
21
+ end
22
+
23
+ # Checks to see if the result is complete.
24
+ def complete?
25
+ @results = Dor::Services::Client.background_job_results.show(job_id: job_id_from(url: url))
26
+ @results[:status] == 'complete'
27
+ end
28
+
29
+ def errors
30
+ @results[:output][:errors]
31
+ end
32
+
33
+ attr_reader :url, :seconds_between_requests, :timeout_in_seconds
34
+
35
+ private
36
+
37
+ def poll_until_complete(seconds_between_requests, timeout_in_seconds)
38
+ Timeout.timeout(timeout_in_seconds) do
39
+ loop do
40
+ break if complete?
41
+
42
+ sleep(seconds_between_requests)
43
+ end
44
+ end
45
+ rescue Timeout::Error
46
+ @results = { output: { errors: ["Not complete after #{timeout_in_seconds} seconds"] } }
47
+ end
48
+
49
+ def job_id_from(url:)
50
+ url.split('/').last
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -3,7 +3,7 @@
3
3
  module Dor
4
4
  module Services
5
5
  class Client
6
- VERSION = '3.4.0'
6
+ VERSION = '3.5.0'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dor-services-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.0
4
+ version: 3.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Coyne
@@ -221,6 +221,7 @@ files:
221
221
  - bin/setup
222
222
  - dor-services-client.gemspec
223
223
  - lib/dor/services/client.rb
224
+ - lib/dor/services/client/async_result.rb
224
225
  - lib/dor/services/client/background_job_results.rb
225
226
  - lib/dor/services/client/collections.rb
226
227
  - lib/dor/services/client/embargo.rb