beantool 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 75ca861d4936deea02255461f8119d961affa9c3
4
+ data.tar.gz: b1f994f111ba2f9a6fa5c1f09d83eba842c4c4bc
5
+ SHA512:
6
+ metadata.gz: 6c2ec98459c4bc053823ff658417ef4cbcb9a722b62ad8fa706f442425ef7d57f6cc346e7d7eabca39dafabbe36c438d5777813760ca01f115a49a0747c384e7
7
+ data.tar.gz: 9ac90d2cb2b74e3d974ac4bb6127bf4a05e2b5de73875672799dc16a055f3563e7230ffc2df4d9fa14dbe61182fdf961268539ad2d9e1ae59ce605e84e30eb33
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (C) 2014 Justin Langhorst
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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.
@@ -0,0 +1,7 @@
1
+ Beantool
2
+ ========
3
+
4
+ Beantool is a command line utility for monitoring and administering a Beanstalkd pool.
5
+
6
+ Refer to `beantool -h` for usage information.
7
+
@@ -0,0 +1,176 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ostruct'
4
+ require 'optparse'
5
+ require File.expand_path(File.join('..', 'lib', 'beantool'), File.dirname(__FILE__))
6
+
7
+ options = OpenStruct.new
8
+ beanfile = File.join(ENV['HOME'], '.beantool')
9
+ options.hosts = File.exist?(beanfile) ? IO.readlines(beanfile).map{ |l| l.chomp } : []
10
+
11
+ OptionParser.new do |opts|
12
+ opts.program_name = Beantool::NAME
13
+ opts.banner = "Usage: #{opts.program_name} [options]"
14
+
15
+ opts.separator("")
16
+ opts.separator("General Options:")
17
+ opts.separator("")
18
+
19
+ opts.on("-a", "--address [ADDRESS]",
20
+ "Beanstalkd address",
21
+ " Specify multiple addresses with",
22
+ " additional arguments. Permanently",
23
+ " set this argument by creating a",
24
+ " '.beantool' file in your home",
25
+ " directory, each line representing",
26
+ " a Beanstalkd address.") do |host|
27
+ options.hosts << host
28
+ end
29
+
30
+ opts.separator("")
31
+ opts.separator("Monitoring Options:")
32
+ opts.separator("")
33
+
34
+ opts.on("-l", "--list", "List all tubes") { options.list = true }
35
+
36
+ opts.on("-s", "--stats [TUBE]",
37
+ "Display stats for entire instance",
38
+ " Optionally provide a comma separated",
39
+ " list of tubes for tube stats.") do |tubes|
40
+ tubes = tubes.to_s.split(',')
41
+ options.stats = tubes
42
+ end
43
+
44
+ opts.on("-i", "--inspect-job [ID]",
45
+ "Inspect job") { |id| options.inspect_job = id }
46
+
47
+ opts.on("-p", "--peek-ready [TUBE]",
48
+ "Peek a ready job") { |tube| options.peek_ready = tube }
49
+
50
+ opts.on("-b", "--peek-buried [TUBE]",
51
+ "Peek a buried job") { |tube| options.peek_buried = tube }
52
+
53
+ opts.on("-d", "--peek-delayed [TUBE]",
54
+ "Peek a delayed job") { |tube| options.peek_delayed = tube }
55
+
56
+ opts.separator("")
57
+ opts.separator("Administering Options:")
58
+ opts.separator("")
59
+
60
+ opts.on("-k", "--kick [NUM]",
61
+ "Kick each tube NUM times") { |num| options.kick = num.to_i }
62
+
63
+ opts.on("-c", "--create [JOB]",
64
+ "Create a job",
65
+ " Use in conjunction with '--move-to',",
66
+ " to create a job on a specific tube.",
67
+ " The 'default' tube will be used if a",
68
+ " '-t' argument is not provided.") { |job| options.create = job }
69
+
70
+ opts.on("-m", "--move-from [TUBE]",
71
+ "Move jobs from tube",
72
+ " Use in conjunction with '-t',",
73
+ " '--move-to'") { |tube| options.move_from = tube }
74
+
75
+ opts.on("-t", "--move-to [TUBE]",
76
+ "Move jobs to tube",
77
+ " Use in conjunction with '-m',",
78
+ " '--move-from'. You can also specify",
79
+ " this argument in conjunction with '-e',",
80
+ " '--export', as a means of backup.") { |tube| options.move_to = tube }
81
+
82
+ opts.on("-P", "--purge [TUBE]",
83
+ "Purge jobs from tube") { |tube| options.purge = tube.split(',') }
84
+
85
+ opts.on("-I", "--import [TUBE]",
86
+ "Import tube from '--filename',",
87
+ " A default filename of '[tube].yaml'",
88
+ " is used if an '-f' argument is not",
89
+ " provided.") { |tube| options.import = tube }
90
+
91
+ opts.on("-E", "--export [TUBE]",
92
+ "Export tube to '--filename'",
93
+ " A default filename of '[tube].yaml'",
94
+ " is used if an '-f' argument is not",
95
+ " provided.") { |tube| options.export = tube }
96
+
97
+ opts.on("-f", "--filename [FILENAME]",
98
+ "Filename for import/export") { |file| options.filename = file }
99
+
100
+ opts.on_tail.separator("")
101
+
102
+ opts.on_tail("-h", "--help", "Show this message") do
103
+ puts opts
104
+ exit
105
+ end
106
+
107
+ opts.on_tail("-v", "--version", "Show version") do
108
+ puts Beantool.version
109
+ exit
110
+ end
111
+
112
+ end.parse!
113
+
114
+ options.hosts << 'localhost:11300' if options.hosts.empty?
115
+
116
+ beantool = nil
117
+
118
+ begin
119
+ beantool = Beantool.new(options.hosts)
120
+ rescue Beaneater::NotConnected
121
+ puts "unable to connect to beanstalkd at #{options.hosts.inspect}"
122
+ exit
123
+ end
124
+
125
+ # Handle monitoring options
126
+
127
+ puts beantool.tubes if options.list
128
+
129
+ if options.stats
130
+ if options.stats.empty?
131
+ puts beantool.stats
132
+ else
133
+ options.stats.each_with_index do |tube,index|
134
+ puts beantool.tube_stats(tube)
135
+ puts "\n" unless index == options.stats.size-1
136
+ end
137
+ end
138
+ end
139
+
140
+ puts beantool.inspect_job(options.inspect_job) if options.inspect_job
141
+
142
+ puts beantool.peek(options.peek_ready, :ready) if options.peek_ready
143
+ puts beantool.peek(options.peek_buried, :buried) if options.peek_buried
144
+ puts beantool.peek(options.peek_delayed, :delayed) if options.peek_delayed
145
+
146
+ # Handle adminstering options
147
+
148
+ if options.kick
149
+ beantool.kick(options.kick || 1)
150
+ end
151
+
152
+ if options.export
153
+ beantool.move(options.export, options.move_to) if options.move_to
154
+ beantool.export(options.export, options.filename || options.export+'.yaml')
155
+ end
156
+
157
+ if options.import
158
+ begin
159
+ options.filename ||= options.import+'.yaml'
160
+ beantool.import(options.import, options.filename)
161
+ rescue Errno::ENOENT
162
+ puts "#{options.filename} does not exist"
163
+ end
164
+ end
165
+
166
+ if options.move_from && options.move_to
167
+ beantool.move(options.move_from, options.move_to)
168
+ end
169
+
170
+ if options.purge
171
+ beantool.purge(options.purge)
172
+ end
173
+
174
+ if options.create
175
+ beantool.create(options.create, options.move_to || 'default')
176
+ end
@@ -0,0 +1,92 @@
1
+ require 'rubygems'
2
+ require 'beaneater'
3
+
4
+ class Beantool
5
+ NAME = 'beantool'
6
+ VERSION = '0.4.0'
7
+
8
+ def self.version
9
+ [NAME, VERSION].join(' ')
10
+ end
11
+
12
+ attr_reader :beanstalk
13
+
14
+ def initialize(addrs='0.0.0.0:11300')
15
+ @beanstalk = Beaneater::Pool.new(Array(addrs))
16
+ end
17
+
18
+ #
19
+ # Monitoring
20
+ #
21
+
22
+ def tubes
23
+ beanstalk.tubes.all.map{|t| t.name}.join("\n")
24
+ end
25
+
26
+ def stats
27
+ beanstalk.stats.keys.map{|stat| "#{stat}: #{beanstalk.stats.send(stat)}"}.join("\n")
28
+ end
29
+
30
+ def tube_stats(name)
31
+ tube = beanstalk.tubes.find(name)
32
+ tube.stats.keys.map{|stat| "#{stat}: #{tube.stats.send(stat)}"}.join("\n")
33
+ rescue Beaneater::NotFoundError
34
+ "#{name} not found"
35
+ end
36
+
37
+ def inspect_job(id)
38
+ job = beanstalk.jobs.peek(id)
39
+ return "job #{id} not found" if job.nil?
40
+ job.stats.keys.map{|stat| "#{stat}: #{job.stats.send(stat)}"}.concat(["body: #{job.body.inspect}"]).join("\n")
41
+ end
42
+
43
+ def peek(name, state)
44
+ job = beanstalk.tubes.find(name).peek(state.to_sym)
45
+ return "job not found" if job.nil?
46
+ ["id: #{job.id}", "body: #{job.body.inspect}"].join("\n")
47
+ rescue Beaneater::NotFoundError
48
+ "#{name} not found"
49
+ end
50
+
51
+ #
52
+ # Administering
53
+ #
54
+
55
+ def kick(num)
56
+ beanstalk.tubes.all.each{|tube| tube.kick(num)}
57
+ end
58
+
59
+ def import(tubename, filename)
60
+ tube = beanstalk.tubes.find(tubename)
61
+ YAML.load_file(filename).each { |job| tube.put(job) }
62
+ end
63
+
64
+ def export(tubename, filename)
65
+ jobs = Array.new
66
+ tube = beanstalk.tubes.find(tubename)
67
+ while tube.peek(:ready)
68
+ job = tube.reserve
69
+ jobs << job.body
70
+ job.delete
71
+ end
72
+ File.open(filename, 'a') { |f| YAML.dump(jobs, f) }
73
+ end
74
+
75
+ def create(job, tubename)
76
+ beanstalk.tubes.find(tubename).put(job)
77
+ end
78
+
79
+ def move(from, to)
80
+ source = beanstalk.tubes.find(from)
81
+ destination = beanstalk.tubes.find(to)
82
+ while source.peek(:ready)
83
+ job = source.reserve
84
+ destination.put(job.body)
85
+ job.delete
86
+ end
87
+ end
88
+
89
+ def purge(tubenames)
90
+ tubenames.each { |tube| beanstalk.tubes.find(tube).clear }
91
+ end
92
+ end
@@ -0,0 +1,17 @@
1
+ require 'minitest/autorun'
2
+ require File.expand_path(File.join('..', 'lib', 'beantool'), File.dirname(__FILE__))
3
+
4
+ class BeantoolTest < MiniTest::Test
5
+ def setup
6
+ @beantool = Beantool.new(['localhost:11300'])
7
+ end
8
+
9
+ def teardown
10
+ @beantool = nil
11
+ end
12
+
13
+ def test_true
14
+ assert true
15
+ end
16
+ end
17
+
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beantool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Langhorst
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: beaneater
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rdoc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.8'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.8'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.9'
111
+ description: Beantool is a command line utility for monitoring and adminstering a
112
+ Beanstalkd pool.
113
+ email:
114
+ - the@justinlanghorst.com
115
+ executables:
116
+ - beantool
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - LICENSE.md
121
+ - README.md
122
+ - bin/beantool
123
+ - lib/beantool.rb
124
+ - test/beantool_test.rb
125
+ homepage: http://rubygems.org/gems/beantool
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '2.0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.2.2
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: CLI tool for Beanstalkd
149
+ test_files:
150
+ - test/beantool_test.rb