退出后如何保持闪亮的应用程序运行其代码?

时间:2016-12-25 10:24:00

标签: r shiny

我有一个应用程序,在接近24小时的最后期限购买产品后会向客户发送大量电子邮件,以便不知道该产品的客户可以有时间购买它。这是server.R代码的一部分。此代码检查当前时间(使用lubridate库中的now()函数)和end_time(在数据库中)之间的时间差,以查看它是否为24小时或更短时间。如果是这样,它应该向客户发送电子邮件。当程序关闭时,程序似乎不会这样做。它似乎检查时差并仅在程序打开并且由某人访问时发送电子邮件。我应该让now()反应吗?这会让程序在完全没人的情况下完成工作吗?我让'recent_email'反应过来,希望我能做到这一点,但事实并非如此。

recent_email <- reactive({

    # Connects to database

    invalidateLater(90000,session)

    con <- dbConnect(drv = MySQL(),user="username",password="password",dbname="my_db",host="my_server_name")

    # Loads FA_TABLE from mysql

    tbl <- dbReadTable(con,"FA_TABLE",row.names=NULL)

    # Subsets the tbl by desired columns and saves it to tbl7

    tbl7 <- tbl[,c("Player","Bid_Num","Start_Time","End_Time","Points")]

    # Get a list of all FA names

    name_list <- unique(tbl7$Player)

    # Vector that keeps the row of FA with highest bid_number

    retain_row <- vector()

    # For loop that will assign the row to retain to the vector called 'retain_row'

    for(k in 1:length(name_list))
    {
      max_bid <- max(tbl7$Bid_Num[tbl7$Player %in% name_list[k]],na.rm = TRUE)

      retain_row[k] <- which((tbl7$Bid_Num == max_bid) & (tbl7$Player == name_list[k]))
    }

    # Subsets the tbl7 by row number saved on "retain_row"

    tbl7 <- tbl7[retain_row,]

    # Create column called "Time_Left"

    tbl7$Time_Left <- ""

    # If Bid_Num is more than 1, add time left. If Bid_Num is not more than 1, Then add "NA"

    for(l in 1:nrow(tbl7))
    {
      ifelse(tbl7$Bid_Num[l] > 1, tbl7$Time_Left[l] <- round(as.numeric(as.POSIXct(tbl7$End_Time[l]),units="sec") - as.numeric(as.POSIXct(now(tzone="EST")),units="sec"),digits=0) + 18000,tbl7$Time_Left[l] <- "NA")
    }

    # Remove row with NA value in Time Left column

    tbl7 <- tbl7[!tbl7$Time_Left %in% c(NA,"NA"),]

    tbl7$Time_Left <- as.numeric(tbl7$Time_Left)

    # Read "clock" table from mysql server

    clock <- dbReadTable(con,"clock",row.names=NULL)

    clock$send <- as.character(clock$send)

    # 24hr, 12hr, 1hr, and 0hr convert to seconds

    t24 <- 3600 * 24 # 86400
    t12 <- 3600 * 12 # 43200
    t1 <- 3600 # 3600
    t0 <- 0 # 0

    # Checks the clock24, clock12, and clock1 variable. Clock shows "YES" when email already has been
    # sent out at the hours specified. (e.g. 24, 12, or 1 hr). "NO" when email has not been sent out.
    # Here is what this loop does:
    # 1) If email has been sent out at the hours specified (So "YES" is given) but 'time remaining' 
    # for specific player from tbl7 is greater than the hours (24,12 or 1), then reset the clock24,
    # clock12, and clock1 of specific player to "yes" from "no", making player eligible for mass email again.
    # 2) If email has not been sent out at the hours specified (So "NO" is given) but 'time remaining'
    # for specific player from tbl7 is less than the hours (24: less than 24 but more than 12, 
    #12: less than 12 but more than 1, or 1: less than 1 but has not been timed out), then send out a
    # mass email about the player.

    for(m in 1:nrow(clock))
    {
      clock <- dbReadTable(con,"clock",row.names=NULL)

      if(length(which(tbl7$Player %in% clock$Player[m])) > 0)
      {
        # If time left for particular player is more than 24 hours and labeled "YES", that means 
        # email has been sent out before, but bidding increased time left, making it eligible for email
        # alert again. So switch label to "NO"

        # Run this if time left of particular is more than 24 hours
        if(((tbl7$Time_Left[which(tbl7$Player %in% clock$Player[m])]) >= t24) == TRUE)
        {
          # "NO" assigned if email was already sent out before and it has more than 24 hours left.
          # "YES" assigned if email was not sent out before and it has more than 24 
          ifelse((clock$clock24[m] == "YES") == TRUE,clock$clock24[m] <- "NO",clock$clock24[m] <- "NO")

          clock$clock12[m] <- "NO"
          clock$clock1[m] <- "NO"

        }

        # Run this if time left of particular player between 12 and 24
        if((((tbl7$Time_Left[which(tbl7$Player %in% clock$Player[m])]) < t24) == TRUE) & (((tbl7$Time_Left[which(tbl7$Player %in% clock$Player[m])]) >= t12) == TRUE))
        {
          # If email has been sent out 24hr and time remaining is between 12 and 24, keep it "YES". If not email hasn't been sent, keep it "NO" so you can send email.
          ifelse((clock$clock24[m] == "YES") == TRUE, clock$clock24[m] <- "YES", clock$clock24[m] <- "NO")

          # Email has not been sent out, write "24" into "send" form. This is a way to signal the system
          # to send 24-hour warning email.
          if(clock$clock24[m] == "NO")
          {
            clock$send[m] <- 24
            clock$clock24[m] <- "YES"
          }
        }


        ###

        if(((tbl7$Time_Left[which(tbl7$Player %in% clock$Player[m])]) >= t12) == TRUE)
        {
          # "NO" assigned if email was already sent out before and it has more than 12 hours left.
          # "YES" assigned if email was not sent out before and it has more than 12
          ifelse((clock$clock12[m] == "YES") == TRUE,clock$clock12[m] <- "NO",clock$clock12[m] <- "NO")

          clock$clock1[m] <- "NO" 
        }

        # Run this if time left of particular between 12 and 24
        if((((tbl7$Time_Left[which(tbl7$Player %in% clock$Player[m])]) < t12) == TRUE) & (((tbl7$Time_Left[which(tbl7$Player %in% clock$Player[m])]) >= t1) == TRUE))
        {
          # 
          ifelse((clock$clock12[m] == "YES") == TRUE, clock$clock12[m] <- "YES", clock$clock12[m] <- "NO")

          if(clock$clock12[m] == "NO")
          {
            clock$send[m] <- 12
            clock$clock12[m] <- "YES"

          }
        }

        ###

        if(((tbl7$Time_Left[which(tbl7$Player %in% clock$Player[m])]) >= t1) == TRUE)
        {
          # "NO" assigned if email was already sent out before and it has more than 1 hour left.
          # "YES" assigned if email was not sent out before and it has more than 1
          ifelse((clock$clock1[m] == "YES") == TRUE,clock$clock1[m] <- "NO",clock$clock1[m] <- "NO")

        }

        # Run this if time left of particular between 0 and 1
        if((((tbl7$Time_Left[which(tbl7$Player %in% clock$Player[m])]) < t1) == TRUE) & (((tbl7$Time_Left[which(tbl7$Player %in% clock$Player[m])]) > 0) == TRUE))
        {
          # 
          ifelse((clock$clock1[m] == "YES") == TRUE, clock$clock1[m] <- "YES", clock$clock1[m] <- "NO")

          if(clock$clock1[m] == "NO")
          {
            clock$send[m] <- 1
            clock$clock1[m] <- "YES"

          }
        }

        # Insert code for 0hr email

        if(((tbl7$Time_Left[which(tbl7$Player %in% clock$Player[m])]) == 0) == TRUE)
        {
          # "NO" assigned if email was already sent out before and it has more than 1 hour left.
          # "YES" assigned if email was not sent out before and it has more than 1
          ifelse((clock$clockend[m] == "YES") == TRUE,clock$clockend[m] <- "YES",clock$clockend[m] <- "YES")
          clock$clockend[m] <- "YES"
          clock$send[m] <- 0

        }

      }

      if(length(which(tbl7$Player %in% clock$Player[m])) == 0)
      {
        next;
      }
      clock <- clock[,c("Player","clock24","clock12","clock1","clockend","send")]

      dbWriteTable(con,"clock",clock,overwrite=TRUE)

    }

    mail_out <- which(clock$send %in% c("0","1","12","24"))

    if(length(mail_out) > 0)
    {
      for(d in 1:length(mail_out))
      {
        which_hour <- clock$send[mail_out[d]]

        if(which_hour %in% c("1","12","24"))
        {
          updateStatus(paste0(which_hour,"-hour alert for ",unlist(strsplit(as.character(clock$Player[mail_out[d]])," "))[2]," ",sub(",","",unlist(strsplit(as.character(clock$Player[mail_out[d]])," "))[1]),". Make your bid by ", tbl7$End_Time[tbl7$Player %in% clock$Player[mail_out[d]]],"ET"))

        }

        if(which_hour %in% c("0",0))
        {
          tbl_highest <- tbl[tbl$Player == clock$Player[mail_out[d]],]

          tbl_highest <- tbl_highest[order(tbl_highest$Points,decreasing = TRUE),]

          tbl_highest <- tbl_highest[1,]

          updateStatus(paste0("Going once, going twice, and..SOLD! ",unlist(strsplit(as.character(clock$Player[mail_out[d]])," "))[2]," ",sub(",","",unlist(strsplit(as.character(clock$Player[mail_out[d]])," "))[1])," signs with ",tbl_highest$Club[1]," for ",tbl_highest$summary[1],"ET"))

        }
        clock$send[mail_out[d]] <- NA

      }
    }

    clock <- clock[,c("Player","clock24","clock12","clock1","clockend","send")]

    dbWriteTable(con,"clock",clock,overwrite=TRUE)

    dbDisconnect(con)

    tbl8 <- tbl[(nrow(tbl)):(nrow(tbl)-9),]

    tbl8 <- tbl8[,c("row_names","Player","Year_Guaranteed","summary","End_Time")]

    tbl8$row_names <- c(1:10)

    tbl8

  })

0 个答案:

没有答案