Last active
August 29, 2015 14:08
-
-
Save TylerFisher/6b6f63ee0e4b34d483a4 to your computer and use it in GitHub Desktop.
This code will refresh all users on your webpage upon deployment of a timestamp.json file to S3. We used this to deploy hotfixes on election night for elections.npr.org. It requires Fabric (a Python library for running tasks on the command line) and jquery-cookie.js, but could easily be refactored to not.
This file contains hidden or 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
#!/usr/bin/env python | |
""" | |
On the server (or locally), deploy a timestamp to S3 | |
if we need to refresh all browsers | |
currently connected to our page | |
""" | |
from datetime import datetime | |
import json | |
from fabric.api import local, task | |
@task | |
def reset_browsers(): | |
""" | |
Create a timestampped JSON file so the client will reset their page. | |
""" | |
payload = {} | |
# get current time and convert to epoch time | |
now = datetime.now().strftime('%s') | |
# set everything you want in the json file | |
payload['timestamp'] = now | |
with open('www/live-data/timestamp.json', 'w') as f: | |
json.dump(now, f) | |
deploy_json('www/live-data/timestamp.json', 'live-data/timestamp.json') | |
def deploy_json(src, dst): | |
""" | |
Deploy to S3. Note the cache headers. | |
""" | |
bucket = 'elections.npr.org' | |
region = 'us-west-2' | |
sync = 'aws s3 cp %s %s --acl "public-read" --cache-control "max-age=5 no-cache no-store must-revalidate" --region "%s"' | |
local(sync % (src, 's3://%s/%s' % (%s, dst), bucket_region)) |
This file contains hidden or 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
The MIT License (MIT) | |
Copyright (c) 2014 NPR | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. |
This file contains hidden or 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
/* | |
* On the client, poll for the timestamp file and check to see if a new timestamp has been deployed. | |
* If so, refresh the client. | |
*/ | |
var reloadTimestamp = null; | |
var getTimestamp = function() { | |
// get the timestamp on page load | |
if (reloadTimestamp == null) { | |
checkTimestamp(); | |
} | |
// continually check the timestamp every three minutes | |
setInterval(checkTimestamp, 180000); | |
} | |
var checkTimestamp = function() { | |
$.ajax({ | |
'url': '/live-data/timestamp.json', | |
// bust the browser cache | |
'cache': false, | |
'success': function(data) { | |
var newTime = data['timestamp']; | |
// if we haven't set a timestamp yet, set it | |
if (reloadTimestamp == null) { | |
reloadTimestamp = newTime; | |
} | |
// if the initial timestamp doesn't match the new one, refresh the page | |
if (reloadTimestamp != newTime) { | |
// set a cookie in case we need to do special things on this reload | |
$.cookie('reload', true); | |
window.location.reload(true); | |
} | |
} | |
}); | |
} | |
$(document).ready(function)() { | |
getTimestamp(); | |
// stuff you only want to happen if we forced a refresh, like skipping a welcome screen | |
if ($.cookie('reload')) { | |
// stuff | |
$.removeCookie('reload'); | |
} | |
}); |
This file contains hidden or 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
{ | |
"timestamp": "1415214618" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment