hutch-schedule 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 90c31b8f2f324f07f2e2bc3c8dcd071ce129e2bf
4
+ data.tar.gz: 784b5b1690b8beccbbf76a4cec009c8539f4b15b
5
+ SHA512:
6
+ metadata.gz: 8feccd2adb36ca1864132c5be689162a88ab5a1072d758c4661183f29e103336be13d4001167b6dfbc2002db87eccdd2d3d2d6b8ee6fb86ee5038086b3cb4439
7
+ data.tar.gz: 03bc493d5ebed1b273e34cc59ab758a91e8fd5a6d88ef9b9b7689e6162442c4f140d9825f114d5dcd9aabebd84123d13f7e701b4488e05a35a7a523c2e84783f
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
4
+ Metrics:
5
+ LineLength: 120
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at wppurking@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hutch-schedule.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 wyatt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Hutch::Schedule
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hutch/schedule`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'hutch-schedule'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hutch-schedule
20
+
21
+ ## Usage
22
+
23
+ 为了能够初始化这个项目, 需要在合适的地方设置 Hutch 的 setup_proc 用于初始化 Hutch::Schedule
24
+
25
+ ```ruby
26
+ Hutch::Config.setup_procs << -> {
27
+ Hutch::Schedule.connect(Hutch.broker)
28
+ }
29
+ ```
30
+
31
+ 提供了如下的功能支持:
32
+ 1. 在 RabbitMQ 中自动创建了一个 <hutch>.schedule 的 topic exchange 用于专门转发需要延迟的 Message
33
+ 2. 在 RabbitMQ 中自动创建了一个 <hutch>_schedule_queue 的 queue 带有:
34
+ - dlx 到 <hutch> 的 topic exchange
35
+ - 以及避免任务一直挤压的 30 天的 ttl
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hutch-schedule. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
46
+
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
51
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hutch/schedule/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hutch-schedule"
8
+ spec.version = Hutch::Schedule::VERSION
9
+ spec.authors = ["wyatt pan"]
10
+ spec.email = ["wppurking@gmail.com"]
11
+
12
+ spec.summary = %q{Add Schedule and Error Retry To Hutch.}
13
+ spec.description = %q{Add Schedule and Error Retry To Hutch.}
14
+ spec.homepage = "https://github.com/wppurking/hutch-schedule"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_runtime_dependency 'hutch', '~> 0.24'
25
+
26
+ spec.add_development_dependency "activejob"
27
+ spec.add_development_dependency "bundler", "~> 1.14"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "timecop", "~> 0.8"
31
+ end
@@ -0,0 +1,59 @@
1
+ require "hutch/schedule"
2
+
3
+ module ActiveJob
4
+ module QueueAdapters
5
+ # == Hutch adapter for Active Job
6
+ #
7
+ # 简单高效的消息服务方案, Hutch 以多线程的方式处理 RabbitMQ 中的不同 Queue 的 Message.
8
+ #
9
+ # Read more about Hutch {here}[https://github.com/gocardless/hutch].
10
+ #
11
+ # Rails.application.config.active_job.queue_adapter = :hutch
12
+ class HutchAdapter
13
+ # 需要将 activejob 使用的队列全部 routing 到一个 rabbitmq 的 queue 中
14
+ AJ_ROUTING_KEY = "active_job"
15
+
16
+ def initialize
17
+ @monitor = Monitor.new
18
+ end
19
+
20
+ # 不适用 Consumer 的 enqueue, 无需每次重复计算
21
+ def enqueue(job) #:nodoc:
22
+ @monitor.synchronize do
23
+ # 将整个 job 的 data 变为 hash 发布出去
24
+ Hutch.publish(routing_key(job), job.serialize)
25
+ end
26
+ end
27
+
28
+ def enqueue_at(job, timestamp) #:nodoc:
29
+ interval = [(timestamp - Time.now.utc.to_i), 1.second].max
30
+ enqueue_in(interval, job.serialize, routing_key(job))
31
+ end
32
+
33
+ # 不适用 Consumer 的 enqueue, 无需每次重复计算
34
+ def enqueue_in(interval, message, routing_key)
35
+ @monitor.synchronize do
36
+ # 必须是 integer
37
+ props = { expiration: interval.in_milliseconds.to_i }
38
+ Hutch::Schedule.publish(routing_key, message, props)
39
+ end
40
+ end
41
+
42
+ # 计算 routing_key
43
+ def routing_key(job)
44
+ "#{AJ_ROUTING_KEY}.#{job.queue_name}"
45
+ end
46
+
47
+ class JobWrapper #:nodoc:
48
+ include Hutch::Consumer
49
+ # 给 ActiveJob 使用的一个大队列
50
+ consume "#{HutchAdapter::AJ_ROUTING_KEY}.#"
51
+
52
+ def process(job_data)
53
+ # 执行 ActiveJob 的 Base, 将整个 job 的 data 传过去, 让其进行反序列化, 组织成参数等等
54
+ Base.execute job_data
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,40 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/core_ext/numeric/time'
3
+ require 'hutch/schedule'
4
+
5
+ module Hutch
6
+ # 如果需要增加让 Consumer Enqueue 的动作, 那么则 include 这个 Module
7
+ module Enqueue
8
+ extend ActiveSupport::Concern
9
+
10
+ # Add Consumer methods
11
+ class_methods do
12
+ # 正常的发布 consumer 对应 routing key 的消息
13
+ def enqueue(message)
14
+ Hutch.publish(enqueue_routing_key, message)
15
+ end
16
+
17
+ # publish message at a delay times
18
+ # interval: 推迟的时间
19
+ # message: 具体的消息
20
+ def enqueue_in(interval, message)
21
+ props = { expiration: interval.in_milliseconds.to_i }
22
+ Hutch::Schedule.publish(enqueue_routing_key, message, props)
23
+ end
24
+
25
+ # 延期在某一个时间点执行
26
+ def enqueue_at(time, message)
27
+ # 如果 time 比当前时间还早, 那么就延迟 1s 钟执行
28
+ interval = [(time.utc - Time.now.utc), 1.second].max
29
+ enqueue_in(interval, message)
30
+ end
31
+
32
+ # routing_key: 目的为将 Message 发送给 RabbitMQ 那么使用其监听的任何一个 routing_key 都可以发送
33
+ def enqueue_routing_key
34
+ raise "Routing Keys is not set!" if routing_keys.size < 1
35
+ routing_keys.to_a.last
36
+ end
37
+ end
38
+ end
39
+ end
40
+
@@ -0,0 +1,72 @@
1
+ require 'active_support/dependencies/autoload'
2
+ require 'active_support/core_ext/object/blank'
3
+ require 'active_support/core_ext/numeric'
4
+ require 'active_support/core_ext/module/delegation'
5
+
6
+ # 共享 Hutch::Broker 实例的所有东西
7
+ module Hutch
8
+ module Schedule
9
+ class Core
10
+
11
+ attr_reader :broker, :exchange
12
+
13
+ delegate :channel, :connection, :logger, to: :broker
14
+
15
+ # 初始化 schedule
16
+ def initialize(broker)
17
+ raise "Broker can`t be nil" if broker.blank?
18
+ @broker = broker
19
+ end
20
+
21
+ # 获取 Hutch 上的 Config
22
+ def config
23
+ broker.instance_variable_get(:@config)
24
+ end
25
+
26
+ # Core 的连接, 注意连接是有顺序的, 必须先将 exchange 初始化好
27
+ def connect!
28
+ declare_exchange!
29
+ declare_publisher!
30
+ setup_queue!
31
+ end
32
+
33
+ def declare_publisher!
34
+ @publisher = Hutch::Publisher.new(connection, channel, exchange, config)
35
+ end
36
+
37
+ # 申明 schedule 使用的 ex
38
+ def declare_exchange!
39
+ @exchange = declare_exchange
40
+ end
41
+
42
+ def declare_exchange(ch = channel)
43
+ exchange_name = "#{config[:mq_exchange]}.schedule"
44
+ # TODO: 检查 mq_exchange_options 中的信息, 确保不会覆盖 x-dead-letter-exchange 的参数
45
+ exchange_options = {
46
+ durable: true,
47
+ 'x-dead-letter-exchange': config[:mq_exchange] }.merge(config[:mq_exchange_options])
48
+ logger.info "using topic exchange(schedule) '#{exchange_name}'"
49
+
50
+ broker.send(:with_bunny_precondition_handler, 'schedule exchange') do
51
+ ch.topic(exchange_name, exchange_options)
52
+ end
53
+ end
54
+
55
+ # 申明 schedule 使用的 queue
56
+ def setup_queue!
57
+ props = { 'x-message-ttl': 30.days.in_milliseconds, 'x-dead-letter-exchange': config[:mq_exchange] }
58
+ queue = broker.queue("#{config[:mq_exchange]}_schedule_queue", props)
59
+
60
+ # TODO: 可以考虑将这个抽取成为参数
61
+ # routing all to this queue
62
+ queue.unbind(exchange, routing_key: '#')
63
+ queue.bind(exchange, routing_key: '#')
64
+ end
65
+
66
+ # Schedule broker 自己的 publish 方法
67
+ def publish(*args)
68
+ @publisher.publish(*args)
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,5 @@
1
+ module Hutch
2
+ module Schedule
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+ require 'active_support/core_ext/object/blank'
3
+ require 'hutch'
4
+ require 'hutch/enqueue'
5
+ require 'hutch/schedule/core'
6
+
7
+ # 引入了 active_job 则引入 adapter
8
+ if defined?(ActiveJob)
9
+ require 'active_job/queue_adapters/hutch_adapter'
10
+ end
11
+
12
+ # gem 的核心入口文件
13
+ module Hutch
14
+ # 在 Hutch 下的独立的 Schedule module, 负责与 schedule 相关的 publish
15
+ module Schedule
16
+
17
+ def self.connect(broker)
18
+ return if core.present?
19
+ @core = Hutch::Schedule::Core.new(broker)
20
+ @core.connect!
21
+ end
22
+
23
+ def self.core
24
+ @core
25
+ end
26
+
27
+ def self.publish(*args)
28
+ core.publish(*args)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,2 @@
1
+ # requrie entry point
2
+ require 'hutch/schedule'
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hutch-schedule
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - wyatt pan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hutch
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.24'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.24'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activejob
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.8'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.8'
97
+ description: Add Schedule and Error Retry To Hutch.
98
+ email:
99
+ - wppurking@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".rubocop.yml"
107
+ - ".travis.yml"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - hutch-schedule.gemspec
114
+ - lib/active_job/queue_adapters/hutch_adapter.rb
115
+ - lib/hutch-schedule.rb
116
+ - lib/hutch/enqueue.rb
117
+ - lib/hutch/schedule.rb
118
+ - lib/hutch/schedule/core.rb
119
+ - lib/hutch/schedule/version.rb
120
+ homepage: https://github.com/wppurking/hutch-schedule
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.6.11
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Add Schedule and Error Retry To Hutch.
144
+ test_files: []