unicorn-directory-watcher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ .idea
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2-p180@unicorn-directory-watcher --create
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :gemcutter
2
+
3
+ gemspec :name => "unicorn-directory-watcher"
@@ -0,0 +1,16 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ unicorn-directory-watcher (0.1.0)
5
+ directory_watcher (>= 1.4.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ directory_watcher (1.4.0)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ unicorn-directory-watcher!
data/README ADDED
File without changes
@@ -0,0 +1,99 @@
1
+ require 'fileutils'
2
+ require 'directory_watcher'
3
+
4
+ module UnicornDirectoryWatcher
5
+ def self.call(*args, &block)
6
+ Runner.new(*args).call(&block)
7
+ end
8
+
9
+ class Runner
10
+ attr_reader :app_name, :root_dir, :watcher_globs
11
+
12
+ def initialize(app_name, root_dir, params={})
13
+ @app_name, @root_dir = app_name, root_dir
14
+ @watcher_globs = params[:watcher_globs] || {
15
+ root_dir => "{app,lib,vendor}/**/*.rb"
16
+ }
17
+ end
18
+
19
+ def call(&block)
20
+ if RUBY_PLATFORM =~ /darwin/
21
+ EM.kqueue = true
22
+ else
23
+ EM.epoll = true
24
+ end
25
+
26
+ FileUtils.mkdir_p("#{root_dir}/log")
27
+ ENV["UNICORN_STDERR_PATH"] = "#{root_dir}/log/#{app_name}.development.stderr.log"
28
+ ENV["UNICORN_STDOUT_PATH"] = "#{root_dir}/log/#{app_name}.development.stdout.log"
29
+
30
+ tail_stderr_log = fork do
31
+ system "tail -f #{ENV["UNICORN_STDERR_PATH"]}"
32
+ end
33
+
34
+ # remove the old log
35
+ system "rm -f -- #{logfile}"
36
+
37
+ # start the unicorn
38
+ yield
39
+
40
+ # get the pid
41
+ system "touch #{pidfile}"
42
+
43
+ master_pid = lambda do
44
+ File.open(pidfile) { |f| f.read }.chomp.to_i
45
+ end
46
+
47
+ system "touch #{logfile}"
48
+
49
+ directory_watchers = watcher_globs.map do |dir, glob|
50
+ # watch our app for changes
51
+ dw = DirectoryWatcher.new dir,
52
+ :glob => glob,
53
+ :scanner => :em,
54
+ :pre_load => true
55
+
56
+ # SIGHUP makes unicorn respawn workers
57
+ dw.add_observer do |*args|
58
+ old_pid = master_pid.call
59
+ Process.kill :USR2, old_pid
60
+ start = Time.now
61
+ loop do
62
+ raise TimeoutError if Time.now - start > 5
63
+ break if master_pid.call != old_pid
64
+ sleep 0.5
65
+ end
66
+ Process.kill :QUIT, old_pid
67
+ end
68
+ Process.kill :HUP, tail_stderr_log
69
+
70
+ dw
71
+ end
72
+
73
+ # wrap this in a lambda, just to avoid repeating it
74
+ stop = lambda { |sig|
75
+ Process.kill :QUIT, master_pid.call # kill unicorn
76
+ directory_watchers.each do |dw|
77
+ dw.stop
78
+ end
79
+ exit
80
+ }
81
+
82
+ trap("INT", stop)
83
+
84
+ directory_watchers.each do |dw|
85
+ dw.start
86
+ end
87
+ sleep
88
+ end
89
+
90
+ protected
91
+ def logfile
92
+ "#{root_dir}/log/unicorn.log"
93
+ end
94
+
95
+ def pidfile
96
+ "#{root_dir}/tmp/pids/unicorn.pid"
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,3 @@
1
+ module UnicornDirectoryWatcher
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib/', __FILE__)
3
+ $:.unshift lib unless $:.include?(lib)
4
+
5
+ require 'unicorn_directory_watcher/version'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "unicorn-directory-watcher"
9
+ s.version = ::UnicornDirectoryWatcher::VERSION
10
+ s.platform = Gem::Platform::RUBY
11
+ s.authors = ["Brian Takita"]
12
+ s.email = ["btakita@truecar.com"]
13
+ s.homepage = "https://github.com/TrueCar/unicorn-directory-watcher"
14
+ s.summary = %q{Unicorn wrapper that restarts the server when a file changes (inspired by http://namelessjon.posterous.com/?tag=unicorn)}
15
+ s.description = %q{Unicorn wrapper that restarts the server when a file changes (inspired by http://namelessjon.posterous.com/?tag=unicorn)}
16
+
17
+ s.required_rubygems_version = ">= 1.3.6"
18
+
19
+ # Man files are required because they are ignored by git
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.require_paths = ["lib"]
23
+
24
+ s.add_dependency "directory_watcher", ">=1.4.0"
25
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unicorn-directory-watcher
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Brian Takita
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-16 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: directory_watcher
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 1.4.0
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description: Unicorn wrapper that restarts the server when a file changes (inspired by http://namelessjon.posterous.com/?tag=unicorn)
28
+ email:
29
+ - btakita@truecar.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - .gitignore
38
+ - .rvmrc
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - README
42
+ - lib/unicorn_directory_watcher.rb
43
+ - lib/unicorn_directory_watcher/version.rb
44
+ - unicorn-directory-watcher.gemspec
45
+ has_rdoc: true
46
+ homepage: https://github.com/TrueCar/unicorn-directory-watcher
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 1.3.6
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.9.2
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Unicorn wrapper that restarts the server when a file changes (inspired by http://namelessjon.posterous.com/?tag=unicorn)
73
+ test_files: []
74
+