spork_tasks 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ Spork Tasks
2
+ ===========
3
+ Spork is kind of annoying to deal with. I just want it to run in the background and have an easy way to restart, start, and stop it. So here are my tasks for doing that.
4
+
5
+ You can copy the rake tasks wherever you like, but I've packaged them as a rails generator
6
+
7
+ Installing
8
+ ----------
9
+
10
+ gem install spork_tasks
11
+
12
+ # If you're using Bundler:
13
+ gem 'spork_tasks', '~> 1.0.0'
14
+
15
+ rails generate spork_tasks
16
+ # Copies the rake file for you.
17
+
18
+ Tasks
19
+ -----
20
+ rake spork:start
21
+ # Starts the server
22
+
23
+ rake spork:stop
24
+ # Stops the server
25
+
26
+ rake spork:restart
27
+ # Restarts spork
28
+
29
+ TODO
30
+ ----
31
+ Better process management. Instead of guessing how long to wait (and sleep) for Rails to start, actually wait for it to start before handing the command line back.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,108 @@
1
+ require 'spork'
2
+ require 'spork/runner'
3
+ # Let's fork spork and put in the background and forget about it.
4
+ # You can start|stop|restart spork using this rake task
5
+ #
6
+ # Examples
7
+ #
8
+ # rake spork:start
9
+ # # => starts spork and detaches the process
10
+ #
11
+ # rake spork:stop
12
+ # # => kills the spork process based on the pid stored in tmp/spork.pid
13
+ #
14
+ # rake spork:restart
15
+ # # => stops then starts spork
16
+ #
17
+ namespace :spork do
18
+ desc "Starts spork server"
19
+ task :start do
20
+ begin
21
+ print "Starting spork..."
22
+
23
+ pid = fork do
24
+ # Swallows spork's notifications. I don't want it spewing all over my terminal from the background.
25
+ $stdout = File.new('/dev/null', 'w')
26
+ # If you want to change the default config you can
27
+ # specify the command line options here
28
+ # For example to change the port:
29
+ # options = ["--port", "7443"]
30
+ options = []
31
+ begin
32
+ Spork::Runner.run(options, $stderr, $stderr)
33
+ rescue => e
34
+ $stderr.puts
35
+ $stderr.puts "#{e.class} => #{e.message}"
36
+ $stderr.puts e.backtrace.join("\n")
37
+ end
38
+ # TODO maybe swallow stderr now... will that work?
39
+ # It should b/c its in the same process... or does spork hijack it?
40
+ # $stderr = File.new('/dev/null', 'w')
41
+ end
42
+
43
+ # Detach the pid, keep track of the pid, and wait for Rails to start.
44
+
45
+ Process.detach(pid)
46
+ File.open("#{tmp_dir}/spork.pid", "w"){|f| f.write pid}
47
+
48
+ seconds = 15
49
+
50
+ puts "\033[35m[Giving Rails #{seconds} seconds to start]\033[0m\n"
51
+ puts "\033[36mYou can change the wait time in lib/tasks/spork.rake \nif Rails is taking longer than #{seconds} seconds to load\033[0m\n"
52
+
53
+ sleep seconds
54
+
55
+ # See if the process actually started
56
+ if process_running?(pid)
57
+ puts "\033[32m[Sporkified!]\033[0m\n"
58
+ else
59
+ puts "\033[31m[Spork failed to start]\033[0m\n"
60
+ end
61
+ rescue StandardError => e
62
+ puts e.inspect
63
+ puts "\033[31m[Spork failed to start]\033[0m\n"
64
+
65
+ end
66
+ end
67
+
68
+ desc "Stops the spork server"
69
+ task :stop do
70
+ print "Stopping spork..."
71
+ if File.exist?("#{tmp_dir}/spork.pid")
72
+ pid = File.read("#{tmp_dir}/spork.pid").to_i
73
+ begin
74
+ Process.kill("INT", pid)
75
+ print "\033[32m[OK]\033[0m\n"
76
+ sleep 1
77
+ rescue Errno::ESRCH => e
78
+ print "\033[33m[not running]\033[0m\n"
79
+ rescue StandardError => e
80
+ print "\033[31m[FAILED]\033[0m\n"
81
+ puts e.inspect
82
+ end
83
+ else
84
+ print "\033[33m[not running]\033[0m\n"
85
+ end
86
+ print "\033[30mis it a spoon? is it a fork?\033[0m\n"
87
+ end
88
+
89
+ desc "Restart the spork server"
90
+ task :restart => [:stop, :start]
91
+
92
+ def tmp_dir
93
+ File.expand_path('../../../tmp', __FILE__)
94
+ end
95
+
96
+ def process_running?(pid=nil)
97
+ unless pid
98
+ return unless File.exist?("#{tmp_dir}/spork.pid")
99
+ pid = File.read("#{tmp_dir}/spork.pid").to_i
100
+ end
101
+ begin
102
+ Process.kill(0, pid)
103
+ rescue Errno::ESRCH => e
104
+ return false
105
+ end
106
+ end
107
+
108
+ end
@@ -0,0 +1,10 @@
1
+ class SporkTasksGenerator < Rails::Generators::Base
2
+ source_root File.expand_path("../.", __FILE__)
3
+
4
+ def copy_rake_tasks
5
+ puts "="*45
6
+ puts "Copying spork.rake into lib/tasks/"
7
+ puts "="*45
8
+ copy_file "spork.rake", "lib/tasks/spork.rake"
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |s|
3
+ s.name = "spork_tasks"
4
+ s.version = "1.0.0"
5
+ s.authors = ["Tyler Montgomery"]
6
+ s.email = ["tyler.a.montgomery@gmail.com"]
7
+ s.homepage = ""
8
+ s.summary = %q{Rake tasks for managing Spork}
9
+ s.description = %q{start|stop|restart tasks to manage the Spork drb server}
10
+
11
+ s.rubyforge_project = "spork_tasks"
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.require_paths = ["lib"]
15
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spork_tasks
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tyler Montgomery
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-28 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: start|stop|restart tasks to manage the Spork drb server
15
+ email:
16
+ - tyler.a.montgomery@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - README.md
22
+ - Rakefile
23
+ - lib/rails/generators/spork_tasks/spork.rake
24
+ - lib/rails/generators/spork_tasks/spork_tasks_generator.rb
25
+ - spork_tasks.gemspec
26
+ homepage: ''
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project: spork_tasks
46
+ rubygems_version: 1.8.10
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: Rake tasks for managing Spork
50
+ test_files: []