ztk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ rvm ruby-1.9.3@ztk --create
2
+ #[[ $(gem -v) != "1.4.2" ]] && rvm rubygems 1.4.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ztk.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Zachary Patten
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # ZTK
2
+
3
+ Zachary's Toolkit
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ztk'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ztk
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+
24
+ ### Parallel
25
+
26
+ Parallel Processing Class
27
+
28
+ This class can be used to easily run iterative and linear processes in a parallel manner.
29
+
30
+ Example:
31
+
32
+ parallel = ZTK::Parallel.new
33
+ 20.times do |x|
34
+ parallel.process do
35
+ x
36
+ end
37
+ end
38
+ parallel.waitall
39
+ parallel.results
40
+ => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/lib/ztk.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "ztk/version"
2
+
3
+ require "ztk/logger"
4
+ require "ztk/parallel"
5
+
6
+ module ZTK
7
+ # Your code goes here...
8
+ end
data/lib/ztk/logger.rb ADDED
@@ -0,0 +1,40 @@
1
+ class ZTK::Logger < ::Logger
2
+ SEVERITIES = Severity.constants.inject([]) {|arr,c| arr[Severity.const_get(c)] = c; arr}
3
+
4
+ def initialize(filename)
5
+ super(filename)
6
+ set_log_level
7
+ end
8
+
9
+ def parse_caller(at)
10
+ if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
11
+ file = Regexp.last_match[1]
12
+ line = Regexp.last_match[2]
13
+ method = Regexp.last_match[3]
14
+ "#{File.basename(file)}:#{line}:#{method} | "
15
+ else
16
+ ""
17
+ end
18
+ end
19
+
20
+ def add(severity, message = nil, progname = nil, &block)
21
+ return if (@level > severity)
22
+
23
+ called_by = parse_caller(caller[1])
24
+ msg = (block && block.call)
25
+ return if (msg.nil? || msg.strip.empty?)
26
+ message = [message, progname, msg].delete_if{|i| i == nil}.join(": ")
27
+ message = "%19s.%06d+%05d|%5s|%s%s\n" % [Time.now.utc.strftime("%Y-%m-%d %H:%M:%S"), Time.now.utc.usec, Process.pid, SEVERITIES[severity], called_by, message]
28
+
29
+ @logdev.write(message)
30
+
31
+ true
32
+ end
33
+
34
+ def set_log_level(level=nil)
35
+ default = (Rails.env.production? ? "INFO" : "DEBUG")
36
+ log_level = (ENV['LOG_LEVEL'] || level || default)
37
+ self.level = ZTK::Logger.const_get(log_level.to_s.upcase)
38
+ end
39
+
40
+ end
@@ -0,0 +1,81 @@
1
+ class ZTK::Parallel
2
+ attr_accessor :results
3
+
4
+ def initialize(options={})
5
+ GC.copy_on_write_friendly = true if GC.respond_to?(:copy_on_write_friendly=)
6
+
7
+ options.reverse_merge!(
8
+ :max_forks => `grep -c processor /proc/cpuinfo`.strip.to_i,
9
+ :one_shot => false
10
+ )
11
+
12
+ @max_forks = options[:max_forks]
13
+ @one_shot = options[:one_shot]
14
+
15
+ @forks = Array.new
16
+ @results = Array.new
17
+ end
18
+
19
+ def process
20
+ pid = nil
21
+ return pid if (@forks.count >= @max_forks)
22
+
23
+ child_reader, parent_writer = IO.pipe
24
+ parent_reader, child_writer = IO.pipe
25
+
26
+ ActiveRecord::Base.connection.disconnect!
27
+ pid = Process.fork do
28
+ ActiveRecord::Base.establish_connection
29
+
30
+ parent_writer.close
31
+ parent_reader.close
32
+
33
+ if (data = yield).present?
34
+ child_writer.write(Base64.encode64(Marshal.dump(data)))
35
+ end
36
+
37
+ child_reader.close
38
+ child_writer.close
39
+ Process.exit!(0)
40
+ end
41
+ ActiveRecord::Base.establish_connection
42
+
43
+ child_reader.close
44
+ child_writer.close
45
+
46
+ fork = {:reader => parent_reader, :writer => parent_writer, :pid => pid}
47
+ @forks << fork
48
+
49
+ pid
50
+ end
51
+
52
+ def wait
53
+ pid, status = (Process.wait2(-1, Process::WNOHANG) rescue nil)
54
+ if pid.present? && status.present?
55
+ if (fork = @forks.select{ |f| f[:pid] == pid }.first).present?
56
+ data = (Marshal.load(Base64.decode64(fork[:reader].read.to_s)) rescue nil)
57
+ @results.push(data) if (data.present? && !@one_shot)
58
+
59
+ fork[:reader].close
60
+ fork[:writer].close
61
+
62
+ @forks -= [fork]
63
+ return [pid, status, data]
64
+ end
65
+ end
66
+ nil
67
+ end
68
+
69
+ def waitall
70
+ results = Array.new
71
+ while @forks.count > 0
72
+ results << wait
73
+ end
74
+ results
75
+ end
76
+
77
+ def count
78
+ @forks.count
79
+ end
80
+
81
+ end
@@ -0,0 +1,3 @@
1
+ module ZTK
2
+ VERSION = "0.0.1"
3
+ end
data/ztk.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/ztk/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Zachary Patten"]
6
+ gem.email = ["zachary@jovelabs.com"]
7
+ gem.description = %q{Zachary's Toolkit}
8
+ gem.summary = %q{Contains various helper classes and utility methods I find I regularly need.}
9
+ gem.homepage = "http://www.github.com/jovelabs"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "ztk"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ZTK::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ztk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Zachary Patten
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Zachary's Toolkit
15
+ email:
16
+ - zachary@jovelabs.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rvmrc
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - lib/ztk.rb
28
+ - lib/ztk/logger.rb
29
+ - lib/ztk/parallel.rb
30
+ - lib/ztk/version.rb
31
+ - ztk.gemspec
32
+ homepage: http://www.github.com/jovelabs
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.24
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Contains various helper classes and utility methods I find I regularly need.
56
+ test_files: []