Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into sbt11
Browse files Browse the repository at this point in the history
Conflicts:
	VERSION
	bin/scala-bootstrapper
	lib/template/project/build/BirdNameProject.scala.erb
	lib/template/project/plugins/Plugins.scala.erb
	lib/template/src/main/scala/com/twitter/birdname/Main.scala.erb
	scala-bootstrapper.gemspec
  • Loading branch information
Robey Pointer committed Mar 29, 2012
2 parents 1c2f354 + 81d7f41 commit 9c70c6e
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 167 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2009 Kyle Maxwell
Copyright (c) 2009-2011 Twitter, Inc.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
3 changes: 3 additions & 0 deletions OWNERS
@@ -0,0 +1,3 @@
kmaxwell
robey
review_group:Scala-Cafe
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.10.0
0.10.0
8 changes: 6 additions & 2 deletions bin/scala-bootstrapper
Expand Up @@ -88,7 +88,7 @@ end

root = File.expand_path(File.dirname(__FILE__) + "/../lib/template")

Dir["#{root}/**/*"].select{|path| File.file?(path)}.each do |path|
Dir["#{root}/**/*"].select { |path| File.file?(path) }.each do |path|
relative = path.sub("#{root}/", "")
content = File.read(path)
template = ERB.new(content, nil, nil, "@output")
Expand Down Expand Up @@ -121,7 +121,11 @@ if File.exists?("src/scripts/startup.sh")
$files << startup
end

[ "src/scripts/#{project_name.downcase}.sh", "console", "run" ].each do |executable|
[
"src/scripts/#{project_name.downcase}.sh",
"src/scripts/devel.sh",
"console",
].each do |executable|
`chmod +x #{executable}` if File.exists?(executable)
end

Expand Down
8 changes: 4 additions & 4 deletions lib/template/TUTORIAL.md
Expand Up @@ -137,10 +137,10 @@ we just added.

"verify stats" in {
val counters = Stats.getCounters
foofa.put("name", "bluebird")()
foofa.get("name")() mustEqual "bluebird"
counters.getOrElse("foofa.gets", 1) must_==1
counters.getOrElse("foofa.puts", 1) must_==1
birdname.put("name", "bluebird")()
birdname.get("name")() mustEqual "bluebird"
counters.getOrElse("birdname.gets", 1) must_==1
counters.getOrElse("birdname.puts", 1) must_==1
}

TODO: add link to scala school lesson on Specs
Expand Down
2 changes: 1 addition & 1 deletion lib/template/run
Expand Up @@ -103,7 +103,7 @@ done

