chasqui 0.0.1

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: 7dc418405a5b23f20b67dc597c78926ee37f2f97
4
+ data.tar.gz: 8dbef72f6a1fe7546c3fa770d461d931ee396312
5
+ SHA512:
6
+ metadata.gz: 6619038c7980b5bdb191014f3e14a3753c5a0256eeaa844035895fc23422736a3584772c8c77dd6b6eb4437ac5bbad019c79138ce0fdab9e8120f8069f03278d
7
+ data.tar.gz: f6b7da7f471ce28f665f44cfcd82edcf1702bb2de0d29dcebfc83baf4662ccfa5cbbbff5aed883b550b554a0d4702026ec42b7f65e713bfc022ecb2cc376b8fa
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
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chasqui.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jordan Bach
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,123 @@
1
+ # Chasqui
2
+
3
+ Chasqui is a simple, lightweight, persistent implementation of the publish-subscribe (pub/sub)
4
+ messaging pattern for service oriented architectures.
5
+
6
+ Chasqui delivers messages to subscribers in a Resque-compatible format. If you are already
7
+ using Resque and/or Sidekiq, Chasqui will make a wonderful companion to your architecture.
8
+
9
+ ## Why do you need Chasqui?
10
+
11
+ * To reduce coupling between applications
12
+ * To process, monitor, and retry messages idependent of request cycles
13
+
14
+ ## Design
15
+
16
+ Chasqui is designed with reliability in mind.
17
+ Failure is expected and planned for in the design.
18
+
19
+ Chasqui uses Redis to create persistent queues. Chasqui does not use the Redis Pub/Sub feature
20
+ because it cannot ensure delivery of messages, especially when subscribers are not running.
21
+
22
+ Chasqui consists of two components, a client and a server.
23
+ The client provides a simple interface for publishing messages. The client places messages
24
+ on a persistent queue, called an inbox, where they wait for further processing.
25
+ The server reads messages from the inbox and places them on the queues of each subscriber.
26
+ The server will create queues for subscribers if the queues do not exist.
27
+
28
+ The advantage of this design is that messages are not lost when the chasqui server or the
29
+ chasqui subscribers are not running.
30
+
31
+ ## Is Chasqui the best choice for you?
32
+
33
+ Chasqui is perfect for you if your current architecture meets all of the following criteria
34
+ listed below.
35
+
36
+ 1. You have a service oriented architecture of some kind.
37
+ 2. You primarily use resque and/or sidekiq to process jobs already.
38
+ 3. You want a simple Pub/Sub solution with minimal setup and maintenance.
39
+ 4. Your organization or traffic volume is not high enough to warrant a more complex solution.
40
+
41
+ If any of the above are not true for you, you may want to consider other available solutions.
42
+ This website maintains a list of alternatives for you to consider: http://queues.io/
43
+
44
+ If Chasqui is not right for you, then please use another solution. I designed Chasqui to
45
+ solve a specific problem for a particular scale and company size.
46
+
47
+ ## Installation
48
+
49
+ Add this line to your application's Gemfile:
50
+
51
+ ```ruby
52
+ gem 'chasqui'
53
+ ```
54
+
55
+ And then execute:
56
+
57
+ $ bundle
58
+
59
+ Or install it yourself as:
60
+
61
+ $ gem install chasqui
62
+
63
+ ## Publishing events
64
+
65
+ Publishing events is simple.
66
+
67
+ ```rb
68
+ Chasqui.publish 'user.sign-up', user_id
69
+ ```
70
+
71
+ To prevent conflicts with other applications, you can choose a unique namespace for your events.
72
+
73
+ ```rb
74
+ # config/initializers/chasqui.rb
75
+ Chasqui.configure do |config|
76
+ config.namespace = 'com.example.myapp'
77
+ end
78
+ ```
79
+
80
+ Now when you call `Chasqui.publish 'event.name', data, ...`, Chasqui will publish the event
81
+ `com.example.myapp.user.sign-up`.
82
+
83
+ ## Subscribing to events
84
+
85
+ __NOT IMPLMENTED YET__
86
+
87
+ ```rb
88
+ # file: otherapp/app/subscribers/user_events.rb
89
+ Chasqui.subscribe queue: 'unique_queue_name_for_app', namespace: 'com.example.myapp' do
90
+
91
+ on 'user.sign-up' do |user_id|
92
+ user = User.find user_id
93
+ UserMailer.signup(user).deliver
94
+ end
95
+
96
+ on 'user.cancel' do |user_id, reason|
97
+ user = User.find user_id
98
+ AdminMailer.user_cancelled(user, reason).deliver
99
+ user.archive!
100
+ end
101
+
102
+ end
103
+ ```
104
+
105
+ ## Configure Chasqui
106
+
107
+ ```rb
108
+ Chasqui.configure do |config|
109
+ config.namespace = 'com.example.transcoder'
110
+ config.redis = ENV.fetch('REDIS_URL')
111
+ config.workers = :sidekiq # or :resque
112
+ ...
113
+ end
114
+ ```
115
+
116
+ ## Contributing
117
+
118
+ 1. Fork it (https://github.com/[my-github-username]/chasqui/fork)
119
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
120
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
121
+ 4. Push to the branch (`git push origin my-new-feature`)
122
+ 5. Create a new Pull Request
123
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/chasqui.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chasqui/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "chasqui"
8
+ spec.version = Chasqui::VERSION
9
+ spec.authors = ["Jordan Bach"]
10
+ spec.email = ["jordan@opensolitude.com"]
11
+ spec.summary = %q{Chasqui is a simple, lightweight, persistent implementation of the publish-subscribe (pub/sub) messaging pattern for service oriented architectures.}
12
+ spec.homepage = "https://github.com/jbgo/chasqui"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec"
23
+ end
data/examples/full.rb ADDED
@@ -0,0 +1,56 @@
1
+ # file: admin/config/initializers/chasqui.rb
2
+ Chasqui.configure do |config|
3
+ config.namespace = 'com.example.admin'
4
+ config.redis = ENV.fetch('REDIS_URL')
5
+ end
6
+
7
+ # file: admin/app/controllers/video_controller.rb
8
+ class VideosController < ApplicationController
9
+
10
+ def upload
11
+ video = Video.find params[:id]
12
+
13
+ if video.upload params[:file]
14
+ Chasqui.publish 'video.upload', video.id
15
+ redirect_to upload_complete_url,
16
+ else
17
+ redirect_to video_url, alert: "Upload failed."
18
+ end
19
+ end
20
+
21
+ end
22
+
23
+ # file: transcoder/config/initializers/chasqui.rb
24
+ Chasqui.configure do |config|
25
+ config.publish 'com.example.transcoder'
26
+ config.redis ENV.fetch('REDIS_URL')
27
+ config.workers :sidekiq # or :resque
28
+ end
29
+
30
+ # file: transcoder/app/subscribers/video_subscriber.rb
31
+ Chasqui.subscribe queue: 'transcoder.video', namespace: 'com.example.admin' do
32
+ on 'video.upload' do |video_id|
33
+ begin
34
+ Transcorder.transcode video_url(video_id)
35
+ Chasqui.publish 'video.complete', video_id
36
+ rescue => ex
37
+ Chasqui.publish 'video.error', video_id, ex.message
38
+ raise
39
+ end
40
+ end
41
+ end
42
+
43
+ # file: admin/app/subscribers/video_subscriber.rb
44
+ Chasqui.subscribe queue: 'admin.events', namespace: 'com.example.transcoder' do
45
+
46
+ on 'transcoder.video.complete' do |video_id|
47
+ video = Video.find video_id
48
+ VideoMailer.transcode_complete(video).deliver
49
+ end
50
+
51
+ on 'transcoder.video.complete' do |video_id, error|
52
+ video = Video.find video_id
53
+ VideoMailer.transcode_error(video, error).deliver
54
+ end
55
+
56
+ end
@@ -0,0 +1,3 @@
1
+ module Chasqui
2
+ VERSION = "0.0.1"
3
+ end
data/lib/chasqui.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'ostruct'
2
+ require 'json'
3
+ require "chasqui/version"
4
+
5
+ module Chasqui
6
+
7
+ Config = Struct.new :namespace, :redis
8
+
9
+ Defaults = {
10
+ publish_queue: 'chasqui.inbox'
11
+ }.freeze
12
+
13
+ module ClassMethods
14
+ def namespace
15
+ @config.namespace unless @config.nil?
16
+ end
17
+
18
+ def redis
19
+ @config.redis unless @config.nil?
20
+ end
21
+
22
+ def publish_queue
23
+ Defaults[:publish_queue]
24
+ end
25
+
26
+ def configure(&block)
27
+ @config ||= Config.new
28
+ yield @config
29
+ end
30
+
31
+ def publish(event, *args)
32
+ name = namespace ? "#{namespace}.#{event}" : event
33
+ redis.rpush publish_queue, { name: name, data: args }.to_json
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ Chasqui.extend Chasqui::ClassMethods
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chasqui do
4
+ it 'has a version number' do
5
+ expect(Chasqui::VERSION).not_to be nil
6
+ end
7
+
8
+ describe '.configure' do
9
+ before { reset_config }
10
+
11
+ context 'defaults' do
12
+ it { expect(Chasqui.namespace).to be_nil }
13
+ it { expect(Chasqui.publish_queue).to eq('chasqui.inbox') }
14
+ end
15
+
16
+ it 'configures the namespace' do
17
+ Chasqui.configure { |config| config.namespace = 'com.example.test' }
18
+ expect(Chasqui.namespace).to eq('com.example.test')
19
+ end
20
+
21
+ it 'configures redis' do
22
+ redis = FakeRedis.new
23
+ Chasqui.configure { |config| config.redis = redis }
24
+ expect(Chasqui.redis).to eq(redis)
25
+ end
26
+ end
27
+
28
+ describe '.publish' do
29
+ before do
30
+ reset_config
31
+ Chasqui.configure { |config| config.redis = FakeRedis.new }
32
+ end
33
+
34
+ it 'publishes an event without a namespace' do
35
+ Chasqui.publish 'test.event', 1, 2, foo: 'bar'
36
+ event = JSON.load Chasqui.redis.lpop('chasqui.inbox')
37
+ expect(event['name']).to eq('test.event')
38
+ expect(event['data']).to eq([1, 2, {'foo'=>'bar'}])
39
+ end
40
+
41
+ it 'publishes an event with a namespace' do
42
+ Chasqui.configure { |config| config.namespace = 'my.app' }
43
+ p Chasqui.namespace
44
+ Chasqui.publish 'test.event', :foo
45
+ event = JSON.load Chasqui.redis.lpop('chasqui.inbox')
46
+ expect(event['name']).to eq('my.app.test.event')
47
+ expect(event['data']).to eq(['foo'])
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def reset_config
54
+ Chasqui.instance_variable_set(:@config, nil)
55
+ end
56
+ end
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'chasqui'
3
+
4
+ class FakeRedis
5
+ def initialize
6
+ @queues = {}
7
+ end
8
+
9
+ def rpush(key, value)
10
+ queue(key) << value
11
+ end
12
+
13
+ def lpop(key)
14
+ queue(key).shift
15
+ end
16
+
17
+ private
18
+
19
+ def queue(key)
20
+ @queues[key] ||= []
21
+ @queues[key]
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chasqui
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jordan Bach
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - jordan@opensolitude.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - chasqui.gemspec
70
+ - examples/full.rb
71
+ - lib/chasqui.rb
72
+ - lib/chasqui/version.rb
73
+ - spec/chasqui_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: https://github.com/jbgo/chasqui
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Chasqui is a simple, lightweight, persistent implementation of the publish-subscribe
99
+ (pub/sub) messaging pattern for service oriented architectures.
100
+ test_files:
101
+ - spec/chasqui_spec.rb
102
+ - spec/spec_helper.rb