funktor 0.3.0 → 0.4.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: 5dad52e1f8c07a2cf42ec9029920e80cc4152870a0fc863c35ad8015dfe5eac1
4
- data.tar.gz: a22bbf4322188071bb1290f4d189f2fb3c6f80fe2ec6a8fe8c8e150887808007
3
+ metadata.gz: 57488e1ec71535021b02e646cce074552b08bab597ccf0394df4eaee7b9b8480
4
+ data.tar.gz: 2d38386a8da34bc2737da98dad4336b90820b03d85c18b52ca0d09d0d2d85a63
5
5
  SHA512:
6
- metadata.gz: 669ee84b2d8e0261886484e365631eaea64ea7a8bb3c3528fce1edebdfa2bb4da61be55e64346a3a4f3b4f7eb5c4dbaebc0ce622a81ae887dc056effa983caa4
7
- data.tar.gz: d5ef08ff4ca811a24131fc16fb38dffb8f0ddb9ff777d96408bd0ab9c4c6d498a9d430cee8af2feba6cad38090a26f952480ead4e16a7336095fe21e28fedc31
6
+ metadata.gz: 2ea9cc75cc179ff340d35fa48d5dbbaba7604b30b1361efb0a3ed8783de6b929279c39cd237ca1b78863e0622a9eb2a5c8f23f2b707d40159f09bdf788f97b4c
7
+ data.tar.gz: 8b018ebd3e15892fcf4b13feedb77873f41a59d75a62b6ee498808e9848f0e717785e4347d9f294b737025519f527a792b6b64c8c70334b8023e8733205c5aee
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- funktor (0.2.19)
4
+ funktor (0.4.1)
5
5
  activesupport
6
6
  aws-sdk-sqs (~> 1.37)
7
7
  thor
@@ -9,6 +9,9 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
+ activejob (6.1.4)
13
+ activesupport (= 6.1.4)
14
+ globalid (>= 0.3.6)
12
15
  activesupport (6.1.4)
13
16
  concurrent-ruby (~> 1.0, >= 1.0.2)
14
17
  i18n (>= 1.6, < 2)
@@ -18,27 +21,38 @@ GEM
18
21
  addressable (2.7.0)
19
22
  public_suffix (>= 2.0.2, < 5.0)
20
23
  aws-eventstream (1.1.1)
21
- aws-partitions (1.473.0)
22
- aws-sdk-core (3.115.0)
24
+ aws-partitions (1.482.0)
25
+ aws-sdk-core (3.119.0)
23
26
  aws-eventstream (~> 1, >= 1.0.2)
24
27
  aws-partitions (~> 1, >= 1.239.0)
25
28
  aws-sigv4 (~> 1.1)
26
29
  jmespath (~> 1.0)
27
- aws-sdk-sqs (1.40.0)
28
- aws-sdk-core (~> 3, >= 3.112.0)
30
+ aws-sdk-sqs (1.42.0)
31
+ aws-sdk-core (~> 3, >= 3.119.0)
29
32
  aws-sigv4 (~> 1.1)
30
- aws-sigv4 (1.2.3)
33
+ aws-sigv4 (1.2.4)
31
34
  aws-eventstream (~> 1, >= 1.0.2)
35
+ byebug (11.1.3)
36
+ coderay (1.1.3)
32
37
  concurrent-ruby (1.1.9)
33
38
  crack (0.4.5)
34
39
  rexml
35
40
  diff-lcs (1.4.4)
36
41
  docile (1.3.5)
42
+ globalid (0.5.1)
43
+ activesupport (>= 5.0)
37
44
  hashdiff (1.0.1)
38
45
  i18n (1.8.10)
39
46
  concurrent-ruby (~> 1.0)
40
47
  jmespath (1.4.0)
48
+ method_source (1.0.0)
41
49
  minitest (5.14.4)
50
+ pry (0.13.1)
51
+ coderay (~> 1.1)
52
+ method_source (~> 1.0)
53
+ pry-byebug (3.9.0)
54
+ byebug (~> 11.0)
55
+ pry (~> 0.13.0)
42
56
  public_suffix (4.0.6)
43
57
  rake (12.3.3)
44
58
  rexml (3.2.5)
@@ -74,11 +88,13 @@ PLATFORMS
74
88
  ruby
75
89
 
76
90
  DEPENDENCIES
91
+ activejob (>= 5.1.5)
77
92
  funktor!
93
+ pry-byebug
78
94
  rake (~> 12.0)
79
95
  rspec (~> 3.0)
80
96
  simplecov
81
97
  webmock
82
98
 
83
99
  BUNDLED WITH
84
- 2.1.4
100
+ 2.2.24
data/funktor.gemspec CHANGED
@@ -33,6 +33,8 @@ Gem::Specification.new do |spec|
33
33
  spec.add_dependency "activesupport" # TODO - Can we build our own verison of cattr_accessor to avoid this?
34
34
  spec.add_dependency "thor" # Thor drives the CLI
35
35
 
36
+ spec.add_development_dependency 'activejob', '>= 5.1.5'
36
37
  spec.add_development_dependency 'simplecov'
37
38
  spec.add_development_dependency 'webmock'
39
+ spec.add_development_dependency 'pry-byebug'
38
40
  end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_job'
4
+ require "funktor/worker/funktor_options"
5
+
6
+ module ActiveJob
7
+ module QueueAdapters
8
+ # == Funktor adapter for Active Job
9
+ #
10
+ # To use Funktor set the queue_adapter config to +:funktor+.
11
+ #
12
+ # Rails.application.config.active_job.queue_adapter = :funktor
13
+ class FunktorAdapter
14
+ def enqueue(job) # :nodoc:
15
+ job.provider_job_id = Funktor.job_pusher.push({
16
+ "worker" => JobWrapper.to_s,
17
+ "wrapped" => job.class,
18
+ "queue" => job.class.work_queue,
19
+ "delay" => 0,
20
+ "worker_params" => [ job.serialize ]
21
+ })
22
+ end
23
+
24
+ def enqueue_at(job, timestamp) # :nodoc:
25
+ delay = (timestamp.utc - Time.now.utc).round
26
+ if delay < 0
27
+ delay = 0
28
+ end
29
+ job.provider_job_id = Funktor.job_pusher.push({
30
+ "worker" => JobWrapper.to_s,
31
+ "wrapped" => job.class,
32
+ "queue" => job.class.work_queue,
33
+ "delay" => delay,
34
+ "worker_params" => [ job.serialize ]
35
+ })
36
+ end
37
+
38
+ class JobWrapper #:nodoc:
39
+ include Funktor::Worker
40
+
41
+ def perform(job_data)
42
+ Base.execute job_data.first
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ class Base
49
+ include Funktor::Worker::FunktorOptions
50
+ end
51
+ end
data/lib/funktor.rb CHANGED
@@ -3,6 +3,7 @@ require 'funktor/aws/sqs/event'
3
3
  require 'funktor/aws/sqs/record'
4
4
  require 'funktor/counter'
5
5
  require 'funktor/job'
6
+ require 'funktor/job_pusher'
6
7
  require 'funktor/logger'
7
8
  require 'funktor/worker'
8
9
  require 'funktor/middleware_chain'
@@ -22,6 +23,10 @@ module Funktor
22
23
  yield self
23
24
  end
24
25
 
26
+ def self.job_pusher
27
+ @job_pusher ||= JobPusher.new
28
+ end
29
+
25
30
  def self.job_pusher_middleware
26
31
  @job_pusher_chain ||= MiddlewareChain.new
27
32
  yield @job_pusher_chain if block_given?
@@ -98,3 +103,5 @@ end
98
103
  require 'funktor/middleware/metrics'
99
104
  require 'funktor/error_handler'
100
105
  require 'funktor/active_job_handler'
106
+
107
+ require 'funktor/rails' if defined?(::Rails::Engine)
@@ -112,15 +112,15 @@ module Funktor
112
112
  end
113
113
 
114
114
  def all_iam_permissions
115
- Dir.glob(File.join('config', 'iam_permissions', '**.yml'))
115
+ Dir.glob(File.join('funktor_config', 'iam_permissions', '**.yml'))
116
116
  end
117
117
 
118
118
  def all_function_definitions
119
- Dir.glob(File.join('config', 'function_definitions', '**.yml'))
119
+ Dir.glob(File.join('funktor_config', 'function_definitions', '**.yml'))
120
120
  end
121
121
 
122
122
  def all_resources
123
- Dir.glob(File.join('config', 'resources', '**.yml'))
123
+ Dir.glob(File.join('funktor_config', 'resources', '**.yml'))
124
124
  end
125
125
 
126
126
  def funktor_config
@@ -1,3 +1,7 @@
1
+ BUNDLE_WITHOUT: development:test:assets
2
+ BUNDLE_PLUGINS: false
3
+ BUNDLE_FROZEN: true
4
+ SERVERLESS_STAGE: ${self:custom.stage}
1
5
  FUNKTOR_APP_NAME: <%= app_name %>
2
6
  FUNKTOR_INCOMING_JOB_QUEUE:
3
7
  Ref: IncomingJobQueue
@@ -1,7 +1,10 @@
1
+ require "active_support/core_ext/hash/indifferent_access"
2
+
1
3
  module Funktor
2
4
  module FakeJobQueue
