-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hooked Main into config, this works.
development.scala is a config file appropriate for development (TRACE and such). serviceClient.rb is a little command line wrapper for interacting with the service.
- Loading branch information
Showing
6 changed files
with
127 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import net.mobocracy.starter.config._ | ||
import com.twitter.conversions.time._ | ||
import com.twitter.logging.config._ | ||
import com.twitter.ostrich.admin.config._ | ||
|
||
new StarterServiceConfig { | ||
port = 4242 | ||
name = "StarterService" | ||
|
||
admin.httpPort = 9900 | ||
|
||
admin.statsNodes = new StatsConfig { | ||
reporters = new JsonStatsLoggerConfig { | ||
loggerName = "stats" | ||
serviceName = name | ||
} :: new TimeSeriesCollectorConfig | ||
} | ||
|
||
loggers = | ||
new LoggerConfig { | ||
level = Level.TRACE | ||
handlers = new ConsoleHandlerConfig { | ||
formatter = new FormatterConfig { | ||
useFullPackageNames = true | ||
prefix = "%s [<yyyyMMdd-HH:mm:ss.SSS>] %s: " | ||
} | ||
} | ||
} :: new LoggerConfig { | ||
node = "stats" | ||
level = Level.FATAL | ||
useParents = false | ||
handlers = new ConsoleHandlerConfig | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/ruby | ||
|
||
require 'socket' | ||
require 'optparse' | ||
|
||
options = { | ||
:port => 4242 | ||
} | ||
opts = OptionParser.new do |opts| | ||
opts.banner = "Usage: serviceClient.rb [option] text" | ||
|
||
opts.on("-p", "--port POT", Integer, "Use the specified port for connecting to the service") do |port| | ||
options[:port] = port.to_i | ||
end | ||
end | ||
|
||
def client(data, port = PORT) | ||
begin | ||
sock = TCPsocket.new('127.0.0.1', port) | ||
sock.write(data + "\n") | ||
reply = sock.readline | ||
puts("Sent: #{data}") | ||
puts("Received: #{reply}") | ||
sock.close | ||
rescue Exception => e | ||
puts("Caught exception sending data, is the server listening?") | ||
puts("Exception: #{e.message}") | ||
end | ||
end | ||
|
||
opts.parse! | ||
if ( ARGV.length == 0 ) then | ||
puts(opts) | ||
exit | ||
else | ||
client(ARGV[0], options[:port]) | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
package net.mobocracy.starter | ||
|
||
import com.twitter.logging.Logger | ||
import com.twitter.ostrich.admin.{RuntimeEnvironment, ServiceTracker} | ||
|
||
object Main { | ||
val log = Logger.get(getClass) | ||
|
||
def main(args: Array[String]) { | ||
println("Hello World") | ||
System.exit(0) | ||
val runtime = RuntimeEnvironment(this, args) | ||
val server = runtime.loadRuntimeConfig[StarterServiceServer] | ||
try { | ||
log.info("Starting service") | ||
server.start() | ||
} catch { | ||
case e: Exception => | ||
log.error(e, "Failed starting service, exiting") | ||
ServiceTracker.shutdown() | ||
System.exit(1) | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,50 @@ | ||
package net.mobocracy.starter | ||
|
||
import java.net.InetSocketAddress | ||
import java.security.MessageDigest | ||
|
||
import com.twitter.finagle.Service | ||
import com.twitter.finagle.builder.{Server, ServerBuilder} | ||
import com.twitter.finagle.stats.OstrichStatsReceiver | ||
import com.twitter.logging.Logger | ||
import com.twitter.ostrich.stats.Stats | ||
import com.twitter.util.Future | ||
|
||
trait StarterService { | ||
val port: Int | ||
val name: String | ||
var server: Option[Server] = None | ||
|
||
val service = new Service[String, String] { | ||
def apply(request: String) = Future.value(request) | ||
} | ||
val log = Logger.get(getClass) | ||
|
||
val serverSpec = ServerBuilder() | ||
// Don't initialize until after mixed in by another class | ||
lazy val serverSpec = ServerBuilder() | ||
.codec(StringCodec()) | ||
.bindTo(new InetSocketAddress(port)) | ||
.name(name) | ||
.reportTo(new OstrichStatsReceiver) | ||
|
||
val service = new Service[String, String] { | ||
def apply(request: String) = { | ||
log.debug("Got request") | ||
Stats.incr("service_request_count") | ||
|
||
val hashedRequest = Stats.time("request_hash") { | ||
getStringHash(request) | ||
} | ||
|
||
request match { | ||
case "please throw an exception" => | ||
Future.exception(new Exception("Asked to throw, don't blame me")) | ||
case _ => | ||
Future.value(request + "\t" + hashedRequest + "\n") | ||
} | ||
} | ||
} | ||
|
||
protected[this] def getStringHash(s: String): String = { | ||
val hasher = MessageDigest.getInstance("SHA-512") | ||
hasher.update(s.getBytes("UTF-8")) | ||
hasher.digest.map { "%02x" format _ }.mkString | ||
} | ||
} |
This file contains 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