queuewrangler 1.0.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 +7 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/Rakefile +9 -0
- data/lib/queuewrangler.rb +5 -0
- data/lib/queuewrangler/version.rb +3 -0
- data/lib/queuewrangler/wrangler.rb +72 -0
- data/queuewrangler.gemspec +23 -0
- data/spec/queuewrangler_spec.rb +50 -0
- data/spec/spec_helper.rb +8 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 65fc3f9ddd1f707cc75164a43f1b8c800e612ab4
|
4
|
+
data.tar.gz: 471a3bcbe371bb22afcdc6a92d1488ddcd0856f2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 86607eaf3630d851abe707abe70d4c5eb3c5d25b80a63f646c13b1381b255131045a26098c447f514b4f9ee68ed65963e0f14500a2c037ceb8ca2ed721148ac2
|
7
|
+
data.tar.gz: 942d78feba4d11b152a8b98c599056e657fff13f7526e241a3b209a62e9e9ba18f81d59ebe2951e6ae202c2341a4293a1e2622643e9c0f11e34c3b4f8d05d7e6
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Lookout, Inc.
|
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,57 @@
|
|
1
|
+
# Queuewrangler
|
2
|
+
|
3
|
+
A gem to simplify queueing long running actions for processing in a background
|
4
|
+
thread.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'queuewrangler'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install queuewrangler
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
``` ruby
|
22
|
+
require 'queuewrangler'
|
23
|
+
|
24
|
+
module RemoteLogger
|
25
|
+
class Logger
|
26
|
+
include Singleton
|
27
|
+
include QueueWrangler::Wrangler
|
28
|
+
|
29
|
+
def initialize
|
30
|
+
initialize_queuewrangler(true)
|
31
|
+
end
|
32
|
+
|
33
|
+
def log(msg)
|
34
|
+
enqueue(:send_data, msg)
|
35
|
+
end
|
36
|
+
|
37
|
+
def send_data(msg)
|
38
|
+
# perform long running action here
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
Thread.new do
|
44
|
+
RemoteLogger::Logger.instance.run!
|
45
|
+
end
|
46
|
+
|
47
|
+
RemoteLogger::Logger.instance.log('This will get enqueued and then sent to the send_data method')
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
56
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
57
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module QueueWrangler
|
4
|
+
module Wrangler
|
5
|
+
attr_reader :queue
|
6
|
+
|
7
|
+
# Initializes the queue and performs other prepatory work required before
|
8
|
+
# queue processing may begin.
|
9
|
+
#
|
10
|
+
# @param [Boolean] enabled True if the queue should be processed
|
11
|
+
# @param [Logger] logger Where to log or no logging if not specified
|
12
|
+
def initialize_queuewrangler(enabled, logger=nil)
|
13
|
+
@enabled = enabled
|
14
|
+
@queue = Queue.new
|
15
|
+
@should_die = false
|
16
|
+
@logger = logger || Logger.new(nil)
|
17
|
+
end
|
18
|
+
|
19
|
+
def log
|
20
|
+
@logger
|
21
|
+
end
|
22
|
+
|
23
|
+
# Determine whether the runloop is dead or not
|
24
|
+
def dead?
|
25
|
+
@should_die
|
26
|
+
end
|
27
|
+
|
28
|
+
# Instruct the runloop to die and flush our internal queue of events
|
29
|
+
def die!
|
30
|
+
@should_die = true
|
31
|
+
process_queue
|
32
|
+
end
|
33
|
+
|
34
|
+
# Flush the outstanding events in the events queue
|
35
|
+
def process_queue
|
36
|
+
until @queue.empty?
|
37
|
+
method, args = @queue.pop
|
38
|
+
|
39
|
+
unless method.nil?
|
40
|
+
log.info("process_queue - handling #{method} with #{args.inspect}")
|
41
|
+
if @enabled
|
42
|
+
processed = send(method, *args)
|
43
|
+
|
44
|
+
unless processed
|
45
|
+
log.info "Failed to process #{method}"
|
46
|
+
enqueue(method, *args)
|
47
|
+
# Arbitrary sleep to make sure we don't spin when we cannot
|
48
|
+
# process a message infinitly
|
49
|
+
sleep 1
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def run!
|
57
|
+
id = self.class.name.split('::').last
|
58
|
+
log.info "#{id}#runloop - starting the runloop, processing: #{@enabled}"
|
59
|
+
until dead?
|
60
|
+
process_queue
|
61
|
+
# Arbitrary sleep just to make sure we don't spin on
|
62
|
+
# #process_queue when the queue is completely empty
|
63
|
+
sleep 0.5
|
64
|
+
end
|
65
|
+
log.info "#{id}#runloop - ending the runloop"
|
66
|
+
end
|
67
|
+
|
68
|
+
def enqueue(method, *args)
|
69
|
+
@queue << [method, [*args]]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -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 'queuewrangler/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "queuewrangler"
|
8
|
+
spec.version = QueueWrangler::VERSION
|
9
|
+
spec.authors = ["R. Tyler Croy, Shaw Vrana"]
|
10
|
+
spec.email = ["shaw@lookout.com"]
|
11
|
+
spec.description = %q{Easily create long running background actions}
|
12
|
+
spec.summary = %q{Make deffering action easy}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'queuewrangler'
|
3
|
+
|
4
|
+
describe QueueWrangler::Wrangler do
|
5
|
+
class DeferredAction
|
6
|
+
include QueueWrangler::Wrangler
|
7
|
+
def initialize
|
8
|
+
initialize_queuewrangler(true)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'instance methods' do
|
13
|
+
subject(:wrangler) { DeferredAction.new }
|
14
|
+
|
15
|
+
it { should respond_to :run! }
|
16
|
+
it { should respond_to :dead? }
|
17
|
+
|
18
|
+
it { should respond_to :die! }
|
19
|
+
describe '#die!' do
|
20
|
+
it 'should process the queue' do
|
21
|
+
wrangler.should_receive(:process_queue)
|
22
|
+
wrangler.die!
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it { should respond_to :process_queue }
|
27
|
+
it { should respond_to :enqueue}
|
28
|
+
|
29
|
+
describe '#enqueue' do
|
30
|
+
let(:uuid) { rand }
|
31
|
+
let(:event) { 'rspec_wrangler' }
|
32
|
+
|
33
|
+
it 'should enqueue a proper message' do
|
34
|
+
wrangler.enqueue(:foo, uuid, event, :extra_info => 'bogus')
|
35
|
+
expect(wrangler.queue.size).to eql(1)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#process_queue' do
|
40
|
+
let(:uuid) { rand }
|
41
|
+
let(:event) { 'rspec_wrangler' }
|
42
|
+
|
43
|
+
it 'should call class method with correct parameters' do
|
44
|
+
wrangler.enqueue(:foo, uuid, event, :extra_info => 'bogus')
|
45
|
+
wrangler.should_receive(:foo).with(uuid, event, :extra_info => 'bogus').and_return(true)
|
46
|
+
wrangler.process_queue
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: queuewrangler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- R. Tyler Croy, Shaw Vrana
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2014-02-06 00:00:00 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: "1.3"
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: *id001
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: rake
|
26
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- &id003
|
29
|
+
- ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
prerelease: false
|
34
|
+
version_requirements: *id002
|
35
|
+
description: Easily create long running background actions
|
36
|
+
email:
|
37
|
+
- shaw@lookout.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- Gemfile
|
46
|
+
- LICENSE.txt
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- lib/queuewrangler.rb
|
50
|
+
- lib/queuewrangler/version.rb
|
51
|
+
- lib/queuewrangler/wrangler.rb
|
52
|
+
- queuewrangler.gemspec
|
53
|
+
- spec/queuewrangler_spec.rb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
homepage: ""
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
metadata: {}
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- *id003
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- *id003
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 2.2.1
|
75
|
+
signing_key:
|
76
|
+
specification_version: 4
|
77
|
+
summary: Make deffering action easy
|
78
|
+
test_files:
|
79
|
+
- spec/queuewrangler_spec.rb
|
80
|
+
- spec/spec_helper.rb
|