activehook 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +10 -0
- data/activehook.gemspec +27 -0
- data/bin/activehook +7 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/activehook.rb +9 -0
- data/lib/activehook/cli.rb +32 -0
- data/lib/activehook/config.rb +48 -0
- data/lib/activehook/errors.rb +8 -0
- data/lib/activehook/hook.rb +56 -0
- data/lib/activehook/log.rb +29 -0
- data/lib/activehook/post.rb +71 -0
- data/lib/activehook/redis.rb +19 -0
- data/lib/activehook/server.rb +19 -0
- data/lib/activehook/version.rb +3 -0
- data/lib/activehook/workers/base.rb +13 -0
- data/lib/activehook/workers/manager.rb +66 -0
- data/lib/activehook/workers/queue.rb +49 -0
- data/lib/activehook/workers/retry.rb +46 -0
- metadata +168 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ec5f569ad2e4d49a1726c2d19a90ce2b7648af1f
|
4
|
+
data.tar.gz: b2de4d8bc94e5ae5d52a1e377d6722196a8c7916
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2148e1300b59027bb0c169308a51e5cdc756f3f4f61a1478794a461fe49dec8b0459df1b8ea6950b3ce45713dbc127424ad4b300bb5b37ea968d697577bd6495
|
7
|
+
data.tar.gz: 9eed68184e55a437fcece72ead80cd5a2b178d02afbd280f1ce4fdb48d935749fe2be7524e35300860a9c610a4c4ad376bdf4c8a6eb98aca3411103ddff0bb3a
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at nsweeting@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Nicholas Sweeting
|
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,72 @@
|
|
1
|
+
# ActiveHook
|
2
|
+
---
|
3
|
+
|
4
|
+
Fast and simple webhook microservice for Ruby. **Please consider it under development at the moment.**
|
5
|
+
|
6
|
+
ActiveHook provides a scalable solution to your applications webhook sending needs. Its Redis-backed, with support for forking and threading - letting it send an enormous amount of webhooks in short order. Basically a much more focused version of a job processor such as Sidekiq, DelayedJob, Resque, etc.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'activehook'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install activehook
|
23
|
+
|
24
|
+
## Getting Started
|
25
|
+
|
26
|
+
Before starting, ensure you have a functioning Redis server available. ActiveHook runs as a seperate server beyond Rails, Sinatra, etc. To start the server simply type the following in your console.
|
27
|
+
|
28
|
+
$ bundle exec activehook -c config/initializers/activehook.rb
|
29
|
+
|
30
|
+
By providing a path to a configuration file, we can setup ActiveHook with plain old ruby. Below is a list of currently available options:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
ActiveHook.configure do |config|
|
34
|
+
#Your redis server url
|
35
|
+
config.redis_url = ENV['REDIS_URL']
|
36
|
+
#The number of redis connections to provide
|
37
|
+
config.redis_pool = 5
|
38
|
+
#The number of forked workers to create for the server
|
39
|
+
config.workers = 2
|
40
|
+
#The number of queue threads to provide for each worker
|
41
|
+
config.queue_threads = 5
|
42
|
+
#The number of retry threads to provide for each worker
|
43
|
+
config.retry_threads = 2
|
44
|
+
#The maximum amount of retries to attempt for failed webhooks
|
45
|
+
config.retry_max = 3
|
46
|
+
#The amount of time between each retry attempt
|
47
|
+
config.retry_time = 3600
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
Queuing a webhook for processing is easy. From within our application, all we have to do is:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
ActiveHook::Hook.new(uri: 'http://example.com/webhook', payload: { msg: 'My first webhook!' })
|
55
|
+
```
|
56
|
+
|
57
|
+
That's it! We provide a valid string URI, as well hash payload. ActiveHooks queue threads will then attempt to send the webhook. If the webhook fails to be delivered, it will be sent to the retry queue. Delivery will be reattempted at the specified intervals, and eventually dropped if all attempts fail.
|
58
|
+
|
59
|
+
## Development
|
60
|
+
|
61
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
62
|
+
|
63
|
+
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).
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nsweeting/activehook. 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.
|
68
|
+
|
69
|
+
|
70
|
+
## License
|
71
|
+
|
72
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/activehook.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'activehook/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activehook"
|
8
|
+
spec.version = ActiveHook::VERSION
|
9
|
+
spec.authors = ["Nicholas Sweeting"]
|
10
|
+
spec.email = ["nsweeting@gmail.com"]
|
11
|
+
spec.summary = "Fast and simple webhook microservice for Ruby."
|
12
|
+
spec.description = "Fast and simple webhook microservice for Ruby."
|
13
|
+
spec.homepage = "https://github.com/nsweeting/activehook"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.executables = %w( activehook )
|
18
|
+
spec.require_paths = %w( lib )
|
19
|
+
|
20
|
+
spec.add_runtime_dependency "redis", "~> 3.3"
|
21
|
+
spec.add_runtime_dependency "connection_pool", "~> 2.2"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
25
|
+
spec.add_development_dependency "byebug", "~> 5.0"
|
26
|
+
spec.add_development_dependency "fakeredis", "~> 0.5"
|
27
|
+
end
|
data/bin/activehook
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "activehook"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/activehook.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'activehook/server'
|
2
|
+
|
3
|
+
module ActiveHook
|
4
|
+
class CLI
|
5
|
+
def initialize(argv)
|
6
|
+
setup_options(argv)
|
7
|
+
end
|
8
|
+
|
9
|
+
def start
|
10
|
+
server = ActiveHook::Server.new
|
11
|
+
server.start
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def setup_options(argv)
|
17
|
+
parser = OptionParser.new do |o|
|
18
|
+
o.banner = 'Usage: bundle exec bin/activehook [options]'
|
19
|
+
|
20
|
+
o.on('-c', '--config PATH', 'Load PATH for config file') do |arg|
|
21
|
+
load(arg)
|
22
|
+
ActiveHook.log.info("Loaded configuration from #{arg}")
|
23
|
+
end
|
24
|
+
|
25
|
+
o.on('-h', '--help', 'Prints this help') do
|
26
|
+
puts o && exit
|
27
|
+
end
|
28
|
+
end
|
29
|
+
parser.parse!(argv)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module ActiveHook
|
2
|
+
class << self
|
3
|
+
def configure
|
4
|
+
reset
|
5
|
+
yield(config)
|
6
|
+
end
|
7
|
+
|
8
|
+
def config
|
9
|
+
@config ||= Config.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def reset
|
13
|
+
@config = nil
|
14
|
+
@connection_pool = nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Config
|
19
|
+
DEFAULTS = {
|
20
|
+
redis_url: ENV['REDIS_URL'],
|
21
|
+
redis_pool: 5,
|
22
|
+
workers: 2,
|
23
|
+
queue_threads: 4,
|
24
|
+
retry_threads: 2,
|
25
|
+
retry_max: 3,
|
26
|
+
retry_time: 3600,
|
27
|
+
}.freeze
|
28
|
+
|
29
|
+
attr_accessor :redis_url, :redis_pool, :retry_max, :retry_time,
|
30
|
+
:workers, :queue_threads, :retry_threads
|
31
|
+
|
32
|
+
def initialize
|
33
|
+
DEFAULTS.each { |key, value| send("#{key}=", value) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def retry_max_time
|
37
|
+
@retry_max_time ||= retry_max * retry_time
|
38
|
+
end
|
39
|
+
|
40
|
+
def worker_options
|
41
|
+
{
|
42
|
+
worker_count: workers,
|
43
|
+
queue_threads: queue_threads,
|
44
|
+
retry_threads: retry_threads
|
45
|
+
}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module ActiveHook
|
4
|
+
class Hook
|
5
|
+
attr_accessor :uri, :payload, :id, :created_at, :retry_at, :fail_at
|
6
|
+
|
7
|
+
def initialize(options = {})
|
8
|
+
options = defaults.merge(options)
|
9
|
+
options.each { |key, value| send("#{key}=", value) }
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform
|
13
|
+
valid?
|
14
|
+
ActiveHook.redis.with do |conn|
|
15
|
+
conn.pipelined do
|
16
|
+
conn.lpush('ah:queue', to_json)
|
17
|
+
conn.incr('ah:total_queued')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def bump_retry
|
23
|
+
@retry_at = Time.now.to_i + ActiveHook.config.retry_time
|
24
|
+
end
|
25
|
+
|
26
|
+
def retry?
|
27
|
+
@fail_at.to_i > Time.now.to_i
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_json
|
31
|
+
{ id: @id,
|
32
|
+
created_at: @created_at,
|
33
|
+
retry_at: @retry_at,
|
34
|
+
fail_at: @fail_at,
|
35
|
+
uri: @uri,
|
36
|
+
payload: @payload }.to_json
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def defaults
|
42
|
+
{ id: SecureRandom.uuid,
|
43
|
+
created_at: Time.now.to_i,
|
44
|
+
retry_at: Time.now.to_i + ActiveHook.config.retry_time,
|
45
|
+
fail_at: Time.now.to_i + ActiveHook.config.retry_max_time }
|
46
|
+
end
|
47
|
+
|
48
|
+
def valid?
|
49
|
+
raise Errors::Hook, 'Payload must be a Hash.' unless @payload.is_a?(Hash)
|
50
|
+
raise Errors::Hook, 'URI is not a valid format.' unless @uri =~ /\A#{URI::regexp}\z/
|
51
|
+
raise Errors::Hook, 'Created at must be an Integer.' unless @created_at.is_a?(Integer)
|
52
|
+
raise Errors::Hook, 'Retry at must be an Integer.' unless @retry_at.is_a?(Integer)
|
53
|
+
raise Errors::Hook, 'Fail at must be an Integer.' unless @fail_at.is_a?(Integer)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module ActiveHook
|
4
|
+
class << self
|
5
|
+
STDOUT.sync = true
|
6
|
+
|
7
|
+
def log
|
8
|
+
@log ||= Log.new
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Log
|
13
|
+
def initialize
|
14
|
+
@log = ::Logger.new(STDOUT)
|
15
|
+
@log.formatter = proc do |_severity, datetime, _progname, msg|
|
16
|
+
"[ #{datetime} ] #{msg}\n"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def info(msg)
|
21
|
+
@log.info("[ \e[32mOK\e[0m ] #{msg}")
|
22
|
+
end
|
23
|
+
|
24
|
+
def err(msg, action: :no_exit)
|
25
|
+
@log.info("[ \e[31mER\e[0m ] #{msg}")
|
26
|
+
exit 1 if action == :exit
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module ActiveHook
|
5
|
+
class POST
|
6
|
+
HEADERS = {
|
7
|
+
"Content-Type" => "application/json",
|
8
|
+
"Accept" => "application/json",
|
9
|
+
"User-Agent" => "ActiveHook/#{ActiveHook::VERSION}"
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
attr_accessor :payload
|
13
|
+
attr_reader :response_time, :uri, :status, :response
|
14
|
+
|
15
|
+
def initialize(options = {})
|
16
|
+
options.each { |key, value| send("#{key}=", value) }
|
17
|
+
end
|
18
|
+
|
19
|
+
def start
|
20
|
+
@status = post_hook
|
21
|
+
log_status
|
22
|
+
end
|
23
|
+
|
24
|
+
def uri=(uri)
|
25
|
+
@uri = URI.parse(uri)
|
26
|
+
end
|
27
|
+
|
28
|
+
def success?
|
29
|
+
status == :success
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def post_hook
|
35
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
36
|
+
measure_response_time do
|
37
|
+
@response = http.post(uri.path, payload.to_json, HEADERS)
|
38
|
+
end
|
39
|
+
response_status(@response)
|
40
|
+
rescue
|
41
|
+
:error
|
42
|
+
end
|
43
|
+
|
44
|
+
def measure_response_time
|
45
|
+
start = Time.now
|
46
|
+
yield
|
47
|
+
finish = Time.now
|
48
|
+
@response_time = "| #{((finish - start) * 1000.0).round(3)} ms"
|
49
|
+
end
|
50
|
+
|
51
|
+
def response_status(response)
|
52
|
+
case response.code.to_i
|
53
|
+
when (200..204)
|
54
|
+
:success
|
55
|
+
when (400..499)
|
56
|
+
:bad_request
|
57
|
+
when (500..599)
|
58
|
+
:server_problems
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def log_status
|
63
|
+
msg = "POST | #{uri} | #{status.upcase} #{response_time}"
|
64
|
+
if status == :success
|
65
|
+
ActiveHook.log.info(msg)
|
66
|
+
else
|
67
|
+
ActiveHook.log.err(msg)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'redis'
|
2
|
+
|
3
|
+
module ActiveHook
|
4
|
+
class << self
|
5
|
+
attr_reader :connection_pool
|
6
|
+
|
7
|
+
def redis
|
8
|
+
@connection_pool ||= ConnectionPool.create
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class ConnectionPool
|
13
|
+
def self.create
|
14
|
+
::ConnectionPool.new(size: ActiveHook.config.redis_pool) do
|
15
|
+
Redis.new(url: ActiveHook.config.redis_url)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'activehook/workers/manager'
|
2
|
+
|
3
|
+
module ActiveHook
|
4
|
+
class Server
|
5
|
+
def initialize
|
6
|
+
at_exit { shutdown }
|
7
|
+
end
|
8
|
+
|
9
|
+
def start
|
10
|
+
@manager = Workers::Manager.new(ActiveHook.config.worker_options)
|
11
|
+
@manager.start
|
12
|
+
sleep
|
13
|
+
end
|
14
|
+
|
15
|
+
def shutdown
|
16
|
+
@manager.shutdown
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'activehook/workers/queue'
|
2
|
+
require 'activehook/workers/retry'
|
3
|
+
|
4
|
+
module ActiveHook
|
5
|
+
module Workers
|
6
|
+
class Manager
|
7
|
+
attr_accessor :worker_count, :queue_threads, :retry_threads
|
8
|
+
|
9
|
+
def initialize(options = {})
|
10
|
+
options.each { |key, value| send("#{key}=", value) }
|
11
|
+
@workers = []
|
12
|
+
@forks = []
|
13
|
+
build_workers
|
14
|
+
end
|
15
|
+
|
16
|
+
def start
|
17
|
+
@workers.each do |worker|
|
18
|
+
ActiveHook.log.info("New worker starting - #{worker.class.name}")
|
19
|
+
@forks << fork { worker.start }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def shutdown
|
24
|
+
@workers.each(&:shutdown)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def build_workers
|
30
|
+
@worker_count.times do
|
31
|
+
@workers << Worker.new(queue_threads: queue_threads,
|
32
|
+
retry_threads: retry_threads)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Worker
|
38
|
+
attr_accessor :queue_threads, :retry_threads
|
39
|
+
attr_reader :workers, :threads
|
40
|
+
|
41
|
+
def initialize(options = {})
|
42
|
+
options.each { |key, value| send("#{key}=", value) }
|
43
|
+
@threads = []
|
44
|
+
@_threads_real = []
|
45
|
+
build_threads
|
46
|
+
end
|
47
|
+
|
48
|
+
def start
|
49
|
+
@threads.each { |thread| @_threads_real << Thread.new { thread.start } }
|
50
|
+
@_threads_real.map(&:join)
|
51
|
+
end
|
52
|
+
|
53
|
+
def shutdown
|
54
|
+
@threads.each(&:shutdown)
|
55
|
+
@_threads_real.each(&:exit)
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def build_threads
|
61
|
+
@queue_threads.times { @threads << Queue.new }
|
62
|
+
@retry_threads.times { @threads << Retry.new }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'activehook/workers/base'
|
2
|
+
require 'activehook/post'
|
3
|
+
|
4
|
+
|
5
|
+
module ActiveHook
|
6
|
+
module Workers
|
7
|
+
class Queue < Base
|
8
|
+
def start
|
9
|
+
until @done
|
10
|
+
json = retrieve_hook
|
11
|
+
HookRunner.new(json) if json
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def retrieve_hook
|
18
|
+
json = ActiveHook.redis.with { |c| c.brpop('ah:queue', @time) }
|
19
|
+
json.last if json
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class HookRunner
|
24
|
+
def initialize(json)
|
25
|
+
@hook = ActiveHook::Hook.new(JSON.parse(json))
|
26
|
+
@post = ActiveHook::POST.new(uri: @hook.uri, payload: @hook.payload)
|
27
|
+
start
|
28
|
+
end
|
29
|
+
|
30
|
+
def start
|
31
|
+
@post.start
|
32
|
+
ActiveHook.redis.with do |conn|
|
33
|
+
@post.success? ? hook_success(conn) : hook_failed(conn)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def hook_success(conn)
|
40
|
+
conn.incr('ah:total_success')
|
41
|
+
end
|
42
|
+
|
43
|
+
def hook_failed(conn)
|
44
|
+
conn.zadd('ah:retry', @hook.retry_at, @hook.to_json) if @hook.retry?
|
45
|
+
conn.incr('ah:total_failed')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'activehook/workers/base'
|
2
|
+
|
3
|
+
module ActiveHook
|
4
|
+
module Workers
|
5
|
+
class Retry < Base
|
6
|
+
def start
|
7
|
+
until @done
|
8
|
+
retries = retrieve_retries
|
9
|
+
update(retries) unless retries.empty?
|
10
|
+
sleep 2
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def retrieve_retries
|
17
|
+
ActiveHook.redis.with do |conn|
|
18
|
+
conn.zrangebyscore('ah:retry', 0, Time.now.to_i)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def update(retries)
|
23
|
+
ActiveHook.redis.with do |conn|
|
24
|
+
conn.pipelined do
|
25
|
+
conn.zrem('ah:retry', retries)
|
26
|
+
conn.incrby('ah:total_retries', retries.count)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
retries.each { |r| RetryRunner.new(r) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class RetryRunner
|
34
|
+
def initialize(json)
|
35
|
+
@json = json
|
36
|
+
@hook = ActiveHook::Hook.new(JSON.parse(@json))
|
37
|
+
start
|
38
|
+
end
|
39
|
+
|
40
|
+
def start
|
41
|
+
@hook.bump_retry
|
42
|
+
@hook.perform
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activehook
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicholas Sweeting
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: redis
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: connection_pool
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.2'
|
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.12'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.12'
|
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: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '5.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '5.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: fakeredis
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.5'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.5'
|
111
|
+
description: Fast and simple webhook microservice for Ruby.
|
112
|
+
email:
|
113
|
+
- nsweeting@gmail.com
|
114
|
+
executables:
|
115
|
+
- activehook
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- ".travis.yml"
|
121
|
+
- CODE_OF_CONDUCT.md
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- activehook.gemspec
|
127
|
+
- bin/activehook
|
128
|
+
- bin/console
|
129
|
+
- bin/setup
|
130
|
+
- lib/activehook.rb
|
131
|
+
- lib/activehook/cli.rb
|
132
|
+
- lib/activehook/config.rb
|
133
|
+
- lib/activehook/errors.rb
|
134
|
+
- lib/activehook/hook.rb
|
135
|
+
- lib/activehook/log.rb
|
136
|
+
- lib/activehook/post.rb
|
137
|
+
- lib/activehook/redis.rb
|
138
|
+
- lib/activehook/server.rb
|
139
|
+
- lib/activehook/version.rb
|
140
|
+
- lib/activehook/workers/base.rb
|
141
|
+
- lib/activehook/workers/manager.rb
|
142
|
+
- lib/activehook/workers/queue.rb
|
143
|
+
- lib/activehook/workers/retry.rb
|
144
|
+
homepage: https://github.com/nsweeting/activehook
|
145
|
+
licenses:
|
146
|
+
- MIT
|
147
|
+
metadata: {}
|
148
|
+
post_install_message:
|
149
|
+
rdoc_options: []
|
150
|
+
require_paths:
|
151
|
+
- lib
|
152
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
153
|
+
requirements:
|
154
|
+
- - ">="
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
158
|
+
requirements:
|
159
|
+
- - ">="
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
version: '0'
|
162
|
+
requirements: []
|
163
|
+
rubyforge_project:
|
164
|
+
rubygems_version: 2.5.0
|
165
|
+
signing_key:
|
166
|
+
specification_version: 4
|
167
|
+
summary: Fast and simple webhook microservice for Ruby.
|
168
|
+
test_files: []
|