Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created February 13, 2013 14:59
Show Gist options
  • Save ttscoff/4945173 to your computer and use it in GitHub Desktop.
Save ttscoff/4945173 to your computer and use it in GitHub Desktop.
Example Rakefile additions for Jekyll task timer and push notifications
# Rake file tricks for timing and notifying task completion
# at the top of the file with other config settings
run_time = Time.now
# quick function to determine diff between passed time and now
def finish_timer(start_time)
finish_time = Time.now - start_time
total_min = "%d" % (finish_time / 60)
total_sec = "%02d" % (finish_time % 60)
"#{total_min}:#{total_sec}"
end
# my custom notification
# notify "text", [{:quiet => true, :url => "url to open if using Pushover"}]
def notify(msg, options = {})
# if :quiet is passed in the options, it won't show growl or Pushover notifications
quiet = options[:quiet].nil? ? false : options[:quiet]
link = options[:url].nil? ? nil : options[:url]
# might want to make this $stdout depending on what you're doing w/the output
$stderr.puts Time.now.strftime('%b %d %H:%M%p') + "-> #{msg}"
unless quiet
# uses http://brettterpstra.com/2013/02/10/beengone-a-script-friendly-way-to-check-computer-idle-time/
# can also be done with ioreg
away = %x{/usr/local/bin/beengone 5}.strip
if away == "false" && $mobile == false
# uses growlnotify
%x{growlnotify -t "Jekyll" -m "#{msg}"} if growl
else
# can optionally use my voicesms to send a text instead of Pushover
# $stderr.puts "Sending... " + %x{~/scripts/voicesms.rb -m "#{msg}"}
push_notify(msg, link) if growl
end
end
end
# sends a Pushover notification
# requires a token and user key from Pushover
def push_notify(msg, link)
url = URI.parse("https://api.pushover.net/1/messages")
req = Net::HTTP::Post.new(url.path)
req.set_form_data({
:token => "XXXXXXXX",
:user => "XXXXXXX",
:message => msg,
:title => "Jekyll",
:url => link.nil? ? '' : link,
:url_title => link.nil? ? '' : "View"
})
res = Net::HTTP.new(url.host, url.port)
res.use_ssl = true
res.verify_mode = OpenSSL::SSL::VERIFY_NONE
res.start {|http| http.request(req) }
end
# example of timed task
desc "Generate jekyll site"
task :generate, :limit, :preview do |t, args|
threshhold = "15"
args.with_defaults(:limit => 0, :preview => 'false')
# start the timer for the individual task
task_time = Time.now
notify "Generating Site with Jekyll"
########################
# perform generate tasks
########################
# return task time, total accumlated time (for running multiple tasks)
# Add a link to staging site in the options hash for Pushover to link to the preview
notify "Site generated. Task: " + finish_timer(task_time) + ", total: " + finish_timer(run_time), {:url => "http://staging.yoursite.com"}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment