beanworker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2011-10-20
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,6 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/beanworker.rb
6
+ lib/beanworker/worker.rb
data/README.rdoc ADDED
@@ -0,0 +1,54 @@
1
+ = beanworker
2
+
3
+ * http://github.com/GearHead90/beanworker
4
+
5
+ == DESCRIPTION:
6
+
7
+ It is a worker-side Beanstalk-based job-queueing dispatcher
8
+ This is a pre-release, I will complete tests and docs in couple days
9
+
10
+ == REQUIREMENTS:
11
+
12
+ * http://github.com/GearHead90/beanqueue
13
+ beanstalk-client
14
+
15
+ == USAGE
16
+
17
+ $ beanworker <jobs.rb> <host:port|path/to/file.yml> [-d|--daemonize [-m|--monitor] [-P|--pid <path/to/pid/dir>] ]
18
+
19
+ -d|--daemonize - process will go in background
20
+ -m|--monitor - process will be started with monitor, that will restart it when it crushes
21
+ -P|--pid - path to the directory, where pid-file(s) will be saved, default is current directory
22
+
23
+ == DSL
24
+
25
+ TODO
26
+
27
+ == INSTALL:
28
+
29
+ gem install beanworker
30
+
31
+ == LICENSE:
32
+
33
+ (The MIT License)
34
+
35
+ Copyright (c) 2011 Andrew Shaydurov
36
+
37
+ Permission is hereby granted, free of charge, to any person obtaining
38
+ a copy of this software and associated documentation files (the
39
+ 'Software'), to deal in the Software without restriction, including
40
+ without limitation the rights to use, copy, modify, merge, publish,
41
+ distribute, sublicense, and/or sell copies of the Software, and to
42
+ permit persons to whom the Software is furnished to do so, subject to
43
+ the following conditions:
44
+
45
+ The above copyright notice and this permission notice shall be
46
+ included in all copies or substantial portions of the Software.
47
+
48
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
49
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
50
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
51
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
52
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
53
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
54
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/beanworker'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'beanworker' do
14
+ self.developer 'Andrew Shaydurov', 'gearhead@it-primorye.ru'
15
+ self.rubyforge_name = self.name
16
+ self.extra_deps = [['beanstalk-client','>= 0'], ['beanqueue', '>= 0.1.0']]
17
+ end
18
+
19
+ require 'newgem/tasks'
20
+ Dir['tasks/**/*.rake'].each { |t| load t }
21
+
22
+ # TODO - want other tests/tasks run by default? Add them to the list
23
+ # remove_task :default
24
+ # task :default => [:spec, :features]
@@ -0,0 +1,51 @@
1
+ #coding: utf-8
2
+ require 'timeout'
3
+ require 'pry'
4
+
5
+ module Beanworker
6
+ module Worker
7
+ def perform(num, *tubes)
8
+ num.times do
9
+ Thread.new do
10
+ connection = Beanqueue.connect Beanworker.connection_config
11
+ tubes.each { |tube| connection.watch(tube.gsub('_', '.')) }
12
+ loop do
13
+ get_one_job connection
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ def get_one_job(connection)
20
+ job = connection.reserve
21
+ name, args = job.stats['tube'].gsub('.', '_'), job.ybody
22
+ need_fork = args.delete('__fork__')
23
+ work_job(name, job.ttr, args, need_fork.nil? ? @need_fork : need_fork)
24
+ job.delete
25
+ rescue SystemExit
26
+ raise
27
+ rescue
28
+ job.bury rescue nil
29
+ end
30
+
31
+ def work_job(name, ttr, args, need_fork=false)
32
+ if need_fork
33
+ Process.wait(Process.fork do
34
+ work_with_timeout(name, ttr, args)
35
+ end)
36
+ else
37
+ work_with_timeout(name, ttr, args)
38
+ end
39
+ end
40
+
41
+ def work_with_timeout(name, ttr, args)
42
+ Timeout::timeout(ttr - 1) do
43
+ self.send(name, args)
44
+ end
45
+ end
46
+
47
+ def fork_default(val)
48
+ @need_fork = val
49
+ end
50
+ end
51
+ end
data/lib/beanworker.rb ADDED
@@ -0,0 +1,28 @@
1
+ #coding: utf-8
2
+ require 'daemons'
3
+ require 'beanqueue'
4
+ require File.dirname(__FILE__) + '/beanworker/worker'
5
+
6
+ module Beanworker
7
+ VERSION = '0.0.1'
8
+
9
+ class << self
10
+ attr_accessor :connection_config
11
+
12
+ def run(opts={})
13
+ self.connection_config = opts[:connection_host] || Beanqueue.get_params(opts[:connection_file])
14
+ if opts[:daemonize]
15
+ Daemons.run_proc 'beanworker', ARGV: ['start'], monitor: opts[:monitor], dir: opts[:pid_dir] do
16
+ run_loop(opts)
17
+ end
18
+ else
19
+ run_loop(opts)
20
+ end
21
+ end
22
+
23
+ def run_loop(opts)
24
+ require opts[:jobs_file]
25
+ loop { gets } unless opts[:no_wait]
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,62 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/beanworker'
3
+ require File.dirname(__FILE__) + '/classes/some_worker'
4
+
5
+ class TestBeanworker < Test::Unit::TestCase
6
+
7
+ def setup
8
+ SomeWorker.fork_default nil
9
+ end
10
+
11
+ def test_truth
12
+ Beanworker.run daemonize: false, connection_file: './test/configs/one.yml', jobs_file: './test/classes/some_worker', no_wait: true
13
+ assert_equal 'localhost:11300', Beanworker.connection_config
14
+
15
+ Beanworker.run daemonize: false, connection_host: 'localhost:11300', jobs_file: './test/classes/some_worker', no_wait: true
16
+ assert_equal 'localhost:11300', Beanworker.connection_config
17
+ end
18
+
19
+ def test_fork_default
20
+ assert_nil SomeWorker.instance_eval { @need_fork }, '@need_fork should not be set'
21
+
22
+ SomeWorker.fork_default true
23
+
24
+ assert SomeWorker.instance_eval { @need_fork }, '@need_fork should be set'
25
+ end
26
+
27
+ def test_work_with_timeout
28
+ assert_nothing_raised do
29
+ SomeWorker.work_with_timeout 'do_something_and_sleep', 3, time: 1, num: 1
30
+ end
31
+
32
+ assert_raise Timeout::Error do
33
+ SomeWorker.work_with_timeout 'do_something_and_sleep', 3, time: 4, num: 2
34
+ end
35
+ end
36
+
37
+ def test_work_job
38
+ assert_nothing_raised do
39
+ SomeWorker.work_job 'do_something_and_sleep', 3, { time: 1, num: 3 }
40
+ end
41
+
42
+ assert_nothing_raised do
43
+ SomeWorker.work_job 'do_something_and_sleep', 3, { time: 1, num: 4 }, true
44
+ end
45
+ end
46
+
47
+ def test_get_one_job
48
+ c1 = Beanqueue.connect 'localhost:11300'
49
+ c1.watch 'do.something.and.sleep'
50
+ Beanqueue.connect 'localhost:11300'
51
+ Beanqueue.push 'do.something.and.sleep', time: 1, num: 5
52
+ SomeWorker.get_one_job c1
53
+ end
54
+
55
+ def test_perform
56
+ Beanworker.connection_config = 'localhost:11300'
57
+ SomeWorker.perform 1, 'do.something.and.sleep'
58
+ Beanqueue.connect 'localhost:11300'
59
+ Beanqueue.push 'do.something.and.sleep', time: 1, num: 6
60
+ sleep(3)
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beanworker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Shaydurov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-20 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: beanstalk-client
16
+ requirement: &27436880 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *27436880
25
+ - !ruby/object:Gem::Dependency
26
+ name: beanqueue
27
+ requirement: &27436380 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.1.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *27436380
36
+ - !ruby/object:Gem::Dependency
37
+ name: hoe
38
+ requirement: &27435920 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '2.12'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *27435920
47
+ description: ! 'It is a worker-side Beanstalk-based job-queueing dispatcher
48
+
49
+ This is a pre-release, I will complete tests and docs in couple days'
50
+ email:
51
+ - gearhead@it-primorye.ru
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files:
55
+ - History.txt
56
+ - Manifest.txt
57
+ files:
58
+ - History.txt
59
+ - Manifest.txt
60
+ - README.rdoc
61
+ - Rakefile
62
+ - lib/beanworker.rb
63
+ - lib/beanworker/worker.rb
64
+ - test/test_beanworker.rb
65
+ - .gemtest
66
+ homepage: http://github.com/GearHead90/beanworker
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options:
70
+ - --main
71
+ - README.rdoc
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project: beanworker
88
+ rubygems_version: 1.8.10
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: It is a worker-side Beanstalk-based job-queueing dispatcher This is a pre-release,
92
+ I will complete tests and docs in couple days
93
+ test_files:
94
+ - test/test_beanworker.rb