Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
matsadler committed Apr 22, 2012
0 parents commit 93ee5f0
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 0 deletions.
63 changes: 63 additions & 0 deletions README.rdoc
@@ -0,0 +1,63 @@
= LaunchTracks

LaunchTracks uses launchd and the Tracks Rack server to start your Ruby web
apps on demand.

== Install

gem install launch_tracks

== Usage

cd /path/to/myapp
launch_tracks setup 4000

Your app will automatically start once you visit http://localhost:4000/ and
shutdown after 10 minutes idle.

The output from stdout and stderr will be available at /tmp/myapp-out.txt and /tmp/myapp-err.txt respectively.

To restart your app <tt>touch restart.txt</tt> in your app's base directory.

=== Commands

[setup] Creates a launchd plist for your app in ~/Library/LaunchAgents, and
loads it, making your app available for on-demand startup. Requires a
port number for your app to run on as an argument.
Example: <tt>launch_tracks setup 4000</tt>

[load] Loads your app's launchd plist from ~/Library/LaunchAgents, making
your app available for on-demand startup.

[unload] Unloads your app's launchd plist from ~/Library/LaunchAgents,
disabling on-demand startup.

[restart] Restart your app. The same can be achieved with
<tt>touch restart.txt</tt> in your app's base directory.

[rm] Unload and remove your app's plist from ~/Library/LaunchAgents,
permanently disabling it.

== Licence

(The MIT License)

Copyright (c) 2012 Matthew Sadler

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:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

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.
92 changes: 92 additions & 0 deletions bin/launch_tracks
@@ -0,0 +1,92 @@
#!/usr/bin/env ruby
require "fileutils"

name = FileUtils.pwd.split("/").last
label = "com.github.matsadler.launch_tracks.#{name}"
logdir = "/tmp"
plist_name = "#{label}.plist"
plist_dir = File.expand_path("~/Library/LaunchAgents")
plist_path = "#{plist_dir}/#{plist_name}"

command, port = ARGV

help = "Usage: launch_tracks help|setup PORTNUMBER|load|unload|restart|rm"

case command
when "help", "-h", "--help"
puts help
exit
when "load"
exec "launchctl load #{plist_path}"
when "unload"
exec "launchctl unload #{plist_path}"
when "restart"
FileUtils.touch("restart.txt")
exit
when "rm"
`launchctl unload #{plist_path}`
FileUtils.rm(plist_path)
exit
when "setup"
abort help unless port =~ /^\d+$/
# continue
else
abort help
end

plist = <<-PLIST
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>#{label}</string>
<key>WorkingDirectory</key>
<string>#{FileUtils.pwd}</string>
<key>StandardOutPath</key>
<string>#{logdir}/#{name}-out.log</string>
<key>StandardErrorPath</key>
<string>#{logdir}/#{name}-err.log</string>
<key>EnvironmentVariables</key>
<dict>
#{ENV.map do |key,string|
"<key>#{key}</key>
<string>#{string}</string>"
end.join("\n ")}
</dict>
<key>ProgramArguments</key>
<array>
#{if File.exist?("Gemfile")
"<string>bundle</string>
<string>exec</string>
"
end}<string>rackup</string>
<string>-rlaunch_tracks</string>
<string>-slaunch_tracks</string>
</array>
<key>Sockets</key>
<dict>
<key>http</key>
<dict>
<key>SockServiceName</key>
<string>#{port}</string>
<key>SockFamily</key>
<string>IPv4</string>
</dict>
</dict>
</dict>
</plist>
PLIST

FileUtils.mkdir_p(plist_dir)
File.open(plist_path, "w") {|f| f << plist}

exec "launchctl load #{plist_path}"
17 changes: 17 additions & 0 deletions launch_tracks.gemspec
@@ -0,0 +1,17 @@
Gem::Specification.new do |s|
s.name = "launch_tracks"
s.version = "0.1.0"
s.summary = "Start Rack apps on demand with launchd."
s.description = "Automaticlly start Rack applications on demand using launchd."
s.files = Dir["lib/**/*.rb"] << "README.rdoc"
s.require_path = "lib"
s.executables << "launch_tracks"
s.rdoc_options << "--main" << "README.rdoc" << "--charset" << "utf-8"
s.extra_rdoc_files = ["README.rdoc"]
s.author = "Matthew Sadler"
s.email = "mat@sourcetagsandcodes.com"
s.homepage = "http://github.com/matsadler/launch_tracks"
s.add_dependency("launch", "~> 2.0")
s.add_dependency("tracks", "~> 0.1")
s.add_dependency("rack-idle", "~> 0.1")
end
16 changes: 16 additions & 0 deletions lib/launch_tracks.rb
@@ -0,0 +1,16 @@
require "fileutils"
require "launch"
require "tracks"
require "rack/idle"

Rack::Handler.register("launch_tracks", "LaunchTracks")

module LaunchTracks

def self.run(app, options={})
app = Rack::Idle.new(app, :watch => "restart.txt")
sockets = Launch::Job.checkin.sockets["http"]
Tracks.new(app, options).listen(sockets.first)
end

end

0 comments on commit 93ee5f0

Please sign in to comment.