soda-core 0.0.6 → 0.0.7

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: 32e5b067cc3dfe374112b0eff7e91d3508f993ea58109bcab77f6a257d34513f
4
- data.tar.gz: 12d12051cdeb103f889f4c104d68ed2d55818efe41705d18ae26ebbfa5fd98a4
3
+ metadata.gz: 5890ee17efa45584d1824c9bb5afb701f1787da78afe0d493fa02ecf2d18d734
4
+ data.tar.gz: 75be3aff3d00251c90ab6c36f54638e00cc4f5852035a73ec315358f5084bbdd
5
5
  SHA512:
6
- metadata.gz: 98481b769364a4cb9cafd63ae11394e631a97c5c914c94b6b3de380fab9324d6ccac4c8730f7483bb0b0bd0933de4585aa5dbc1650b8b0bf1e445b1fd56a2677
7
- data.tar.gz: 1d28e5fbf250aa8c76098c2608a51df7b998369d6b9e738c3baaae06ce53ac27f79f18b3cfa16a62faf7ccc8f7c85db57d2b67e30b18f8561eca8e8d7c87a641
6
+ metadata.gz: b46fc65693b7dba0ce1e36da8cd12eef30025b443ac0c028430066d8d2b6e509503c091a5510298530ef04fd567c038ff474a272b77192d4f40d0cb72fef5ac0
7
+ data.tar.gz: 8c64c47b3c7c9452f002ef36182cf0ce55ba81cd8ae68584fdbf0dc578f268913d4b1d4926492d6d91652d0d6cf4772fec8ecd0bd1b153be98c2d4a4ef4040db
data/lib/soda.rb CHANGED
@@ -18,6 +18,7 @@ require "soda/retrier"
18
18
  require "soda/worker"
19
19
 
20
20
  module Soda
21
+ NAME = "Soda"
21
22
  DEFAULTS = {
22
23
  concurrency: 10,
23
24
  }
data/lib/soda/cli.rb CHANGED
@@ -23,10 +23,15 @@ module Soda
23
23
  def run
24
24
  build_options
25
25
 
26
+ logger.info("🥤 %s v%s" % [Soda::NAME, Soda::VERSION])
27
+
26
28
  if rails?
27
29
  if Rails::VERSION::MAJOR >= 5
30
+ require "./config/application.rb"
28
31
  require "./config/environment.rb"
29
32
  require "soda/rails"
33
+ require "soda/extensions/active_job"
34
+
30
35
  logger.info("Loaded Rails v%s application." % ::Rails.version)
31
36
  else
32
37
  raise "Not compatible with Rails v%s!" % Rails.version
@@ -98,9 +103,13 @@ module Soda
98
103
  opts.merge!(require: val)
99
104
  end
100
105
 
101
- o.on("-q", "--queues [PATH]", "Queue to listen to, with optional weights") do |val|
106
+ o.on("-q", "--queue QUEUE[,WEIGHT]", "Queue to listen to, with optional weights") do |val|
102
107
  opts.merge!(queues: opts.fetch(:queues, []).push(val.split(/\,+/)))
103
108
  end
109
+
110
+ o.on("-c", "--concurrency [INT]", "Number of processor threads") do |val|
111
+ opts.merge!(concurrency: Integer(val))
112
+ end
104
113
  end
105
114
  end
106
115
 
data/lib/soda/client.rb CHANGED
@@ -1,11 +1,25 @@
1
1
  module Soda
2
2
  class Client
3
+ DEFAULTS = {
4
+ "retry" => true,
5
+ "delay" => 0,
6
+ }
7
+
8
+ class << self
9
+ def push(*args)
10
+ new.push(*args)
11
+ end
12
+ end
13
+
14
+
3
15
  def push(item)
4
- copy = normalize!(item)
5
- mw = Soda.client_middleware
16
+ copy = normalize!(item)
6
17
 
18
+ mw = Soda.client_middleware
7
19
  mw.use(item["klass"], copy, copy["queue"]) do
8
- Soda.queue(copy["queue"]) do |queue|
20
+ jid = copy["id"]
21
+ jid.tap do
22
+ queue = Soda.queue(copy["queue"])
9
23
  queue.push_in(copy["delay"], Soda.dump_json(copy))
10
24
  end
11
25
  end
