Created
February 26, 2014 06:40
-
-
Save jmervine/9224710 to your computer and use it in GitHub Desktop.
Simple Web Interface to Purge Caches via MaxCDN
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
require "thread" | |
require "maxcdn" | |
require "sinatra" | |
require "json" | |
require "haml" | |
THREAD_COUNT = 4.freeze # max thread count | |
maxcdn = MaxCDN::Client.new(ENV["ALIAS"], ENV["KEY"], ENV["SECRET"]) | |
set :bind, "0.0.0.0" | |
set :port, 3333 | |
get "/" do | |
### | |
# Fetch Zone list for display | |
haml(:index, :locals => { :zones => maxcdn.get("zones/pull.json")["data"]["pullzones"] }) | |
end | |
get "/purge" do | |
zones = params["zones"].split(",").each(&:strip!).each(&:to_i) | |
### | |
# Ensure thread safe via Mutex | |
mutex = Mutex.new | |
### | |
# Collection for each purge result | |
results = [] | |
### | |
# Too many threads can be bad, restrict threads to THREAD_COUNT | |
THREAD_COUNT.times.map do | |
Thread.new(zones, results) do |zones, results| | |
### | |
# Loop through zones | |
while zone = mutex.synchronize { zones.pop } | |
### | |
# Looging is good | |
puts "INFO: Purging Zone: #{zone} (#{Time.now})" | |
### | |
# Purge zone and collect result | |
result = maxcdn.purge(zone) | |
### | |
# Merge results (thread) safely | |
mutex.synchronize do | |
results << result | |
end | |
end | |
end | |
end.each(&:join) | |
content_type :json | |
JSON.dump results | |
end |
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
source "https://rubygems.org" | |
gem "sinatra" | |
gem "haml" | |
gem "maxcdn" |
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
!!! | |
%html | |
%head | |
%title MaxCDN Purge Caches | |
%meta{:charset => "utf-8"} | |
%meta{:name => "viewport", :content => "width=device-width, initial-scale=1"} | |
%link{:href=>"//netdna.bootstrapcdn.com/bootswatch/3.1.1/united/bootstrap.min.css", :rel=>"stylesheet"} | |
%script{:src => "http://code.jquery.com/jquery-2.1.0.min.js"} | |
:css | |
button { | |
width: 100px | |
} | |
:javascript | |
$(window).load(function() { | |
$("button").each(function() { | |
//- Add click event to all buttons. | |
$(this).click(function() { | |
//- Build purge url string from button value. | |
var url = "/purge?zones="+$(this).attr("value"); | |
var that = $(this); | |
//- Add a simple text spinner while pruging. | |
var spinner = [ "|", "/", "---", "\\" ]; | |
var spinIdx = 0; | |
function ticker() { | |
if (spinIdx == spinner.length) { | |
spinIdx = 0; | |
} | |
that.html(spinner[spinIdx++]); | |
} | |
ticker(); | |
var interval = setInterval(function() { | |
ticker(); | |
}, 1000); | |
// Update button style. | |
that.removeClass("btn-primary"); | |
that.addClass("btn-warning"); | |
that.addClass("disabled"); | |
// Sumbit Ajax purge request. | |
$.ajax(url) | |
.always(function() { | |
// On pass or fail, cleanup. | |
clearInterval(interval); | |
that.removeClass("disabled"); | |
that.removeClass("btn-warning"); | |
}) | |
.done(function() { | |
// On success, notify. | |
that.html("Complete"); | |
that.addClass("btn-success"); | |
}) | |
.fail(function() { | |
// On error, notify. | |
that.html("ERROR!"); | |
that.addClass("btn-danger"); | |
}); | |
}); | |
}); | |
}); | |
%body | |
.navbar.navbar-default.navbar-static-top | |
.navbar-header | |
%a.navbar-brand{:href => "/"} MaxCDN Purge Caches | |
.container | |
.table-responsive | |
%table.table.table-striped | |
%thead | |
%tr | |
%th Zone Name | |
%th Zone ID | |
%th(width=100) | |
%button.btn.btn-sm.btn-primary{:value => zones.map{|z|z["id"]}.join(",")} Purge All | |
%tbody | |
- for zone in zones | |
%tr | |
%td= zone["name"] | |
%td= zone["id"] | |
%td | |
%button.btn.btn-sm.btn-primary{:value => zone["id"]} Purge | |
%footer | |
%center | |
%p | |
Powered by | |
%a{:href => "http://maxcdn.com"} MaxCDN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment