nsque 0.0.2

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: 88f206502dc0517560d3ac2da3034aeb6ff1e600
4
+ data.tar.gz: b038bb128dc307461adfbb4df297d4e16ef874c2
5
+ SHA512:
6
+ metadata.gz: 99edc915abbb8161bfdd389a3e10d22045be3fc9fc7e009fdc0a8021cd7fe7cd85e4c130aaf35efa5d01881b748d1729256e2380751321338e6ec63d72af0e7e
7
+ data.tar.gz: 7a0e348f03b19d4e6f330ebeb94650309540bf341f0c5f7d1fdb124eaf9ba4845bdab6e4a45f0f63d0ad1a13ac6efdde35a51e5f5104d8e075bfdfface4a8c75
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ # Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ source 'https://rubygems.org'
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Mikhail Salosin
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ nsque
2
+ =====
3
+
4
+ Background job library based on NSQ (http://nsq.io)
data/lib/nsque.rb ADDED
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ require 'nsque/version'
3
+ require 'nsque/errors'
4
+ require 'nsque/job'
5
+ require 'nsque/producer'
6
+ require 'nsque/worker'
7
+
8
+ require 'krakow'
9
+ require 'json'
10
+
11
+ module Nsque
12
+ end
@@ -0,0 +1,7 @@
1
+ module Nsque
2
+ class Error < RuntimeError
3
+ end
4
+
5
+ class ChannelRequiredError < Error; end
6
+ class ProducerCantBeNilError < Error; end
7
+ end
data/lib/nsque/job.rb ADDED
@@ -0,0 +1,45 @@
1
+ module Nsque
2
+ class Job
3
+ ##
4
+ # Inherit your worker class from this base class and you will be able to do
5
+ # asynchronous jobs:
6
+ #
7
+ # class SomeJob < Nsque::Job
8
+ #
9
+ # def self.arguments(object)
10
+ # {}
11
+ # # convert given object to arguments that will be passed to perform method
12
+ # end
13
+ #
14
+ # def perform(*args)
15
+ # # some important work
16
+ # end
17
+ # end
18
+ #
19
+ # And you will be able to make async calls like
20
+ #
21
+ # SomeJob.process_async(cool_object)
22
+ #
23
+ # Note that process_async is a class method, perform is an instance method.
24
+ # Also do not forget to implement arguments class method.
25
+ # It takes object and converts it to something (usually Hash or Array) that
26
+ # will be stored in the queue and will be passed to perform method of the job.
27
+
28
+ def self.process_async(object)
29
+ client_push('class' => self.to_s, 'args' => arguments(object))
30
+ end
31
+
32
+ def self.process_in(delay, object)
33
+ at = (Time.now + delay).to_f
34
+ item = { 'class' => self.to_s, 'args' => arguments(object), 'at' => at }
35
+ client_push(item)
36
+ end
37
+
38
+ private
39
+
40
+ def self.client_push(item)
41
+ $producer.write(item)
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,21 @@
1
+ module Nsque
2
+ class Producer
3
+ attr_reader :messages_count
4
+
5
+ def initialize(options = {})
6
+ Krakow::Utils::Logging.level = options.delete(:logging_level) || :warn
7
+ @producer = Krakow::Producer.new(options)
8
+ @messages_count = 0
9
+ end
10
+
11
+ def write(item)
12
+ message = JSON.generate(item)
13
+ @producer.write(message)
14
+ @messages_count += 1
15
+ end
16
+
17
+ def reset_counters
18
+ @messages_count = 0
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,45 @@
1
+ module Nsque
2
+ class TestingWorker
3
+ # use this worker in your test to process delayed jobs
4
+
5
+ def initialize(options)
6
+ raise ChannelRequiredError.new unless options.has_key?(:channel)
7
+ @options = options
8
+ raise ProducerCantBeNilError.new if options[:producer].nil?
9
+ @producer = options[:producer]
10
+ @consumer = Krakow::Consumer.new(@options)
11
+ end
12
+
13
+ def process_all
14
+ count = 0
15
+ while @producer.messages_count > count
16
+ message = @consumer.queue.pop
17
+ hash = JSON.parse(message.message)
18
+ begin
19
+ klass = hash['class'].constantize
20
+ klass.new.perform(hash['args'])
21
+ rescue
22
+ end
23
+ @consumer.confirm(message)
24
+ count += 1
25
+ end
26
+
27
+ @producer.reset_counters
28
+ count
29
+ end
30
+
31
+ def clear_all
32
+ count = 0
33
+
34
+ while !@consumer.queue.empty?
35
+ message = @consumer.queue.pop
36
+ @consumer.confirm(message)
37
+
38
+ count += 1
39
+ end
40
+
41
+ @producer.reset_counters
42
+ count
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Nsque
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,36 @@
1
+ module Nsque
2
+ class Worker
3
+
4
+ def initialize(options)
5
+ Krakow::Utils::Logging.level = options.delete(:logging_level) || :warn
6
+ raise ChannelRequiredError.new unless options.has_key?(:channel)
7
+ @options = options
8
+ end
9
+
10
+ def run
11
+ consumer = Krakow::Consumer.new(@options)
12
+ loop do
13
+ message = consumer.queue.pop
14
+ begin
15
+ hash = JSON.parse(message.message)
16
+ p hash.inspect
17
+ enqueue_after = (hash['at'].to_f - Time.now.to_f) * 1000
18
+ if enqueue_after <= 0
19
+ klass = hash['class'].constantize
20
+ klass.new.perform(hash['args'])
21
+ else
22
+ enqueue_after = [enqueue_after.to_i, 1.hour.to_i * 1000].min #FIXME NSQ max timeout is 1 hour
23
+ p "Requeued: #{enqueue_after} ms"
24
+ consumer.requeue(message, enqueue_after.to_i)
25
+ next
26
+ end
27
+ rescue => e
28
+ p e.message
29
+ end
30
+ consumer.confirm(message)
31
+ end
32
+ ensure
33
+ consumer.terminate if consumer
34
+ end
35
+ end
36
+ end
data/nsque.gemspec ADDED
@@ -0,0 +1,14 @@
1
+ require File.expand_path('../lib/nsque/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'nsque'
5
+ gem.version = Nsque::VERSION
6
+ gem.description = gem.summary = "Background job library based on NSQ (http://nsq.io)"
7
+ gem.authors = ["Mikhail Salosin"]
8
+ gem.email = 'mikhail@salosin.me'
9
+ gem.files = `git ls-files`.split("\n")
10
+ gem.test_files = `git ls-files -- test/*`.split("\n")
11
+ gem.homepage = 'http://rubygems.org/gems/nsque'
12
+ gem.license = 'MIT'
13
+ gem.add_dependency 'krakow'#, github: 'AlphaB/krakow'
14
+ end
data/test/helper.rb ADDED
File without changes
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nsque
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Mikhail Salosin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: krakow
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Background job library based on NSQ (http://nsq.io)
28
+ email: mikhail@salosin.me
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - LICENSE
36
+ - README.md
37
+ - lib/nsque.rb
38
+ - lib/nsque/errors.rb
39
+ - lib/nsque/job.rb
40
+ - lib/nsque/producer.rb
41
+ - lib/nsque/testing_worker.rb
42
+ - lib/nsque/version.rb
43
+ - lib/nsque/worker.rb
44
+ - nsque.gemspec
45
+ - test/helper.rb
46
+ homepage: http://rubygems.org/gems/nsque
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.2.2
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Background job library based on NSQ (http://nsq.io)
70
+ test_files:
71
+ - test/helper.rb