@@ -14,19 +28,20 @@ module Soda
14
28
  private
15
29
 
16
30
  def normalize!(item)
17
- item.dup.tap do |copy|
18
- copy.keys.each do |key|
19
- copy.merge!(String(key) => copy[key])
31
+ item = DEFAULTS.merge(item)
32
+ item.tap do
33
+ item.keys.each do |key|
34
+ item.merge!(String(key) => item.delete(key))
20
35
  end
21
36
 
22
37
  id = SecureRandom.base64(10)
23
- klass = copy["klass"].to_s
24
- delay = Integer(copy["delay"]) || 0
25
- queue = copy["queue"] || Soda.default_queue!.name
38
+ klass = item["klass"].to_s
39
+ delay = item["delay"].to_i
40
+ queue = item["queue"] || Soda.default_queue!.name
26
41
 
27
42
  # TODO: add validation
28
43
  #
29
- copy.merge!(
44
+ item.merge!(
30
45
  "id" => id,
31
46
  "klass" => klass,
32
47
  "delay" => delay,
@@ -0,0 +1,27 @@
1
+ module ActiveJob
2
+ module QueueAdapters
3
+ class SodaAdapter
4
+ def enqueue(job)
5
+ enqueue_at(job, Time.now)
6
+ end
7
+
8
+ def enqueue_at(job, ts)
9
+ job.provider_job_id = ::Soda::Client.push(
10
+ "klass" => JobWrapper,
11
+ "wrapped" => job.class,
12
+ "queue" => job.queue_name,
13
+ "delay" => [0, (ts - Time.now).to_i].max,
14
+ "args" => [job.serialize],
15
+ )
16
+ end
17
+
18
+ class JobWrapper
19
+ include ::Soda::Worker
20
+
21
+ def perform(data = {})
22
+ Base.execute(data.merge("provider_job_id" => id))
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
data/lib/soda/fetcher.rb CHANGED
@@ -37,7 +37,7 @@ module Soda
37
37
  start = now
38
38
  logger.debug(%(fetching from "%s") % queue.name)
39
39
 
40
- queue.pop.tap do |msgs|
40
+ (queue.pop || []).tap do |msgs|
41
41
  logger.debug(%(fetched %d message(s) from "%s" (%fms)) % [msgs.count, queue.name, (now - start)])
42
42
  end
43
43
  end
@@ -31,7 +31,7 @@ module Soda
31
31
 
32
32
  def run
33
33
  until stopped?
34
- msgs = fetch
34
+ msgs = fetch || []
35
35
  msgs.each(&method(:process))
36
36
  end
37
37
  rescue Exception => ex
data/lib/soda/rails.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  module Soda
2
2
  module Rails
3
3
  class Engine < ::Rails::Engine
4
- # TODO: define AJ integration
5
4
  end
6
5
  end
7
6
  end
data/lib/soda/retrier.rb CHANGED
@@ -24,7 +24,7 @@ module Soda
24
24
  if ret.is_a?(Numeric)
25
25
  ret < msg.receive_count
26
26
  else
27
- ret
27
+ !!ret
28
28
  end
29
29
  end
30
30
 
data/lib/soda/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Soda
2
- VERSION = "0.0.6".freeze
2
+ VERSION = "0.0.7".freeze
3
3
  end
data/lib/soda/worker.rb CHANGED
@@ -28,6 +28,7 @@ module Soda
28
28
  )
29
29
  end
30
30
  end
31
+ alias_method :perform_at, :perform_in
31
32
 
32
33
  private
33
34
 
@@ -70,6 +71,12 @@ module Soda
70
71
  @options = options
71
72
  end
72
73
 
74
+ %i[id].each do |method|
75
+ define_method(method) do
76
+ options.fetch(String(method))
77
+ end
78
+ end
79
+
73
80
  private
74
81
 
75
82
  attr_reader :options
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soda-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noah Portes Chaikin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-16 00:00:00.000000000 Z
11
+ date: 2020-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-sqs
@@ -63,6 +63,7 @@ files:
63
63
  - lib/soda.rb
64
64
  - lib/soda/cli.rb
65
65
  - lib/soda/client.rb
66
+ - lib/soda/extensions/active_job.rb
66
67
  - lib/soda/fetcher.rb
67
68
  - lib/soda/logger.rb
68
69
  - lib/soda/manager.rb