Last active
August 29, 2015 14:06
-
-
Save parabuzzle/794100147a8f5ca10664 to your computer and use it in GitHub Desktop.
A simple little ruby script for starting a jekyll server and builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env ruby | |
require 'thread' | |
# Set the process name | |
$0 = 'jekyll_dev' | |
# Environment | |
ENV['JEKYLL_WITH_DRAFTS'] ||= 'true' | |
# Commands to run | |
start_server = 'jekyll server' | |
start_builder = 'jekyll build --watch --drafts' | |
start_builder_no_drafts = 'jekyll build --watch' | |
# Setup | |
threads = [] | |
shutdown = false | |
# Run the threads | |
puts 'starting server on port 4000' | |
threads << Thread.new { `#{start_server}` } | |
sleep 1 | |
if ENV['JEKYLL_WITH_DRAFTS'] == 'true' | |
puts 'starting builder with drafts enabled' | |
threads << Thread.new { `#{start_builder}` } | |
else | |
puts 'starting builder without drafts enabled' | |
threads << Thread.new { `#{start_builder_no_drafts}` } | |
end | |
# Signal Handling | |
Signal.trap("TERM") { shutdown = true } | |
Signal.trap("KILL") { shutdown = true } | |
Signal.trap("INT") { shutdown = true } | |
# Main Loop | |
while shutdown != true do | |
sleep 0.2 | |
end | |
puts '...shutting down' | |
# If we break out of the main loop.. we kill the threads | |
threads.each do |thread| | |
Thread.kill(thread) | |
end | |
# Ensure that all threads are stopped properly | |
threads.each do |thread| | |
unless thread | |
while thread.status.match(/run|sleep/) | |
Thread.kill(thread) | |
sleep 0.1 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment