TimL TimL -4 years ago 85
R Question

Return Text Status From Function

I am trying to create a function that will do a couple of things. First it will check a couple of tables to see if workflows have been completed or not and then return a status message one way or the other. This is easy enough. However there is an issue that if the ETL's are disrupted the status table does not get updated. As a result if the script is kicked off manually it will fail.

I want to insert logic into the function to check the system time and if it is after 7:00 AM, skip the check and run the rest of the script. Here is what I have right now. It works fine after my cut-off time, but when I set the variable to less than seven for testing I get no status message back.

run_time <- as.numeric(format(Sys.time(),"%H"));
run_time <- 4

wrkflw_check <- function(m) {
bk <- 36
msg <- "stoped checking"

if (run_time > 7) {
msg <- "Complete"
}
else {
# loop until workflows complete or 3 hours. Which ever comes first
for (i in 1:bk) {
if ((etl_check$status == "wait") | (dl_check$status == "wait")) {
Sys.sleep(300)
etl_status <- paste0("etl status: ", etl_check$status)
dl_status <- paste0(" dl status: ", dl_check$status)
print(etl_status)
print(dl_status)
etl_check <- dbGetQuery(fm01, etl_sql)
dl_check <- dbGetQuery(dl, dl_sql)
i <- i + 1
} else {
i <- bk
msg <- "Complete"
}
}
}}

msg <- wrkflw_check(m);

etl_check <- dbGetQuery(fm01, etl_sql)
dl_check <- dbGetQuery(dl, dl_sql)


What I need to get back is the msg variable.

Answer Source

I had to break things up a little to get what I wanted. But this works now.

wrkflw_check <- function(m){
    bk <- 36
    msg <- "stoped checking"
# loop until workflows complete or 3 hours. Which ever comes first
    for (i in 1:bk) {
        if ((etl_check$status == "wait") | (dl_check$status == "wait")) {
            Sys.sleep(300)
            etl_status <- paste0("etl status: ", etl_check$status)
            dl_status <- paste0(" dl status: ", dl_check$status)
            print(etl_status)
            print(dl_status)
            etl_check <- dbGetQuery(fm01, etl_sql)
            dl_check <- dbGetQuery(dl, dl_sql)
            i <- i + 1
        } else {
            i <- bk
            msg <- "Complete"
            return(msg)
        }
    }
}


run_time <- as.numeric(format(Sys.time(),"%H"));
#run_time <- 4
run_time

if (run_time > 7) {
    msg <- "Complete"
} else {
    msg <- "incomplete"
    msg <- wrkflw_check(m)
};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download