cloudtasker 0.8.2 → 0.9.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: c4c3ce66208b1b45f5ec361f098ac3ceb1db562f2b4af45b3d42ef6ce54b6aac
4
- data.tar.gz: 52ed1b155fea5ce6c307498f058b4b57d115b97e73ac7d0e1ab685c10c62672d
3
+ metadata.gz: a91675f06c63076ac2e197a1279839713eca290c41ad010b26fafb67e0700327
4
+ data.tar.gz: d3826037921062db28923142541f1b89d9e1b2384270d54231d1309f7edc7c5d
5
5
  SHA512:
6
- metadata.gz: a3a4276e4494fc8912480a06cfe81f283340b516ec45cc9a504ffe02c733b42c3dfe3e14e9d132aacf93ff5d4f25534459d85467426497bc46383fbfc4c7fa08
7
- data.tar.gz: ce32054e9e9f6fe496bcd3c36b6e15d012720834f71c237da1cbe82119a6a09d23c4b1463c11044261e890f3e1262fc94ed6dd42e86f72bfb340f79fed6cbbba
6
+ metadata.gz: 55daf715a52f5b1624a17771f0cab64c8241f30dde8abe6189782cb0abb7dc218039994740d66c04592688936febbf0067891cc67262657bf4eb7e6f914878c7
7
+ data.tar.gz: 9db2298e6189825b01646a291cd5ad0c134a533e5dd2039c1c44e68286a3aa0dd725d4f494bf58af2f8b7a0703c151ea4a27ce292ddcc9b8d20238853387092f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # Changelog
2
2
 
3
+ ## [v0.9.0](https://github.com/keypup-io/cloudtasker/tree/v0.8.2) (2020-01-23)
4
+
5
+ [Full Changelog](https://github.com/keypup-io/cloudtasker/compare/v0.8.2...v0.9.0)
6
+
7
+ **Fixed bugs:**
8
+ - Cloud Task: Base64 encode task body to support UTF-8 characters (e.g. emojis).
9
+ - Redis: Restrict to one connection (class level) to avoid too many DNS lookups
10
+
11
+ **Migration**
12
+ For Sinatra applications please update your Cloudtasker controller according to [this diff](https://github.com/keypup-io/cloudtasker/commit/311fa8f9beec91fbae012164a25b2ee6e261a2e4#diff-c2a0ea6c6e6c31c749d2e1acdc574f0f).
13
+
14
+ ## [v0.8.2](https://github.com/keypup-io/cloudtasker/tree/v0.8.2) (2019-12-05)
15
+
16
+ [Full Changelog](https://github.com/keypup-io/cloudtasker/compare/v0.8.1...v0.8.2)
17
+
18
+ **Fixed bugs:**
19
+ - Config: do not add processor host to `Rails.application.config.hosts` if originally empty.
20
+
3
21
  ## [v0.8.1](https://github.com/keypup-io/cloudtasker/tree/v0.8.1) (2019-12-03)
4
22
 
5
23
  [Full Changelog](https://github.com/keypup-io/cloudtasker/compare/v0.8.0...v0.8.1)
@@ -16,9 +16,6 @@ module Cloudtasker
16
16
  # Run a worker from a Cloud Task payload
17
17
  #
18
18
  def run
19
- # Build payload
20
- payload = JSON.parse(request.body.read).merge(job_retries: job_retries)
21
-
22
19
  # Process payload
23
20
  WorkerHandler.execute_from_payload!(payload)
24
21
  head :no_content
@@ -37,6 +34,27 @@ module Cloudtasker
37
34
 
38
35
  private
39
36
 
37
+ #
38
+ # Parse the request body and return the actual job
39
+ # payload.
40
+ #
41
+ # @return [Hash] The job payload
42
+ #
43
+ def payload
44
+ @payload ||= begin
45
+ # Get raw body
46
+ content = request.body.read
47
+
48
+ # Decode content if the body is Base64 encoded
49
+ if request.headers[Cloudtasker::Config::ENCODING_HEADER].to_s.downcase == 'base64'
50
+ content = Base64.decode64(content)
51
+ end
52
+
53
+ # Return content parsed as JSON and add job retries count
54
+ JSON.parse(content).merge(job_retries: job_retries)
55
+ end
56
+ end
57
+
40
58
  #
41
59
  # Extract the number of times this task failed at runtime.
42
60
  #
@@ -82,6 +82,29 @@ module Cloudtasker
82
82
  Google::Protobuf::Timestamp.new.tap { |e| e.seconds = schedule_time.to_i }
83
83
  end
84
84
 
85
+ #
86
+ # Format the job payload sent to Cloud Tasks.
87
+ #
88
+ # @param [Hash] hash The worker payload.
89
+ #
90
+ # @return [Hash] The Cloud Task payloadd.
91
+ #
92
+ def self.format_task_payload(payload)
93
+ payload = JSON.parse(payload.to_json, symbolize_names: true) # deep dup
94
+
95
+ # Format schedule time to Google Protobuf timestamp
96
+ payload[:schedule_time] = format_schedule_time(payload[:schedule_time])
97
+
98
+ # Encode job content to support UTF-8. Google Cloud Task
99
+ # expect content to be ASCII-8BIT compatible (binary)
100
+ payload[:http_request][:headers] ||= {}
101
+ payload[:http_request][:headers][Cloudtasker::Config::CONTENT_TYPE_HEADER] = 'text/json'
102
+ payload[:http_request][:headers][Cloudtasker::Config::ENCODING_HEADER] = 'Base64'
103
+ payload[:http_request][:body] = Base64.encode64(payload[:http_request][:body])
104
+
105
+ payload
106
+ end
107
+
85
108
  #
86
109
  # Find a task by id.
87
110
  #
@@ -104,10 +127,7 @@ module Cloudtasker
104
127
  # @return [Cloudtasker::Backend::GoogleCloudTask, nil] The created task.
105
128
  #
106
129
  def self.create(payload)
107
- # Format payload
108
- payload = payload.merge(
109
- schedule_time: format_schedule_time(payload[:schedule_time])
110
- ).compact
130
+ payload = format_task_payload(payload)
111
131
 
112
132
  # Extract relative queue name
113
133
  relative_queue = payload.delete(:queue)
@@ -12,6 +12,15 @@ module Cloudtasker
12
12
  # Retry header in Cloud Task responses
13
13
  RETRY_HEADER = 'X-CloudTasks-TaskExecutionCount'
14
14
 
15
+ # Content-Transfer-Encoding header in Cloud Task responses
16
+ ENCODING_HEADER = 'Content-Transfer-Encoding'
17
+
18
+ # Content Type
19
+ CONTENT_TYPE_HEADER = 'Content-Type'
20
+
21
+ # Authorization header
22
+ AUTHORIZATION_HEADER = 'Authorization'
23
+
15
24
  # Default values
16
25
  DEFAULT_LOCATION_ID = 'us-east1'
17
26
  DEFAULT_PROCESSOR_PATH = '/cloudtasker/run'
@@ -8,13 +8,17 @@ module Cloudtasker
8
8
  # Suffix added to cache keys when locking them
9
9
  LOCK_KEY_PREFIX = 'cloudtasker/lock'
10
10
 
11
+ def self.client
12
+ @client ||= Redis.new(Cloudtasker.config.redis || {})
13
+ end
14
+
11
15
  #
12
16
  # Return the underlying redis client.
13
17
  #
14
18
  # @return [Redis] The redis client.
15
19
  #
16
20
  def client
17
- @client ||= Redis.new(Cloudtasker.config.redis || {})
21
+ @client ||= self.class.client
18
22
  end
19
23
 
20
24
  #
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cloudtasker
4
- VERSION = '0.8.2'
4
+ VERSION = '0.9.0'
5
5
  end
@@ -42,8 +42,8 @@ module Cloudtasker
42
42
  http_method: 'POST',
43
43
  url: Cloudtasker.config.processor_url,
44
44
  headers: {
45
- 'Content-Type' => 'application/json',
46
- 'Authorization' => "Bearer #{Authenticator.verification_token}"
45
+ Cloudtasker::Config::CONTENT_TYPE_HEADER => 'application/json',
46
+ Cloudtasker::Config::AUTHORIZATION_HEADER => "Bearer #{Authenticator.verification_token}"
47
47
  },
48
48
  body: worker_payload.to_json
49
49
  },
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudtasker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arnaud Lachaume
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-05 00:00:00.000000000 Z
11
+ date: 2020-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport