From 732da1785991cfcb0471ee63197af27db86463eb Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sat, 9 May 2015 17:30:03 -0700 Subject: [PATCH] only wait for compilation unless when necessary --- bin/compile.sh | 17 +++++++++++++++++ server/harp_blog.rb | 14 ++++++++------ server/server.rb | 20 ++++++++++++++++---- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/bin/compile.sh b/bin/compile.sh index 5fd7021..91f3156 100755 --- a/bin/compile.sh +++ b/bin/compile.sh @@ -7,6 +7,21 @@ DIR=$(dirname "$0") HARP="node_modules/harp/bin/harp" BLOG_DIR="${1:-${DIR}/..}" TARGET="${BLOG_DIR%/}/${2:-www}" +LOCK_FILE="$BLOG_DIR/compile.lock" + +if [[ -e "$LOCK_FILE" ]]; then + echo "Bailing, another compilation is running" + exit 1 +fi + +function lock { + echo $$ >| "$LOCK_FILE" +} +function delete_lock_file { + rm -f "$LOCK_FILE" +} +trap delete_lock_file SIGHUP SIGINT SIGTERM SIGEXIT +lock function main() { echo "* compile rss feed" @@ -27,6 +42,8 @@ function main() { echo "* minify js" minify_js + + delete_lock_file } function compile_rss() { diff --git a/server/harp_blog.rb b/server/harp_blog.rb index 6085a7d..cced3e2 100644 --- a/server/harp_blog.rb +++ b/server/harp_blog.rb @@ -160,15 +160,17 @@ class HarpBlog end def compile - run('make compile') - @mutated = false + success, output = run('make compile') + if success + @mutated = false + else + puts output + end + success end def compile_if_mutated - if @mutated - compile - true - end + compile if @mutated end diff --git a/server/server.rb b/server/server.rb index 5231e57..dca012e 100755 --- a/server/server.rb +++ b/server/server.rb @@ -83,14 +83,24 @@ before do else params end + else + @fields = {} end end after do - start = Time.now - if blog.compile_if_mutated - duration = Time.now.to_f - start.to_f - puts "Compiled blog in #{duration.round}ms" + @wait_for_compilation ||= @fields['wait'] + compile = -> { + start = Time.now + if blog.compile_if_mutated + duration = Time.now.to_f - start.to_f + puts "Compiled blog in #{duration.round(2)}s" + end + } + if @wait_for_compilation + compile.call + else + Thread.new &compile end end @@ -275,6 +285,7 @@ post '/posts/drafts/:id/publish' do |id| if post = blog.get_draft(id) new_post = blog.publish_post(post) + @wait_for_compilation = true status 201 headers 'Location' => url_for(new_post.url), 'Content-Type' => 'application/json' JSON.generate(post: new_post.fields) @@ -391,6 +402,7 @@ post '/posts/:year/:month/:id/unpublish' do |year, month, id| if post = blog.get_post(year, month, id) new_post = blog.unpublish_post(post) + @wait_for_compilation = true status 200 headers 'Location' => url_for(new_post.url), 'Content-Type' => 'application/json' JSON.generate(post: new_post.fields)