3
- def self.push(worker, payload)
4
- jobs[worker.name].push({worker: worker, payload: payload})
5
+ def self.push(payload)
6
+ payload = payload.with_indifferent_access
7
+ jobs[payload["worker"].to_s].push(payload)
5
8
  end
6
9
 
7
10
  def self.jobs
data/lib/funktor/job.rb CHANGED
@@ -38,7 +38,7 @@ module Funktor
38
38
  # TODO - In Funktor Pro we need to override this method (or do something else) so that
39
39
  # we can schedule jobs farther in the future than 15 minutes. We do this here in case a
40
40
  # retry sequence goes too long.
41
- jdelay = job_data["delay"]
41
+ jdelay = job_data["delay"] || 0
42
42
  return jdelay < 900 ? jdelay : 900
43
43
  end
44
44
 
@@ -0,0 +1,31 @@
1
+ require 'aws-sdk-sqs'
2
+
3
+ module Funktor
4
+ class JobPusher
5
+
6
+ def push(payload)
7
+ puts "payload ============"
8
+ pp payload
9
+ job_id = SecureRandom.uuid
10
+ payload[:job_id] = job_id
11
+
12
+ Funktor.job_pusher_middleware.invoke(payload) do
13
+ client.send_message({
14
+ queue_url: queue_url(payload),
15
+ message_body: Funktor.dump_json(payload)
16
+ })
17
+ return job_id
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def client
24
+ @client ||= ::Aws::SQS::Client.new
25
+ end
26
+
27
+ def queue_url(payload)
28
+ payload[:incoming_job_queue_url] || ENV['FUNKTOR_INCOMING_JOB_QUEUE']
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+ module Funktor
3
+ class Rails < ::Rails::Engine
4
+ # This hook happens after `Rails::Application` is inherited within
5
+ # config/application.rb and before config is touched, usually within the
6
+ # class block. Definitely before config/environments/*.rb and
7
+ # config/initializers/*.rb.
8
+ config.before_configuration do
9
+ if defined?(::ActiveJob)
10
+ require 'active_job/queue_adapters/funktor_adapter'
11
+ end
12
+ end
13
+ end if defined?(::Rails)
14
+
15
+ if defined?(::Rails) && ::Rails::VERSION::MAJOR < 5
16
+ raise "🚫 ERROR: Funktor does not support Rails versions under 5.x"
17
+ end
18
+ end
@@ -17,9 +17,9 @@ module Funktor
17
17
 
18
18
  def work_all_jobs
19
19
  while jobs.any?
20
- job_data = jobs.shift
21
- worker = job_data[:worker]
22
- worker_params = job_data[:payload][:worker_params]
20
+ job_data = jobs.shift.with_indifferent_access
21
+ worker = Object.const_get job_data[:worker]
22
+ worker_params = job_data[:worker_params]
23
23
  worker.new.perform(worker_params)
24
24
  end
25
25
  end
@@ -56,14 +56,16 @@ module Funktor
56
56
  end
57
57
 
58
58
  class InlineJobPusherMiddleware
59
- def call(worker, payload)
60
- worker.new.perform(*payload[:worker_params])
59
+ def call(payload)
60
+ payload = payload.with_indifferent_access
61
+ worker = Object.const_get payload["worker"]
62
+ worker.new.perform(*payload["worker_params"])
61
63
  end
62
64
  end
63
65
 
64
66
  class FakeJobPusherMiddleware
65
- def call(worker, payload)
66
- Funktor::FakeJobQueue.push(worker, payload)
67
+ def call(payload)
68
+ Funktor::FakeJobQueue.push(payload)
67
69
  end
68
70
  end
69
71
  end
@@ -1,3 +1,3 @@
1
1
  module Funktor
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.2"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require 'securerandom'
2
- require 'aws-sdk-sqs'
3
2
  require "active_support"
3
+ require 'funktor/worker/funktor_options'
4
4
 
5
5
  module Funktor
6
6
  class DelayTooLongError < StandardError; end
@@ -9,34 +9,10 @@ end
9
9
  module Funktor::Worker
10
10
  def self.included(base)
11
11
  base.extend ClassMethods
12
- base.class_eval do
13
- cattr_accessor :funktor_options_hash
14
- #alias_method :perform_later, :perform_async
15
- end
12
+ base.include(Funktor::Worker::FunktorOptions)
16
13
  end
17
14
 
18
15
  module ClassMethods
19
- def funktor_options(options = {})
20
- self.funktor_options_hash = options
21
- end
22
-
23
- def get_funktor_options
24
- self.funktor_options_hash || {}
25
- end
26
-
27
- def custom_queue_url
28
- get_funktor_options[:queue_url]
29
- end
30
-
31
- def custom_queue
32
- get_funktor_options[:queue]
33
- end
34
-
35
- def queue_url
36
- # TODO : Should this default to FUNKTOR_ACTIVE_JOB_QUEUE?
37
- # Depends how e handle this in pro...?
38
- custom_queue_url || ENV['FUNKTOR_INCOMING_JOB_QUEUE']
39
- end
40
16
 
