hutch-schedule 0.5.1 → 0.6.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
  SHA1:
3
- metadata.gz: c39d9fb10e3c0a56ec9b631c740a24b377ed7c58
4
- data.tar.gz: c1954bbbe01856982cd21e7e522ebbc30b6c086d
3
+ metadata.gz: c3c90439f10d883825f1ca1b9dbf5bbfcf0aca7c
4
+ data.tar.gz: fb5932ac34591240d31c453ab97fa91ce2db1979
5
5
  SHA512:
6
- metadata.gz: eded10acdb13ed5bfc9b620cc686c6f82c36ac083d0b03b38362cc96e46d91942e45a863992e317a3b609bbd9ec5437ccb43f16186ce2b2f0fc1f9f7ab5b5dc2
7
- data.tar.gz: 66c135271fc10dd710ec7ac4dcc8b220d6de476254f4216c9071f8f85bbd198abf0424b40a374dbec1b7d3bc0961296d7410497fd328c9bac4839f6d19da6d63
6
+ metadata.gz: 042f54b09184cd3e22b28fca4d7b0aa64c8a62e75cb6970b2b3ec65a09af24934ad04b2ad0ce9b9e980757019cea471c0119571c3a63deeab55cb7905fba1e92
7
+ data.tar.gz: 11e92d105f86cefa0ecbade21ccb784dd4648385fb2ccdcd378056d008c51aa4a1382920f044ca11d3a08b59047dba6c954703566e3eda7f8a07a9baf8dcb27a
@@ -11,6 +11,7 @@ module ActiveJob
11
11
  # All activejob Message will routing to one RabbitMQ Queue.
12
12
  # Because Hutch will one Consumer per Queue
13
13
  AJ_ROUTING_KEY = "active_job"
14
+ @@queue_consumers = {}
14
15
 
15
16
  def initialize
16
17
  @monitor = Monitor.new
@@ -18,21 +19,13 @@ module ActiveJob
18
19
 
19
20
  def enqueue(job) #:nodoc:
20
21
  @monitor.synchronize do
21
- # publish all job data to hutch
22
- Hutch.publish(HutchAdapter.routing_key(job), job.serialize)
22
+ @@queue_consumers[job.queue_name].enqueue(job.serialize)
23
23
  end
24
24
  end
25
25
 
26
26
  def enqueue_at(job, timestamp) #:nodoc:
27
- interval = [(timestamp - Time.now.utc.to_i), 1.second].max
28
- enqueue_in(interval, job.serialize, HutchAdapter.routing_key(job))
29
- end
30
-
31
- def enqueue_in(interval, message, routing_key)
32
27
  @monitor.synchronize do
33
- # must be integer
34
- props = { expiration: interval.in_milliseconds.to_i }
35
- Hutch::Schedule.publish(routing_key, message, props)
28
+ @@queue_consumers[job.queue_name].enqueue_at(timestamp, job.serialize)
36
29
  end
37
30
  end
38
31
 
@@ -44,23 +37,23 @@ module ActiveJob
44
37
  # Register all ActiveJob Class to Hutch. (per queue per consumer)
45
38
  def self.register_actice_job_classes
46
39
  # TODO: 需要考虑如何将 AJ 的 Proc queue_name 动态注册到 Hutch
47
- queue_consumers = {}
48
-
49
40
  Dir.glob(Rails.root.join('app/jobs/**/*.rb')).each { |x| require_dependency x }
50
41
  ActiveJob::Base.descendants.each do |job_clazz|
51
42
  # Need activeJob instance #queue_name
52
43
  job = job_clazz.new
53
44
  # Multi queue only have one consumer
54
- next if queue_consumers.key?(job.queue_name)
55
- queue_consumers[job.queue_name] = HutchAdapter.dynamic_consumer(job)
56
- Hutch.register_consumer(queue_consumers[job.queue_name])
45
+ next if @@queue_consumers.key?(job.queue_name)
46
+ @@queue_consumers[job.queue_name] = HutchAdapter.dynamic_consumer(job)
47
+ Hutch.register_consumer(@@queue_consumers[job.queue_name])
57
48
  end
58
49
  end
59
50
 
60
51
  private
61
52
  def self.dynamic_consumer(job_instance)
62
53
  Class.new do
54
+ # don't include Hutch::Consumer, we should change the name of consumer to registe
63
55
  extend Hutch::Consumer::ClassMethods
56
+ include Hutch::Enqueue
64
57
 
65
58
  attr_accessor :broker, :delivery_info
66
59
 
data/lib/hutch/enqueue.rb CHANGED
@@ -30,8 +30,10 @@ module Hutch
30
30
 
31
31
  # delay at exatly time point
32
32
  def enqueue_at(time, message, props = {})
33
+ # compatible with with ActiveJob API
34
+ time_or_timestamp = time.respond_to?(:utc) ? time.utc.to_f : time
33
35
  # if time is early then now then just delay 1 second
34
- interval = [(time.utc - Time.now.utc), 1.second].max
36
+ interval = [(time_or_timestamp - Time.now.utc.to_f), 1.second].max
35
37
  enqueue_in(interval, message, props)
36
38
  end
37
39
 
@@ -24,11 +24,12 @@ module Hutch
24
24
 
25
25
  class << self
26
26
  def connect
27
+ ActiveJob::QueueAdapters::HutchAdapter.register_actice_job_classes if defined?(ActiveJob::QueueAdapters::HutchAdapter)
28
+
27
29
  return if core.present?
28
30
  Hutch.connect unless Hutch.connected?
29
31
  @core = Hutch::Schedule::Core.new(Hutch.broker)
30
32
  @core.connect!
31
- ActiveJob::QueueAdapters::HutchAdapter.register_actice_job_classes if defined?(ActiveJob::QueueAdapters::HutchAdapter)
32
33
  end
33
34
 
34
35
  def core
@@ -1,5 +1,5 @@
1
1
  module Hutch
2
2
  module Schedule
3
- VERSION = "0.5.1"
3
+ VERSION = "0.6.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hutch-schedule
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wyatt pan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-15 00:00:00.000000000 Z
11
+ date: 2018-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hutch