Archive for category software development
node.js at Facebook
Jan 23
Recently I found the need to create an init.d script and since I had a hard time finding an example elsewhere1, here’s the overly simple script I came up with to get the job done:
#!/bin/bash
# myapp daemon
# chkconfig: 345 20 80
# description: myapp daemon
# processname: myapp
DAEMON_PATH="/home/wes/Development/projects/myapp"
DAEMON=myapp
DAEMONOPTS="-my opts"
NAME=myapp
DESC="My daemon description"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
case "$1" in
start)
printf "%-50s" "Starting $NAME..."
cd $DAEMON_PATH
PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
#echo "Saving PID" $PID " to " $PIDFILE
if [ -z $PID ]; then
printf "%s\n" "Fail"
else
echo $PID > $PIDFILE
printf "%s\n" "Ok"
fi
;;
status)
printf "%-50s" "Checking $NAME..."
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
if [ -z `ps axf | grep 5003 | grep -v grep` ]; then
printf "%s\n" "Process dead but pidfile exists"
else
echo "Running"
fi
else
printf "%s\n" "Service not running"
fi
;;
stop)
printf "%-50s" "Stopping $NAME"
PID=`cat $PIDFILE`
cd $DAEMON_PATH
if [ -f $PIDFILE ]; then
kill -HUP $PID
printf "%s\n" "Ok"
rm -rf $PIDFILE
else
printf "%s\n" "pidfile not found"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {status|start|stop|restart}"
exit 1
esac
This script will work in /etc/init.d on Xubuntu 11.10 (so most Debian-based systems) and CentOS 5.5 and you can control it via chkconfig.
- That said, if you know of such an example I’d love to hear from you. [↩]
Fun with jsonselect
Nov 16
One of the strengths of CSS and jQuery is that it provides a common and powerful mechanism known as a selector language for referencing bits of data, especially data whose structure is not exactly known at runtime which makes such an addressing scheme a perfect fit for the often lumpy world of HTML.
Increasingly JSON is being used as a transport medium for data and with the rise of NoSQL solutions, having a selector language for JSON makes a lot of sense when dealing with JSON documents whose structure isn’t deterministic.
JSONSelect provides a good implementation of just such a JSON selector language but after working with it on a project I found myself needing to do more than it allowed me to do. Namely, I wanted 1. to be able to perform a selection and receive matching paths instead of the data contained in those paths and I wanted 2. to be able to modify data specified at a path location in-place.
jsonselect.match(sel, obj, asPath); // Added the asPath flag to return a path instead of the values jsonselect.forEach(sel, obj, fun, asPath); // Added the same flag to forEach, I use this to jsonselect.get(path,obj); // For getting the value using a path jsonselect.set(path, value, obj); // For setting the value of a path jsonselect.del(path,root); // For deleting a path
Here is my modified version of jsonselect in case anyone needs help solving the same problems I mentioned above.
When writing reports I’ve often come across the need to find the unix timestamp beginning and end of a day. Here is a Python snippet that does just that.
yesterday = datetime.datetime.now() - datetime.timedelta(days = 1) yesterday_beginning = datetime.datetime(yesterday.year, yesterday.month, yesterday.day,0,0,0,0) yesterday_beginning_time = int(time.mktime(yesterday_beginning.timetuple())) yesterday_end = datetime.datetime(yesterday.year, yesterday.month, yesterday.day,23,59,59,999) yesterday_end_time = int(time.mktime(yesterday_end.timetuple())) print yesterday_beginning_time print yesterday_end_time
Here is a simple function to check an array to see if it contains all null values.
function allNulls($arr) {
if(is_array($arr) && count(array_diff($arr, array(null))) == 0) {
return true;
}
return false;
}
echo (allNulls(array(null,null,null)) ? "true" : "false") . PHP_EOL;
echo (allNulls(array(null,1,null)) ? "true" : "false") . PHP_EOL;
echo (allNulls(array("test",null,null)) ? "true" : "false") . PHP_EOL;
echo (allNulls(array("",null,null)) ? "true" : "false") . PHP_EOL;
echo (allNulls(array(0,null,null)) ? "true" : "false") . PHP_EOL;
Simple PHP Proxy
May 24
While developing apps that use external web services, a proxy often comes in handy in order to bypass the pesky XSS security settings found in most browsers. Here is a simple PHP proxy I’ve found quite helpful.
// http://benalman.com/projects/php-simple-proxy/
$url = "http://mcaf.ee/api/shorten";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_MAXCONNECTS, 16);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_REQUEST));
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
if(!is_null($type)) $type = 'text/html';
header('Content-Type: '.$type);
switch($status) {
case 500:
header("HTTP/1.1 500 Internal Server Error");
header("Cache-Control: no-cache");
break;
case 200:
default:
header("HTTP/1.1 200 OK");
break;
}
echo $result;
I’ve been dabbling in Erlang recently. I’ve wanted to learn a functional programming language for a while now and Erlang’s concurrency make it rather attractive.
For my “hello world” app, I decided to write a simple log parser which processes chunks of a file in parallel. Here is a part of that app which produces a list of tuples which describes the chunks adjusted to the nearest newline (Unix newlines, \n, in this case).
getChunkDivisions(File, 0, Chunksize, ChunkDivisions) ->
[{chunk,0,Chunksize}|ChunkDivisions];
getChunkDivisions(File, Size, Chunksize, ChunkDivisions) ->
if
Size-Chunksize=<0 ->
ComputedChunkEnd = Chunksize-(Chunksize-Size),
CorrectedChunkEnd = walkToNextLineBreak(File, ComputedChunkEnd),
getChunkDivisions(File, 0, CorrectedChunkEnd, ChunkDivisions);
true ->
ComputedChunkEnd = Size-Chunksize,
CorrectedChunkEnd = walkToNextLineBreak(File, ComputedChunkEnd),
getChunkDivisions(File, CorrectedChunkEnd, Chunksize, [{chunk,CorrectedChunkEnd,Size}|ChunkDivisions])
end.
walkToNextLineBreak(File,Start) ->
file:position(File, Start-1),
{ok, Data} = file:read(File, 1024),
Start+string:chr(Data, $\n).
On a recent project I need to find all the possible permutations of a given URL. Stripping off subdomains, paths, and query parameters. Here is the first part of the solution. A method which takes a string and strips it down based on a given divider in a given direction at a given interval.
Here’s the code:
private static String[] getPermutations(String whole, String divider, int lim, int dir) {
String[] chunks = whole.split((divider.matches("\\.") ? "\\" : "")+divider);
System.out.println("chunks.length: "+chunks.length);
if(chunks.length <= lim) {
System.out.println("return whole: "+whole);
return new String[]{whole};
}
String[] permutations = new String[chunks.length-lim];
if(dir == 1) {
permutations[0] = whole;
System.out.println("permutations[0]: "+permutations[0]);
for(int i = 1; i < chunks.length-lim; i++) {
String permutation = "";
for(int o = i; o < chunks.length; o++) {
permutation += (o == i ? "" : divider) + chunks[o];
}
permutations[i] = permutation;
System.out.println("permutations["+i+"]: "+permutations[i]);
}
} else if(dir == -1) {
for(int i = 0; i < chunks.length-lim; i++) {
String permutation = "";
for(int o = 0; o < chunks.length-i; o++) {
permutation += (o == 0 ? "" : divider) + chunks[o];
}
permutations[i] = permutation;
System.out.println("permutations["+i+"]: "+permutations[i]);
}
}
return permutations;
}
Here is an example of it being used.
Input:
getPermutations("com",".",1, 1);
getPermutations("google.com",".",1, 1);
getPermutations("a.b.c.d.e.cool.google.com",".",1, 1);
getPermutations("/path/asdf/a/b/c/d/e/f","/",0, -1);
getPermutations("a=b&c=d&e=f","&",0, -1);
Output:
$ javac Runme.java && java Runme chunks.length: 1 return whole: com chunks.length: 2 permutations[0]: google.com chunks.length: 8 permutations[0]: a.b.c.d.e.cool.google.com permutations[1]: b.c.d.e.cool.google.com permutations[2]: c.d.e.cool.google.com permutations[3]: d.e.cool.google.com permutations[4]: e.cool.google.com permutations[5]: cool.google.com permutations[6]: google.com chunks.length: 9 permutations[0]: /path/asdf/a/b/c/d/e/f permutations[1]: /path/asdf/a/b/c/d/e permutations[2]: /path/asdf/a/b/c/d permutations[3]: /path/asdf/a/b/c permutations[4]: /path/asdf/a/b permutations[5]: /path/asdf/a permutations[6]: /path/asdf permutations[7]: /path permutations[8]: chunks.length: 3 permutations[0]: a=b&c=d&e=f permutations[1]: a=b&c=d permutations[2]: a=b
Next I'll need to combine the URL components to get a list of valid URLs..
Here is a color calculator I adapted from a friend’s PHP implementation:
Color = function() {
};
Color.hexdec = function(hex_string) {
hex_string = (hex_string + '').replace(/[^a-f0-9]/gi, '');
return parseInt(hex_string, 16);
}
Color.dechex = function(number) {
if (number < 0) {
number = 0xFFFFFFFF + number + 1;
}
return parseInt(number, 10).toString(16);
}
Color.pad = function(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
return str;
}
Color.calcgrad = function(val, color1, color2) {
if(!color1.match(/^#[0-9a-f]{6}/) || !color2.match(/^#[0-9a-f]{6}/)) return 'match err!';
if (val > 1) {
val = 1;
}
if (val < 0) {
val = 0;
}
val = parseFloat(val);
c1 = [Color.hexdec(color1.substr(1,2)), Color.hexdec(color1.substr(3,2)), Color.hexdec(color1.substr(5,2))]; //b
c2 = [Color.hexdec(color2.substr(1,2)), Color.hexdec(color2.substr(3,2)), Color.hexdec(color2.substr(5,2))]; //r
if (val < .5) {
delta = [(c2[0] - c1[0]), (c2[1] - c1[1]), (c1[2] - c2[2])];
arrColor = [c1[0] +1 * 2), c1[1] -2 * 2), c1[2] -3 * 2)];
}
return '#'+Color.pad(Color.dechex(arrColor[0]),2)+Color.pad(Color.dechex(arrColor[1]),2)+Color.pad(Color.dechex(arrColor[2]),2);
}

