Skip to content

Instantly share code, notes, and snippets.

@lenary
Forked from seancribbs/00demo.md
Last active December 18, 2015 17:49
Show Gist options
  • Save lenary/5821651 to your computer and use it in GitHub Desktop.
Save lenary/5821651 to your computer and use it in GitHub Desktop.
%% -------------------------------------------------------------------
%%
%% counters_demo: Sets up and clears node partitions
%%
%% Copyright (c) 2007-2012 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
-module(counters_demo).
-export([part/2]).
% Instructions:
% Attach a shell to your cluster, and partition by running:
% > Healer = counters_demo:part(['[email protected]'], "newcookie").
% Then to heal the partition, run:
% > Healer().
%
part(Friends0, Cookie) ->
OldCookie = erlang:get_cookie(),
Enemies = nodes() -- Friends0,
Friends = [node()|Friends0],
[ rpc:call(F, erlang, set_cookie, [F, Cookie]) || F <- Friends ],
[ rpc:call(F, erlang, disconnect_node, [E]) || F <- Friends,
E <- Enemies ],
fun() ->
heal(Friends, Enemies, OldCookie)
end.
heal(Friends, Enemies, Cookie) ->
[ rpc:call(F, erlang, set_cookie, [F, Cookie]) || F <- Friends ],
rpc:sbcast(Friends ++ Enemies, riak_core_node_watcher, broadcast).
<!DOCTYPE html>
<html>
<head>
<script src="jquery-2.0.0.min.js"></script>
<script type="text/javascript">
var t = window.setInterval(function() {
get_counter();}, 500);
function get_counter() {
$.ajax({
url: "/buckets/demo/counters/demo",
cache: false,
dataType: "text",
success: function(data) {
$('#counter').text(data);
}});
}
$(document).ready(function() {
$("#increment").click(function() {
$.ajax({
type: 'POST',
data: '1',
url: '/buckets/demo/counters/demo',
success: function() { get_counter() },
cache:false,
dataType: 'text'
});
});
});
$(document).ready(function() {
$("#decrement").click(function() {
$.ajax({
type: 'POST',
data: '-1',
url: '/buckets/demo/counters/demo',
success: function() { get_counter() },
cache:false,
dataType: 'text'
});
});
});
</script>
<style>
body {
background: #fbfbfb;
font-family:'titillium', verdana, arial, sans-serif;
font-weight: normal;
line-height: 1;
}
h1, h2, h3, h4, h5, h6 {
font-family:'titillium', verdana, arial, sans-serif;
font-weight: bold;
color: #444;
}
input, textarea {
border: none;
}
input {
color: #fff;
}
#counter {
background: #384945;
text-align: center;
margin: 0 auto;
color: #fe9925;
font-family:'titillium', verdana, arial, sans-serif;
font-weight: bold;
font-size: 84pt;
width: 30%;
padding: .2em
}
#controls {
padding: 3em;
text-align: center;
}
</style>
<title>Riak KV Counters Demo</title>
</head>
<body>
<div id="counter">
</div>
<div id="controls">
<a href="#" id="increment">Increment</a>&nbsp;
<a href="#" id="decrement">Decrement</a>
</div>
</body>
</html>
#!/bin/sh
if [[ ! -z $node ]]; then
node=localhost:10018
fi
function storeInRiak {
echo "Storing $1 as $2";
curl -X PUT "http://$node/buckets/crdt_cookbook/keys/$1" -H "Content-Type: $2" --data-binary @$1
}
storeInRiak counters_demo.html 'text/html'
curl -o jquery-2.0.0.min.js http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js
storeInRiak jquery-2.0.0.min.js 'application/javascript'
echo "Done. Open: http://$node/buckets/crdt_cookbook/keys/counters_demo.html"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment