hawking 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +1 -0
- data/.travis.yml +3 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +28 -0
- data/README.md +75 -0
- data/Rakefile +6 -0
- data/bin/hawking +21 -0
- data/examples/enqueue.rb +4 -0
- data/examples/jobs.rb +3 -0
- data/hawking.gemspec +24 -0
- data/lib/hawking.rb +56 -0
- data/spec/hawking_spec.rb +52 -0
- data/spec/spec_helper.rb +4 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3dc5cdf9adf6ca786efe48dbd040d7b00583f5b9
|
4
|
+
data.tar.gz: 80267e423e17f81fa36649c31bdf1a1d4f37a71c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0252cd0ee608b20e548627af88bb68c135bba7cf2bc77866f52240e9c865a1a005db59273f9254e03bea9314ced2a7cf1e73df00a893915131f561ff2a03ecd5
|
7
|
+
data.tar.gz: 0dbbd239b80976bec8b258e72940ee36588e83a4c94960c9ad59e7c777cf451f274c0b5e4001ea9d09eef963326595b1d05c24847cbc9d2aafddb3fd0598ad81
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --format documentation
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hawking (0.1)
|
5
|
+
json_pure
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.2.5)
|
11
|
+
json_pure (1.8.1)
|
12
|
+
rake (10.1.0)
|
13
|
+
rspec (2.14.1)
|
14
|
+
rspec-core (~> 2.14.0)
|
15
|
+
rspec-expectations (~> 2.14.0)
|
16
|
+
rspec-mocks (~> 2.14.0)
|
17
|
+
rspec-core (2.14.7)
|
18
|
+
rspec-expectations (2.14.4)
|
19
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
20
|
+
rspec-mocks (2.14.4)
|
21
|
+
|
22
|
+
PLATFORMS
|
23
|
+
ruby
|
24
|
+
|
25
|
+
DEPENDENCIES
|
26
|
+
hawking!
|
27
|
+
rake
|
28
|
+
rspec
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
Hawking
|
2
|
+
=======
|
3
|
+
|
4
|
+
[![Build Status](https://travis-ci.org/rogeriozambon/hawking.png?branch=master)](https://travis-ci.org/rogeriozambon/hawking)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/rogeriozambon/hawking.png)](https://codeclimate.com/github/rogeriozambon/hawking)
|
6
|
+
|
7
|
+
Background job queueing using Socket. Inspired in the [Stalkeg gem](https://github.com/han/stalker).
|
8
|
+
|
9
|
+
Queueing jobs
|
10
|
+
-------------
|
11
|
+
|
12
|
+
From anywhere in your app:
|
13
|
+
|
14
|
+
require 'hawking'
|
15
|
+
|
16
|
+
Hawking::Queue.new('email.send', :to => 'joe@example.com')
|
17
|
+
Hawking::Queue.new('post.cleanup', :id => post.id)
|
18
|
+
|
19
|
+
Working jobs
|
20
|
+
------------
|
21
|
+
|
22
|
+
In a standalone file, typically jobs.rb or worker.rb:
|
23
|
+
|
24
|
+
require 'hawking'
|
25
|
+
|
26
|
+
extend Hawking
|
27
|
+
|
28
|
+
job 'email.send' do |args|
|
29
|
+
Pony.send(:to => args['to'], :subject => "Hello there")
|
30
|
+
end
|
31
|
+
|
32
|
+
job 'post.cleanup' do |args|
|
33
|
+
Post.find(args['id']).cleanup
|
34
|
+
end
|
35
|
+
|
36
|
+
Running
|
37
|
+
-------
|
38
|
+
|
39
|
+
Hawking:
|
40
|
+
|
41
|
+
$ sudo gem install hawking
|
42
|
+
|
43
|
+
Now run a worker using the stalk binary:
|
44
|
+
|
45
|
+
$ hawking jobs.rb
|
46
|
+
Working 2 jobs: [ email.send post.cleanup ]
|
47
|
+
Working send.email (email=hello@example.com)
|
48
|
+
|
49
|
+
Hawk will log to stdout as it starts working each job.
|
50
|
+
|
51
|
+
Filter to a list of jobs you wish to run with an argument:
|
52
|
+
|
53
|
+
$ hawking jobs.rb email.send
|
54
|
+
Working 1 jobs: [ email.send ]
|
55
|
+
|
56
|
+
Running the tests
|
57
|
+
-----------------
|
58
|
+
|
59
|
+
$ rake spec
|
60
|
+
|
61
|
+
Maintainer
|
62
|
+
----------
|
63
|
+
|
64
|
+
* Rogério Zambon (http://rogerio.me)
|
65
|
+
|
66
|
+
Licence
|
67
|
+
-------
|
68
|
+
|
69
|
+
(The MIT License)
|
70
|
+
|
71
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
72
|
+
|
73
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
74
|
+
|
75
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/bin/hawking
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
STDERR.sync = STDOUT.sync = true
|
3
|
+
|
4
|
+
require "hawking"
|
5
|
+
extend Hawking
|
6
|
+
|
7
|
+
usage = "hawking <jobs.rb> [<job>[,<job>,..]]"
|
8
|
+
|
9
|
+
file = ARGV.shift or abort usage
|
10
|
+
jobs = ARGV.shift.split(',') rescue nil
|
11
|
+
|
12
|
+
file = "./#{file}" unless file.match(/^[\/.]/)
|
13
|
+
|
14
|
+
require file
|
15
|
+
|
16
|
+
trap("INT") do
|
17
|
+
puts "\rExiting"
|
18
|
+
exit
|
19
|
+
end
|
20
|
+
|
21
|
+
Hawking.run
|
data/examples/enqueue.rb
ADDED
data/examples/jobs.rb
ADDED
data/hawking.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "hawking"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "hawking"
|
6
|
+
s.version = Hawking::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
|
9
|
+
s.authors = ["Rogério Zambon"]
|
10
|
+
s.email = ["rogeriozambon@gmail.com"]
|
11
|
+
s.homepage = "http://github.com/rogeriozambon/hawking"
|
12
|
+
s.summary = "Background job queueing using Socket. Inspired in the Stalkeg gem."
|
13
|
+
s.description = s.summary
|
14
|
+
|
15
|
+
s.executables = ["hawking"]
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency "json_pure"
|
21
|
+
|
22
|
+
s.add_development_dependency "rake"
|
23
|
+
s.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/hawking.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require "socket"
|
2
|
+
require "json"
|
3
|
+
require "timeout"
|
4
|
+
|
5
|
+
module Hawking
|
6
|
+
extend self
|
7
|
+
|
8
|
+
VERSION = "0.1"
|
9
|
+
|
10
|
+
def run
|
11
|
+
puts "Working #{@@jobs.size} jobs: [ #{@@jobs.keys.join(' ')} ]"
|
12
|
+
|
13
|
+
server = TCPServer.open "127.0.0.1", 4481
|
14
|
+
|
15
|
+
loop { work_jobs server }
|
16
|
+
end
|
17
|
+
|
18
|
+
def work_jobs(server)
|
19
|
+
Thread.start(server.accept) do |listener|
|
20
|
+
job = JSON.parse listener.gets, symbolize_names: true
|
21
|
+
|
22
|
+
puts "Working on #{job[:queue]} (#{job[:data]})"
|
23
|
+
|
24
|
+
begin
|
25
|
+
Timeout::timeout(120) do
|
26
|
+
handler = @@jobs[job[:queue]]
|
27
|
+
handler.call(job[:data])
|
28
|
+
end
|
29
|
+
rescue Timeout::Error
|
30
|
+
raise "The job hit 120 seconds timeout"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def job(queue, &block)
|
36
|
+
@@jobs ||= {}
|
37
|
+
@@jobs[queue] = block
|
38
|
+
end
|
39
|
+
|
40
|
+
def jobs
|
41
|
+
@@jobs
|
42
|
+
end
|
43
|
+
|
44
|
+
class Queue
|
45
|
+
def initialize
|
46
|
+
@socket = TCPSocket.open "127.0.0.1", 4481
|
47
|
+
end
|
48
|
+
|
49
|
+
def put(queue, data)
|
50
|
+
info = JSON.generate :queue => queue, :data => data
|
51
|
+
|
52
|
+
@socket.write info
|
53
|
+
@socket.close
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Hawking" do
|
4
|
+
before do
|
5
|
+
$stdout.stub(:write).and_return ""
|
6
|
+
end
|
7
|
+
|
8
|
+
it "storing a job" do
|
9
|
+
Hawking.job "example" do |data|
|
10
|
+
puts "Some example with #{data}"
|
11
|
+
end
|
12
|
+
|
13
|
+
jobs = Hawking.jobs
|
14
|
+
|
15
|
+
jobs.should be_a(Hash)
|
16
|
+
jobs.keys.should include("example")
|
17
|
+
|
18
|
+
jobs["example"].should be_a(Proc)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "queuing data" do
|
22
|
+
server = TCPServer.open "127.0.0.1", 4481
|
23
|
+
|
24
|
+
hawking = Hawking::Queue.new
|
25
|
+
hawking.put "example", :data => "john@example.org"
|
26
|
+
|
27
|
+
data = JSON.parse server.accept.gets, symbolize_names: true
|
28
|
+
|
29
|
+
server.close
|
30
|
+
|
31
|
+
data.should be_a(Hash)
|
32
|
+
data.keys.should include(:queue, :data)
|
33
|
+
|
34
|
+
expect(data[:queue]).to eq "example"
|
35
|
+
expect(data[:data]).to eq data: "john@example.org"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "works" do
|
39
|
+
Hawking.job "example" do |data|
|
40
|
+
puts "Some example with #{data}"
|
41
|
+
end
|
42
|
+
|
43
|
+
server = TCPServer.open "127.0.0.1", 4481
|
44
|
+
|
45
|
+
hawking = Hawking::Queue.new
|
46
|
+
hawking.put "example", :data => "john@example.org"
|
47
|
+
|
48
|
+
Hawking.work_jobs server
|
49
|
+
|
50
|
+
server.close
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hawking
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rogério Zambon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json_pure
|
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
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
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: 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: Background job queueing using Socket. Inspired in the Stalkeg gem.
|
56
|
+
email:
|
57
|
+
- rogeriozambon@gmail.com
|
58
|
+
executables:
|
59
|
+
- hawking
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .rspec
|
64
|
+
- .travis.yml
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/hawking
|
70
|
+
- examples/enqueue.rb
|
71
|
+
- examples/jobs.rb
|
72
|
+
- hawking.gemspec
|
73
|
+
- lib/hawking.rb
|
74
|
+
- spec/hawking_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
homepage: http://github.com/rogeriozambon/hawking
|
77
|
+
licenses: []
|
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.1.10
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Background job queueing using Socket. Inspired in the Stalkeg gem.
|
99
|
+
test_files: []
|