R/handleSGfindtags.R

Defines functions handleSGfindtags

Documented in handleSGfindtags

#' handle the job of running the tag finder on one sensorgnome boot session
#'
#' Called by \code{\link{processServer}}.
#'
#' Runs the tag finder for one boot session of a single SG.
#' The files for that boot session have already been merged into
#' the DB for that receiver.
#'
#' @param j the job, with these properties:
#' \itemize{
#' \item serno serial number of receiver (including leading 'SG-')
#' \item monoBN monotonic boot session number
#' \item boolean TRUE or FALSE; can the tag finder be run with \code{--resume}?
#' }
#'
#' @return TRUE on success, or FALSE if the tag finder has an error.
#'
#' @note if topJob(j) contains fields motusUserID and motusProjectID, these are
#' recorded in the receiver database for the batch generated by this call.
#'
#' @note if topJob(j) contains the field "paramOverrides", then it is treated
#' as a string of parameter overrides for the tag finder.
#'
#' @seealso \code{\link{server}}
#'
#' @export
#'
#' @author John Brzustowski \email{jbrzusto@@REMOVE_THIS_PART_fastmail.fm}

handleSGfindtags = function(j) {

    serno = j$serno

    jobLog(j, paste0("Running tag finder on receiver ", serno, " boot session ", j$monoBN, if (j$canResume) " (resumed)"))

    if (updateTagDeployments())
        jobLog(j, "Some tag deployment metadata were updated from motus.org")

    ## lock this receiver's DB

    lockSymbol(serno)

    ## make sure we unlock the receiver DB when this function exits, even on error
    ## NB: the runMotusProcessServer script also drops any locks held by a given
    ## processServer after the latter exits.

    on.exit(lockSymbol(serno, lock=FALSE))

    src = getRecvSrc(serno)

    tj = topJob(j)

    ## get registered parameter overrides
    por = getParamOverrides(serno, monoBN = j$monoBN, motusProjectID = tj$motusProjectID)

    ## get job-specific parameter overrides
    por2 = tj$paramOverrides
    if (isTRUE(nchar(por2) > 0)) {
        por = paste(por, por2)
    }

    if (por != "")
        jobLog(j, paste0("Got parameter overrides: ", por))

    rv = NULL
    ## run the tag finder
    tryCatch({
        rv = sgFindTags(src, MOTUS_METADB_CACHE, resume=j$canResume, par = paste(sgDefaultFindTagsParams, por), mbn=j$monoBN)
        if (grepl("--pulses_only", por, fixed=TRUE))
            newSubJob(j, "exportPulses", serno=serno, batchID=rv$batchID)
    }, error = function(e) {
        jobLog(j, paste(as.character(e), collapse="   \n"))
    })

    if (! is.null(rv)) {
        ## record motus user and project IDs for this batch, which may
        ## be present in the top job (typically an "uploadFile" job)

        muid = tj$motusUserID
        mpid = tj$motusProjectID
        if (!isTRUE(muid > 0))
            muid = "null"
        if (!isTRUE(mpid > 0))
            mpid = "null"
        dbGetQuery(src$con, sprintf("update batches set motusUserID=%s, motusProjectID=%s, motusJobID=%d where batchID=%d", muid, mpid, as.integer(j), rv$batchID))
    }

    closeRecvSrc(src)

    if (is.null(rv))
        return(FALSE)

    jobLog(j, paste0("Got ", rv$numHits, " (unfiltered) detections."))
    return(TRUE)
}
jbrzusto/motusServer documentation built on May 19, 2019, 8:19 a.m.