only wait for compilation unless when necessary

This commit is contained in:
Sami Samhuri 2015-05-09 17:30:03 -07:00
parent b504f87e3c
commit 732da17859
3 changed files with 41 additions and 10 deletions

View file

@ -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() {

View file

@ -160,15 +160,17 @@ class HarpBlog
end
def compile
run('make compile')
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

View file

@ -83,14 +83,24 @@ before do
else
params
end
else
@fields = {}
end
end
after do
@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}ms"
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)