arthropod 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 402aefa5ebb2883aa9660285c850995454a8a5dd87dc5cf4b1fc3131c66a25cc
4
- data.tar.gz: fb12c70b9088b22e81cac21de40edd01e23fb50b668b8f775010ed80cfdff31b
3
+ metadata.gz: 7b049a78069051c3d4a0f8f52e9d25b047c884a46e452ff24e57de6882f1b638
4
+ data.tar.gz: 225660b18ac24915428b66bf85ece0ed5f8d21894befbbb0279f317047304151
5
5
  SHA512:
6
- metadata.gz: 2bf3436d18735c33198c163fc950aaca20f663828c6e5cdc918000df26bcf109a1c6abac885c1a96dec22fc42c8a4d0ea64c745165974d3b0a75d4738ed81364
7
- data.tar.gz: 52a1c6f6b0455633e97414eae5eeadf359b4436aae49653714031f9aba8f023538845c77fc9c9536426a5c749073c99aae67db1225345ee793b3916eb23467d1
6
+ metadata.gz: 9cbec6ec5b5dcd4a84868e0b8e0df73341fb289b21efbaea230237d033d1dce50abcce60c14ce3de3342435abcd6685d77114b35bc34d33986debe0fbe01d233
7
+ data.tar.gz: 0037a32c4ab1015430fd8fe2fad2d981ffa2ae8d399f3ad17cdb0007f3455de32e37dc341ef4eda78505c37dd9b71b81e7875209202d19b70351507119a2f5ff
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  .env
2
+ *.gem
data/README.md CHANGED
@@ -4,6 +4,22 @@ Arthropod is a easy way to run remote ruby code synchronously, using Amazon SQS.
4
4
 
5
5
  *Do not use it yet, the API isn't stable at all and it wasn't tested enough on production*
6
6
 
7
+ ## Installation
8
+
9
+ ```
10
+ gem install arthropod
11
+ ```
12
+
13
+ Or in your Gemfile
14
+
15
+ ```
16
+ gem 'arthropod', '~> 0.0.1'
17
+ ```
18
+
19
+ ## Configuration
20
+
21
+ You will need the following environment variables `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_REGION`. Optionally the `Arthropod::Client.push` and `Arthropod::Server.pull` methods can take a `client` argument with your own instance of `Aws::SQS::Client`, see https://docs.aws.amazon.com/en_en/sdk-for-ruby/v3/developer-guide/sqs-examples.html for more information.
22
+
7
23
  ## Usage
8
24
 
9
25
  A simple use case first, let's say you want to push a video encoding task to another server:
@@ -20,7 +36,7 @@ On the "server" side:
20
36
 
21
37
  ```ruby
22
38
  Arthropod::Server.pull(queue_name: "video_encoding") do |request|
23
- video_url = request.body.url
39
+ video_url = request.body["url"]
24
40
 
25
41
  # Do the encoding stuff
26
42
  encoded_video_url = VideoEncoder.encode(video_url)
@@ -58,6 +74,10 @@ puts response.body
58
74
  # => "https://my_storage.my_service.com/my_reencoded_video_file.mp4"
59
75
  ```
60
76
 
77
+ ## Errors
78
+
79
+ Any exception raised on server side will cause the client side to close immediately and raise a `Arthropod::Client::ServerError` exception.
80
+
61
81
  ## API
62
82
 
63
83
  ```ruby
@@ -89,4 +109,4 @@ Of course you can also achieve that by simply using SQS or any kind of message s
89
109
 
90
110
  ## Example: the poor man's video encoding service
91
111
 
92
- If you're not concerned about latency, you can for example push some heavy video encoding task from and ActiveJob job in your Rails task and run a little cron job every minute on your uber-CUDA-powered computer at home to pull those jobs and reencode your videos. It should be reliable enough and it may be even be way faster than doing it with the CPU off a regular server.
112
+ If you're not concerned about latency, you can for example push some heavy video encoding task from and ActiveJob job in your Rails task and run a little cron job every minute on your uber-CUDA-powered computer at home to pull those jobs and reencode your videos. It should be reliable enough and it may be even be way faster than doing it with the CPU of a regular server.
@@ -3,6 +3,8 @@ require 'securerandom'
3
3
 
4
4
  module Arthropod
5
5
  module Client
6
+ class ServerError < StandardError; end
7
+
6
8
  def self.push(queue_name:, body:, client: nil)
7
9
  client ||= Aws::SQS::Client.new
8
10
 
@@ -19,6 +21,8 @@ module Arthropod
19
21
  begin
20
22
  if response.state == "close"
21
23
  return response
24
+ elsif response.state == "error"
25
+ raise Arthropod::Client::ServerError
22
26
  else
23
27
  yield response if block_given?
24
28
  end
@@ -23,10 +23,14 @@ module Arthropod
23
23
  send_message({ state: "open", body: body })
24
24
  end
25
25
 
26
- def close(body = nil)
26
+ def close!(body = nil)
27
27
  send_message({ state: "close", body: body })
28
28
  end
29
29
 
30
+ def error!(body = nil)
31
+ send_message({ state: "error", body: body })
32
+ end
33
+
30
34
  private
31
35
 
32
36
  def send_message(message_body)
@@ -10,7 +10,10 @@ module Arthropod
10
10
  response.messages.each do |message|
11
11
  request = Arthropod::Request.new(client: client, message: message)
12
12
  begin
13
- request.close(yield request)
13
+ request.close!(yield request)
14
+ rescue => error
15
+ request.error!
16
+ raise error
14
17
  ensure
15
18
  client.delete_message(queue_url: sender_queue.queue_url, receipt_handle: message.receipt_handle)
16
19
  end
@@ -1,3 +1,3 @@
1
1
  module Arthropod
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -41,6 +41,13 @@ RSpec.describe(Arthropod::Client) do
41
41
  receive(:send_message)
42
42
  .with({ queue_url: sender_queue_url, message_body: JSON.dump({ return_queue_url: return_queue_url, body: body }) })
43
43
  )
44
+ expect(client).to(
45
+ receive(:delete_queue)
46
+ .with({ queue_url: return_queue_url })
47
+ )
48
+ end
49
+
50
+ it "works" do
44
51
  expect(client).to(
45
52
  receive(:receive_message)
46
53
  .with({ queue_url: return_queue_url, max_number_of_messages: 1, wait_time_seconds: 1 })
@@ -51,17 +58,21 @@ RSpec.describe(Arthropod::Client) do
51
58
  .with({ queue_url: return_queue_url, max_number_of_messages: 1, wait_time_seconds: 1 })
52
59
  .and_return(OpenStruct.new(messages: [ OpenStruct.new({ "body" => JSON.dump({ "state" => "close", "body" => "payload" }), receipt_handle: "receipt_handle" }) ]))
53
60
  )
54
- expect(client).to(
55
- receive(:delete_queue)
56
- .with({ queue_url: return_queue_url })
57
- )
58
- end
59
-
60
- it "works" do
61
61
  response = Arthropod::Client.push(client: client, queue_name: "test", body: body) do |response|
62
62
  expect(response.body).to eq("update")
63
63
  end
64
64
  expect(response.body).to eq("payload")
65
65
  end
66
+
67
+ it "should raise on error" do
68
+ expect(client).to(
69
+ receive(:receive_message)
70
+ .with({ queue_url: return_queue_url, max_number_of_messages: 1, wait_time_seconds: 1 })
71
+ .and_return(OpenStruct.new(messages: [ OpenStruct.new({ "body" => JSON.dump({ "state" => "error" }), receipt_handle: "receipt_handle" }) ]))
72
+ )
73
+ expect do
74
+ Arthropod::Client.push(client: client, queue_name: "test", body: body)
75
+ end.to raise_error(Arthropod::Client::ServerError)
76
+ end
66
77
  end
67
78
  end
@@ -29,6 +29,9 @@ RSpec.describe(Arthropod::Server) do
29
29
  .with({ queue_url: sender_queue_url, max_number_of_messages: 1, wait_time_seconds: 1 })
30
30
  .and_return(OpenStruct.new(messages: [ OpenStruct.new({ "body" => JSON.dump({ "body" => "request", "return_queue_url" => return_queue_url }), receipt_handle: "receipt_handle" }) ]))
31
31
  )
32
+ end
33
+
34
+ it "works" do
32
35
  expect(client).to(
33
36
  receive(:send_message)
34
37
  .with({ queue_url: return_queue_url, message_body: JSON.dump({ state: "open", body: "response" }) })
@@ -37,9 +40,7 @@ RSpec.describe(Arthropod::Server) do
37
40
  receive(:send_message)
38
41
  .with({ queue_url: return_queue_url, message_body: JSON.dump({ state: "close", body: "final_response" }) })
39
42
  )
40
- end
41
43
 
42
- it "works" do
43
44
  Arthropod::Server.pull(client: client, queue_name: "test") do |request|
44
45
  expect(request.body).to eq("request")
45
46
  request.respond "response"
@@ -47,5 +48,18 @@ RSpec.describe(Arthropod::Server) do
47
48
  "final_response"
48
49
  end
49
50
  end
51
+
52
+ it "send error on exception" do
53
+ expect(client).to(
54
+ receive(:send_message)
55
+ .with({ queue_url: return_queue_url, message_body: JSON.dump({ state: "error", body: nil }) })
56
+ )
57
+
58
+ expect do
59
+ Arthropod::Server.pull(client: client, queue_name: "test") do |request|
60
+ raise "error"
61
+ end
62
+ end.to raise_error(Exception)
63
+ end
50
64
  end
51
65
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arthropod
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Goya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-07 00:00:00.000000000 Z
11
+ date: 2019-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-sqs