41
17
  def perform_async(*worker_params)
42
18
  self.perform_in(0, *worker_params)
@@ -54,39 +30,24 @@ module Funktor::Worker
54
30
  if delay > max_delay
55
31
  raise Funktor::DelayTooLongError.new("The delay can't be longer than #{max_delay} seconds. This is a limitation of SQS. Funktor Pro has mechanisms to work around this limitation.")
56
32
  end
57
- self.push_to_incoming_job_queue(delay, *worker_params)
33
+ self.push(delay, *worker_params)
58
34
  end
59
35
 
60
- def push_to_incoming_job_queue(delay, *worker_params)
61
- job_id = SecureRandom.uuid
62
- payload = build_job_payload(job_id, delay, *worker_params)
63
-
64
- Funktor.job_pusher_middleware.invoke(self, payload) do
65
- client.send_message({
66
- queue_url: queue_url,
67
- message_body: Funktor.dump_json(payload)
68
- })
69
- end
36
+ def push(delay, *worker_params)
37
+ payload = build_job_payload(delay, *worker_params)
38
+ Funktor.job_pusher.push(payload)
70
39
  end
71
40
 
72
41
  def max_delay
73
42
  900
74
43
  end
75
44
 
76
- def client
77
- @client ||= Aws::SQS::Client.new
78
- end
79
-
80
- def work_queue
81
- (self.custom_queue || 'default').to_s
82
- end
83
-
84
- def build_job_payload(job_id, delay, *worker_params)
45
+ def build_job_payload(delay, *worker_params)
85
46
  {
86
47
  worker: self.name,
87
48
  worker_params: worker_params,
88
49
  queue: self.work_queue,
89
- job_id: job_id,
50
+ incoming_job_queue_url: self.queue_url,
90
51
  delay: delay,
91
52
  funktor_options: get_funktor_options
92
53
  }
@@ -0,0 +1,37 @@
1
+ module Funktor
2
+ module Worker
3
+ module FunktorOptions
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.class_eval do
7
+ cattr_accessor :funktor_options_hash
8
+ end
9
+ end
10
+ module ClassMethods
11
+ def funktor_options(options = {})
12
+ self.funktor_options_hash = options
13
+ end
14
+
15
+ def get_funktor_options
16
+ self.funktor_options_hash || {}
17
+ end
18
+
19
+ def custom_queue_url
20
+ get_funktor_options[:queue_url]
21
+ end
22
+
23
+ def custom_queue
24
+ get_funktor_options[:queue]
25
+ end
26
+
27
+ def queue_url
28
+ custom_queue_url
29
+ end
30
+
31
+ def work_queue
32
+ (self.custom_queue || 'default').to_s
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: funktor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Green
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-28 00:00:00.000000000 Z
11
+ date: 2021-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-sqs
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activejob
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 5.1.5
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 5.1.5
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: simplecov
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,20 @@ dependencies:
80
94
  - - ">="
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
83
111
  description: Background processing in AWS Lambda.
84
112
  email:
85
113
  - jeremy@octolabs.com
@@ -104,6 +132,7 @@ files:
104
132
  - exe/funktor
105
133
  - exe/funktor-deploy
106
134
  - funktor.gemspec
135
+ - lib/active_job/queue_adapters/funktor_adapter.rb
107
136
  - lib/funktor.rb
108
137
  - lib/funktor/active_job_handler.rb
109
138
  - lib/funktor/aws/sqs/event.rb
@@ -141,12 +170,15 @@ files:
141
170
  - lib/funktor/fake_job_queue.rb
142
171
  - lib/funktor/incoming_job_handler.rb
143
172
  - lib/funktor/job.rb
173
+ - lib/funktor/job_pusher.rb
144
174
  - lib/funktor/logger.rb
145
175
  - lib/funktor/middleware/metrics.rb
146
176
  - lib/funktor/middleware_chain.rb
177
+ - lib/funktor/rails.rb
147
178
  - lib/funktor/testing.rb
148
179
  - lib/funktor/version.rb
149
180
  - lib/funktor/worker.rb
181
+ - lib/funktor/worker/funktor_options.rb
150
182
  homepage: https://github.com/Octo-Labs/funktor
151
183
  licenses:
152
184
  - MIT
@@ -168,8 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
200
  - !ruby/object:Gem::Version
169
201
  version: '0'
170
202
  requirements: []
171
- rubyforge_project:
172
- rubygems_version: 2.7.6.3
203
+ rubygems_version: 3.1.6
173
204
  signing_key:
174
205
  specification_version: 4
175
206
  summary: Background processing in AWS Lambda.