Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Add call to Starport Lambda to clear /tmp (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkbudd authored Nov 27, 2020
1 parent 53ccdf2 commit a8de708
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import java.io.{BufferedOutputStream, File, FileOutputStream}
import java.net.URI

import com.amazonaws.services.s3.AmazonS3ClientBuilder

import com.krux.starport.Logging


object S3FileHandler extends Logging {

val bufferSize = 1024
final val TmpDirectory = "/tmp/starport"

def getTempDirectory(baseDir: Option[String]): File = {
if (Lambda.isLambda()) {
new File("/tmp")
new File(TmpDirectory)
} else {
// TODO change this to "/mnt/tmp/starport"
// and delete the file afterwards
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ package com.krux.starport.lambda
import java.io.{ByteArrayOutputStream, File, PrintStream}

import com.amazonaws.services.lambda.runtime.{Context, RequestHandler}

import com.krux.starport.Logging
import com.krux.starport.db.tool.SubmitPipeline
import com.krux.starport.util.LambdaExitException
import com.krux.starport.util.{LambdaExitException, S3FileHandler}

/**
* Lambda to invoke the com.krux.starport.db.tool.SubmitPipeline util remotely.
*
* Property configuration for lambda (pass via JAVA_TOOL_OPTIONS env var):
*
* -Dstarport.config.url=s3://{your-bucket}/starport/starport.conf
* -Dlogger.root.level=DEBUG|INFO|...
* -Dexecution.context=lambda
*
*/
* Lambda to invoke the com.krux.starport.db.tool.SubmitPipeline util remotely.
*
* Property configuration for lambda (pass via JAVA_TOOL_OPTIONS env var):
*
* -Dstarport.config.url=s3://{your-bucket}/starport/starport.conf
* -Dlogger.root.level=DEBUG|INFO|...
* -Dexecution.context=lambda
*/
class SubmitHandler extends RequestHandler[SubmitRequest, SubmitResponse] with Logging {
val outCapture = new ByteArrayOutputStream
val errCapture = new ByteArrayOutputStream
Expand All @@ -33,41 +31,58 @@ class SubmitHandler extends RequestHandler[SubmitRequest, SubmitResponse] with L
var outString = ""
var errString = ""

try {
SubmitPipeline.main(input.getArgs)
} catch {
case caughtExit: LambdaExitException => {
status = caughtExit.status
logger.error("exit:", caughtExit)
logger.error(scanTmpFiles())
}
case unhandled: Throwable => {
status = 255
logger.error("exception:", unhandled)
logger.error(scanTmpFiles())
}
} finally {
outPrintStream.flush()
errPrintStream.flush()
outString = outCapture.toString
errString = errCapture.toString
outCapture.reset()
errCapture.reset()
lambdaOut.print(outString)
lambdaErr.print(errString)
}
val args: Array[String] = input.getArgs

try {
args.head match {
case "deleteTmpDir" => {
status = 255
logger.error(s"received call to delete ${S3FileHandler.TmpDirectory}")
logger.error(deleteTmpDir())
}
case _ => SubmitPipeline.main(args)
}
} catch {
case caughtExit: LambdaExitException => {
status = caughtExit.status
logger.error("exit:", caughtExit)
logger.error(scanTmpFiles())
}
case unhandled: Throwable => {
status = 255
logger.error("exception:", unhandled)
logger.error(scanTmpFiles())
}
} finally {
outPrintStream.flush()
errPrintStream.flush()
outString = outCapture.toString
errString = errCapture.toString
outCapture.reset()
errCapture.reset()
lambdaOut.print(outString)
lambdaErr.print(errString)
}

SubmitResponse(outString, errString, status, input)

SubmitResponse(outString, errString, status, input)
}

/**
* @return /tmp file listing with size for troubleshooting purposes
*/
private def scanTmpFiles(): String = {
new File("/tmp")
new File(S3FileHandler.TmpDirectory)
.listFiles()
.map(f => s"${f.getAbsolutePath}: ${f.length()}")
.mkString("\n")
}
}

/**
* @return delete the /tmp directory
*/
private def deleteTmpDir(): String = {
new File(S3FileHandler.TmpDirectory).delete
s"${S3FileHandler.TmpDirectory} deleted."
}
}
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "7.3.0"
version in ThisBuild := "7.4.0"

0 comments on commit a8de708

Please sign in to comment.