Skip to content

Commit

Permalink
vals > vars, replaced a couple check-and-get conditionals with getOrElse
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Andreas committed Oct 2, 2010
1 parent ec655e3 commit 330b7b5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 32 deletions.
5 changes: 3 additions & 2 deletions project/build.properties
@@ -1,8 +1,9 @@
#Project properties
#Wed Sep 29 21:20:19 PDT 2010
#Project Properties
#Wed Sep 29 23:36:32 PDT 2010
project.organization=Urban Airship
project.name=Octobot
sbt.version=0.7.4
scala.version=2.8.0
project.version=1.0
build.scala.versions=2.8.0
project.initialize=false
1 change: 0 additions & 1 deletion src/main/scala/com/urbanairship/octobot/Beanstalk.scala
Expand Up @@ -33,7 +33,6 @@ object Beanstalk {
}
}

// This code can never be reached, but the compiler wants it.
client
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/main/scala/com/urbanairship/octobot/Metrics.scala
Expand Up @@ -41,8 +41,8 @@ object Metrics {
executionTimes.put(task, timeList)
} else {
val timeList = executionTimes.get(task)

if (timeList.size() == 10000) timeList.removeLast()

timeList.addFirst(time)
executionTimes.put(task, timeList)
}
Expand All @@ -55,10 +55,8 @@ object Metrics {
if (!taskRetries.containsKey(task)) {
taskRetries.put(task, retries)
} else {
var retriesForTask = taskRetries.get(task)

retriesForTask += retries
taskRetries.put(task, retriesForTask)
val retriesForTask = taskRetries.get(task)
taskRetries.put(task, retriesForTask + retries)
}
}
}
Expand All @@ -70,14 +68,14 @@ object Metrics {
if (!taskSuccesses.containsKey(task)) {
taskSuccesses.put(task, 1)
} else {
var success = taskSuccesses.get(task)
val success = taskSuccesses.get(task)
taskSuccesses.put(task, success + 1)
}
} else {
if (!taskFailures.containsKey(task)) {
taskFailures.put(task, 1)
} else {
var failure = taskFailures.get(task)
val failure = taskFailures.get(task)
taskFailures.put(task, failure + 1)
}
}
Expand Down
26 changes: 14 additions & 12 deletions src/main/scala/com/urbanairship/octobot/Octobot.scala
Expand Up @@ -20,18 +20,18 @@ object Octobot {
if (configFile != null && !configFile.equals("")) {
PropertyConfigurator.configure(configFile)
} else {
BasicConfigurator.configure()
BasicConfigurator.configure()
logger.warn("log4j.configuration not set - logging to stdout.")
}

// If a startup hook is configured, call it before launching workers.
val startupHook = Settings.get("Octobot", "startup_hook")
if (startupHook != null && !startupHook.equals(""))
if (startupHook != null)
launchStartupHook(startupHook)

// If a shutdown hook is configured, register it.
val shutdownHook = Settings.get("Octobot", "shutdown_hook")
if (shutdownHook != null && !shutdownHook.equals(""))
if (shutdownHook != null)
registerShutdownHook(shutdownHook)

val enableEmailErrors = Settings.getAsBoolean("Octobot", "email_enabled")
Expand All @@ -44,13 +44,15 @@ object Octobot {
new Thread(new Introspector(), "Introspector").start()

logger.info("Launching Workers...")
var queues: List[HashMap[String, Any]] = null
try {
queues = getQueues()
} catch {
case ex : NullPointerException => {
logger.fatal("Error: No valid queues found in Settings. Exiting.")
throw new Error("Error: No valid queues found in Settings. Exiting.")

val queues: List[HashMap[String, Any]] = {
try {
getQueues()
} catch {
case ex : NullPointerException => {
logger.fatal("Error: No valid queues found in Settings. Exiting.")
throw new Error("Error: No valid queues found in Settings. Exiting.")
}
}
}

Expand All @@ -64,8 +66,8 @@ object Octobot {

// Spawn worker threads for each queue in our configuration.
for (i <- 0 until numWorkers) {
var consumer = new QueueConsumer(queue)
var worker = new Thread(consumer, "Worker")
val consumer = new QueueConsumer(queue)
val worker = new Thread(consumer, "Worker")

logger.info("Attempting to connect to " + queueConf.get("protocol") +
" queue: " + queueConf.get("name") + " with priority " +
Expand Down
18 changes: 8 additions & 10 deletions src/main/scala/com/urbanairship/octobot/TaskExecutor.scala
Expand Up @@ -6,19 +6,17 @@ import org.json.JSONObject

object TaskExecutor {
val taskCache = new mutable.HashMap[String, Method]
val argClass = new JSONObject().getClass

def execute(taskName: String, message: JSONObject) {
var method: Method = null

if (taskCache.contains(taskName)) {
// TODO refactor as getOrElse
method = taskCache.get(taskName).get
} else {
val task = Class.forName(taskName)
val klass = new JSONObject().getClass

method = task.getMethod("run", klass)
taskCache.put(taskName, method)
val method: Method = {
taskCache.getOrElse(taskName, {
val task = Class.forName(taskName)
val meth = task.getMethod("run", argClass)
taskCache.put(taskName, meth)
meth
})
}

method.invoke(null, message)
Expand Down

0 comments on commit 330b7b5

Please sign in to comment.