if [ $# -eq 0 ]; then
echo "usage: $0 [-i INCLUDE] [-h HEAPSTER] [-ygd] CLASS ..." >&2
echo " -i INCLUDE include in the classpath the project with the sbt root INCLUDE" >&2
echo " -i INCLUDE include in the classpath the project with the sbt root INCLUDE" >&2
echo " -h HEAPSTER use heapster in the directory HEAPSTER" >&2
echo " -y enable yourkit debugging" >&2
echo " -g enable GC debugging/logging" >&2
Expand Down
@@ -1,21 +1,23 @@
package com.twitter.birdname

import com.twitter.logging.Logger
import com.twitter.util._
import java.util.concurrent.Executors
import scala.collection.mutable
import com.twitter.util._
import config._
import thrift._

class BirdNameServiceImpl(config: BirdNameServiceConfig) extends BirdNameService.ThriftServer {
val serverName = "BirdName"
val thriftPort = config.thriftPort
override val tracerFactory = config.tracerFactory

/**
* These services are based on finagle, which implements a nonblocking server. If you
* are making blocking rpc calls, it's really important that you run these actions in
* a thread pool, so that you don't block the main event loop. This thread pool is only
* needed for these blocking actions. The code looks like:
*
* // Depends on com.twitter.util >= 1.6.10
* val futurePool = new FuturePool(Executors.newFixedThreadPool(config.threadPoolSize))
*
* def hello() = futurePool {
Expand Down
@@ -1,11 +1,12 @@
package com.twitter.birdname

import com.twitter.logging.Logger
import com.twitter.ostrich.admin.{RuntimeEnvironment, ServiceTracker}
import com.twitter.ostrich.admin.RuntimeEnvironment
import thrift._

object Main {
val log = Logger.get(getClass)
private val log = Logger.get(getClass)

def main(args: Array[String]) {
val runtime = RuntimeEnvironment(this, args)
val server = runtime.loadRuntimeConfig[BirdNameService.ThriftServer]
Expand All @@ -20,4 +21,3 @@ object Main {
}
}
}

@@ -1,14 +1,17 @@
package com.twitter.birdname
package config

import com.twitter.finagle.tracing.{NullTracer, Tracer}
import com.twitter.logging.Logger
import com.twitter.logging.config._
import com.twitter.ostrich.admin.{RuntimeEnvironment, ServiceTracker}
import com.twitter.ostrich.admin.config._
import com.twitter.util.Config
import thrift._

class BirdNameServiceConfig extends ServerConfig[BirdNameService.ThriftServer] {
var thriftPort: Int = 9999
var tracerFactory: Tracer.Factory = NullTracer.factory

def apply(runtime: RuntimeEnvironment) = new BirdNameServiceImpl(this)
}
2 changes: 2 additions & 0 deletions lib/template/src/scripts/birdname.sh
@@ -0,0 +1,2 @@
#!/bin/sh
sh `dirname "$0"`/service.sh "$@"
23 changes: 23 additions & 0 deletions lib/template/src/scripts/config.sh
@@ -0,0 +1,23 @@
#!/bin/sh

export APP_NAME="birdname"
export APP_HOME="/usr/local/$APP_NAME/current"

export PIDFILE="/var/run/$APP_NAME/$APP_NAME.pid"
export STDOUT_FILE="/var/log/$APP_NAME/stdout"
export STDERR_FILE="/var/log/$APP_NAME/error"

export MAIN_JAR="$APP_NAME-1.0-SNAPSHOT.jar"
export ADMIN_PORT=9900

export HEAP_OPTS="-Xmx4096m -Xms4096m -XX:NewSize=768m"
export GC_OPTS="-XX:+UseParallelOldGC -XX:+UseAdaptiveSizePolicy -XX:MaxGCPauseMillis=1000 -XX:GCTimeRatio=99"

export STAGE=production
export EXTRA_JAVA_OPTS="-Dstage=$STAGE"

# you can also define the following functions to override behavior:
# running - checks whether the process is running
# start - executed after the process is verified to not be running and a start was requested
# stop - executed after the process is verified to be running and a stop was requested
# stopped - is passed the PID before ths top and checks if its still running
3 changes: 3 additions & 0 deletions lib/template/src/scripts/devel.sh
@@ -0,0 +1,3 @@
#!/bin/bash
echo "Starting birdname in development mode..."
java -server -Xmx1024m -Dstage=development -jar ./dist/birdname/birdname-@VERSION@.jar
31 changes: 31 additions & 0 deletions lib/template/src/scripts/server.sh
@@ -0,0 +1,31 @@
#!/bin/sh
## this file encapsulates starting the server including the JVM options
## and putting the pid into the pidfile. this is executed by service.sh
## on the start action.
## it is an expectation that this be run in your application home directory.

SCRIPT_DIR=`dirname $0`
source "$SCRIPT_DIR/config.sh"

GC_LOGGING_OPTS="-verbosegc -Xloggc:/var/log/$APP_NAME/gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+PrintHeapAtGC"
JAVA_OPTS="-server -XX:+DisableExplicitGC -XX:+UseNUMA $GC_OPTS $GC_LOGGING_OPTS $HEAP_OPTS $EXTRA_JAVA_OPTS"

if [ -z $APP_HOME ]; then
APP_HOME=`pwd`
fi

if [ -z $PIDFILE ]; then
PIDFILE="/var/run/$APP_NAME/$APP_NAME.pid"
fi

if [ ! -f "$APP_HOME/$MAIN_JAR" ]; then
echo "jar not found at $APP_HOME/$MAIN_JAR"
exit 1
fi

if [ -z $JAVA_HOME ]; then
JAVA_HOME="/usr/java/default"
fi

echo $$ > $PIDFILE
exec ${JAVA_HOME}/bin/java ${JAVA_OPTS} -jar ${APP_HOME}/${MAIN_JAR} ${CMD_OPTS}
116 changes: 116 additions & 0 deletions lib/template/src/scripts/service.sh
@@ -0,0 +1,116 @@
#!/bin/sh
## this file is the control script for your jvm service.
## it is executed by monit with an argument of either start, stop or restart
## it is not responsible for options specific to the jvm but does background,
## disown, and redirect output.

function start() {
cd $APP_HOME
$START_SCRIPT > $STDOUT_FILE 2> $STDERR_FILE &
disown %1

sleep $INITIAL_SLEEP # give it time to compile config files and bind to ports
}

function running() {
curl -m 5 -s "http://localhost:$ADMIN_PORT/ping.txt" > /dev/null 2> /dev/null
}

function stop() {
kill -TERM $(cat $PIDFILE)
}

function stopped() {
! kill -0 $1 > /dev/null 2> /dev/null
}

SCRIPT_DIR=`dirname $0`
source "$SCRIPT_DIR/config.sh"
START_SCRIPT="sh $SCRIPT_DIR/server.sh"

if [ -z $INITIAL_SLEEP ]; then
INITIAL_SLEEP=3
fi

if [ -z $MAX_START_TRIES ]; then
MAX_START_TRIES=5
fi

case "$1" in
start)
printf "Starting %s... " "$APP_NAME"

if running; then
echo "already running."
exit 0
fi

start

tries=0
while ! running; do
tries=$((tries + 1))
if [ $tries -ge $MAX_START_TRIES ]; then
echo "ERROR: failed to start"
exit 1
fi
sleep 1
done
echo "done."
;;

stop)
printf "Stopping %s... " "$APP_NAME"

if [ ! -f $PIDFILE ]; then
GUESSED_PID=`pgrep -u $USER java`
if [ -z $GUESSED_PID ]; then
echo "ERROR: no pid file and no java process found"
else
echo "ERROR: no pid file, but found java process: $GUESSED_PID"
fi
exit 1
fi

SNAPSHOT_PID=$(cat $PIDFILE)

if $(stopped $SNAPSHOT_PID); then
echo "already stopped."
exit 0
fi

stop

tries=0
while ! $(stopped $SNAPSHOT_PID); do
tries=$((tries + 1))
if [ $tries -ge 5 ]; then
echo "ERROR: failed to stop"
exit 1
fi
sleep 1
done
echo "done."
;;

status)
if running; then
echo "$APP_NAME is running."
else
echo "$APP_NAME is NOT running."
fi
;;

restart)
$0 stop
sleep 2
$0 start
;;

*)
echo "Usage: /etc/init.d/$APP_NAME {start|stop|restart|status}"
exit 1
;;
esac

exit 0

0 comments on commit 9c70c6e

Please sign in to comment.