quick_utils 0.0.1
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.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/quick_utils/rails_daemon.rb +63 -0
- data/lib/quick_utils/rake_daemon.rb +19 -0
- data/lib/quick_utils/thread_runner.rb +23 -0
- data/lib/quick_utils/version.rb +3 -0
- data/lib/quick_utils/watcher.rb +32 -0
- data/lib/quick_utils.rb +9 -0
- data/quick_utils.gemspec +26 -0
- metadata +66 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'daemons'
|
3
|
+
require 'optparse'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
module QuickUtils
|
7
|
+
class RailsDaemon
|
8
|
+
|
9
|
+
@@proc_name = ''
|
10
|
+
@@log_file = ''
|
11
|
+
@@rails_root = Dir.pwd
|
12
|
+
|
13
|
+
def self.set_process_name(pname)
|
14
|
+
@@proc_name = pname
|
15
|
+
@@log_file = File.join(@@rails_root, 'log', "#{@@proc_name}.log")
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.set_rails_root(dir)
|
19
|
+
@@rails_root = dir
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(args)
|
23
|
+
@options = {:worker_count => 1, :environment => :development, :delay => 5}
|
24
|
+
|
25
|
+
optparse = OptionParser.new do |opts|
|
26
|
+
opts.banner = "Usage: #{File.basename($0)} [options] start|stop|restart|run"
|
27
|
+
|
28
|
+
opts.on('-h', '--help', 'Show this message') do
|
29
|
+
puts opts
|
30
|
+
exit 1
|
31
|
+
end
|
32
|
+
opts.on('-e', '--environment=NAME', 'Specifies the environment to run this apn_sender under ([development]/production).') do |e|
|
33
|
+
@options[:environment] = e
|
34
|
+
end
|
35
|
+
opts.on('-n', '--number-of-workers=WORKERS', "Number of unique workers to spawn") do |worker_count|
|
36
|
+
@options[:worker_count] = worker_count.to_i rescue 1
|
37
|
+
end
|
38
|
+
opts.on('-v', '--verbose', "Turn on verbose mode") do
|
39
|
+
@options[:verbose] = true
|
40
|
+
end
|
41
|
+
opts.on('-d', '--delay=D', "Delay between rounds of work (seconds)") do |d|
|
42
|
+
@options[:delay] = d
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# If no arguments, give help screen
|
47
|
+
@args = optparse.parse!(args.empty? ? ['-h'] : args)
|
48
|
+
end
|
49
|
+
|
50
|
+
def daemonize
|
51
|
+
@options[:worker_count].times do |worker_index|
|
52
|
+
process_name = @options[:worker_count] == 1 ? @@proc_name : "#{@@proc_name}.#{worker_index}"
|
53
|
+
Daemons.run_proc(process_name, :dir => "#{@@rails_root}/tmp/pids", :dir_mode => :normal, :ARGV => @args) do |*args|
|
54
|
+
run process_name
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def run(worker_name = nil)
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module QuickUtils
|
2
|
+
class RakeDaemon < RailsDaemon
|
3
|
+
|
4
|
+
@@rake_task = ''
|
5
|
+
|
6
|
+
def self.set_rake_task(task)
|
7
|
+
@@rake_task = task
|
8
|
+
end
|
9
|
+
|
10
|
+
def run(worker_name = nil)
|
11
|
+
|
12
|
+
ENV['RAILS_ENV'] = @options[:environment].to_s
|
13
|
+
ENV['LOG_FILE'] = @@log_file
|
14
|
+
|
15
|
+
system "cd #{@@rails_root}; exec rake RAILS_ENV='#{@options[:environment].to_s}' LOG_FILE='#{@@log_file}' #{@@rake_task}"
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module QuickUtils
|
2
|
+
class ThreadRunner
|
3
|
+
|
4
|
+
def self.run(dataset, nthreads)
|
5
|
+
threads = []
|
6
|
+
count = dataset.size
|
7
|
+
nthreads = 1 if count < nthreads
|
8
|
+
bin_size = count / nthreads
|
9
|
+
puts "Dividing #{count} items into sizes of #{bin_size} for #{nthreads} threads."
|
10
|
+
t = 0
|
11
|
+
while t < nthreads do
|
12
|
+
subset = dataset[t*bin_size..t*bin_size+bin_size - 1]
|
13
|
+
threads << Thread.new(subset) do |cs|
|
14
|
+
yield cs
|
15
|
+
end
|
16
|
+
t = t + 1
|
17
|
+
end
|
18
|
+
threads.each {|t| t.join}
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module QuickUtils
|
2
|
+
class Watcher < RailsDaemon
|
3
|
+
def self.set_app_name(app_name)
|
4
|
+
@@main_proc = app_name
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.set_proc_list(plist)
|
8
|
+
@@processes = plist
|
9
|
+
end
|
10
|
+
|
11
|
+
def run(worker_name = nil)
|
12
|
+
|
13
|
+
logger = Logger.new(@@log_file)
|
14
|
+
|
15
|
+
while true
|
16
|
+
# get num thins running
|
17
|
+
thin_num = `ps -ef | grep "thin" | grep "#{@@main_proc}" | grep -v "grep" | wc | awk '{print $1}'`.to_i
|
18
|
+
needs_restart = false
|
19
|
+
@@processes.each do |proc|
|
20
|
+
pnum = `ps -ef | grep "#{proc}" | grep -v "grep" | wc | awk '{print $1}'`.to_i
|
21
|
+
needs_restart ||= (pnum == 0)
|
22
|
+
end
|
23
|
+
if thin_num == 0 || needs_restart
|
24
|
+
logger.info("Restarting #{main_proc} now...");
|
25
|
+
system "cd #{@@rails_root}; bundle exec script/restart;"
|
26
|
+
end
|
27
|
+
sleep @options[:delay]
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/quick_utils.rb
ADDED
data/quick_utils.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "quick_utils/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "quick_utils"
|
7
|
+
s.version = QuickUtils::VERSION
|
8
|
+
s.authors = ["Alan Graham"]
|
9
|
+
s.email = ["alangraham5@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Utility classes for a rails-deployed application}
|
12
|
+
s.description = %q{Utility classes for a rails-deployed application}
|
13
|
+
|
14
|
+
s.rubyforge_project = "quick_utils"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "daemons"
|
22
|
+
|
23
|
+
# specify any dependencies here; for example:
|
24
|
+
# s.add_development_dependency "rspec"
|
25
|
+
# s.add_runtime_dependency "rest-client"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: quick_utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alan Graham
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-20 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: daemons
|
16
|
+
requirement: &70129914024040 !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: *70129914024040
|
25
|
+
description: Utility classes for a rails-deployed application
|
26
|
+
email:
|
27
|
+
- alangraham5@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- Rakefile
|
35
|
+
- lib/quick_utils.rb
|
36
|
+
- lib/quick_utils/rails_daemon.rb
|
37
|
+
- lib/quick_utils/rake_daemon.rb
|
38
|
+
- lib/quick_utils/thread_runner.rb
|
39
|
+
- lib/quick_utils/version.rb
|
40
|
+
- lib/quick_utils/watcher.rb
|
41
|
+
- quick_utils.gemspec
|
42
|
+
homepage: ''
|
43
|
+
licenses: []
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project: quick_utils
|
62
|
+
rubygems_version: 1.8.15
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Utility classes for a rails-deployed application
|
66
|
+
test_files: []
|