Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
# Load in the PrimalAccess class
require './PrimalAccess.rb'
require 'rubygems'
# We require this particular gem
#
# To install it:
# gem install json
#!/usr/bin/env ruby
# We need to use some ruby gems
require 'rubygems'
# We require the httparty gem
#
# To install it:
# gem install httparty
#
% node -v
v0.6.12
% node
> [1,2,3,11,22].sort()
[ 1,
11,
2,
22,
3 ]
> console.log("That's just awesome")
@derekwyatt
derekwyatt / execOnChange.sh
Created September 1, 2012 15:32
Executes an arbitrary command when files change.
#!/bin/bash
command="$1"
shift
fileSpec="$@"
sentinel=/tmp/t.$$
touch -t197001010000 $sentinel
while :
do
@derekwyatt
derekwyatt / FutureShutdown.scala
Created August 18, 2012 17:49
Shutdown pattern
// Spawn your futures
val fs = (1 to 100).map { i =>
Future { Thread.sleep(i); i }
}
// Wrap all of the work up into a single
// Future
val f = Future.sequence(fs)
// Wait on it forever - i.e. until it's done
@derekwyatt
derekwyatt / Master.scala
Created August 16, 2012 14:10
The Terminator pattern
import akka.actor.{Actor, ActorRef}
class Master(surrogate: ActorRef) extends Actor {
import Terminator._
// Askf for the kids
override def preStart() {
surrogate ! GetChildren(self)
}
@derekwyatt
derekwyatt / FutureSequence.scala
Created August 7, 2012 19:46
Using a Future.sequence() over the Master / Worker remote node pattern
// Downloading could take a while :)
implicit val askTimeout = Timeout(5 minutes)
// Some list of URLs we want to download
val urls = List(url1, ulr2, url3, ..., urlN)
// The Master
val m = system.actorOf(Props[Master], "master")
// Assume Workers are now present
@derekwyatt
derekwyatt / TestWorkerTest.scala
Created August 7, 2012 18:45
Test of the TestWorker in the Master / Worker remote node pattern
class WorkerSpec extends TestKit(ActorSystem("WorkerSpec"))
with ImplicitSender
with WordSpec
with BeforeAndAfterAll
with MustMatchers {
override def afterAll() {
system.shutdown()
}
@derekwyatt
derekwyatt / TestWorker.scala
Created August 7, 2012 18:40
Business Logic Worker of the Master / Worker remote node pattern
class TestWorker(masterLocation: ActorPath) extends Worker(masterLocation) {
// We'll use the current dispatcher for the execution context.
// You can use whatever you want.
implicit val ec = context.dispatcher
def doWork(workSender: ActorRef, msg: Any): Unit = {
Future {
workSender ! msg
WorkComplete("done")
} pipeTo self
@derekwyatt
derekwyatt / Worker.scala
Created August 7, 2012 18:38
Worker of the Master / Worker remote node pattern
abstract class Worker(masterLocation: ActorPath)
extends Actor with ActorLogging {
import MasterWorkerProtocol._
// We need to know where the master is
val master = context.actorFor(masterLocation)
// This is how our derivations will interact with us. It
// allows dervations to complete work asynchronously
case class WorkComplete(result: Any)