Commit b8e2fa2d authored by Charles's avatar Charles

fix issue when mysql is stopped

parent 38097365
......@@ -3,48 +3,7 @@ library(data.table)
library(RMySQL)
library(mailR)
con <- dbConnect(MySQL(),
user = Sys.getenv("VELOV_USER"),
password = Sys.getenv("VELOV_PASSWORD"),
dbname = Sys.getenv("VELOV_DB"),
host = Sys.getenv("VELOV_HOST"),
port = as.numeric(Sys.getenv("VELOV_PORT")))
if (!"dispo" %in% dbListTables(con)) {
# Create disponibilite table
df <- data.frame(
number = integer(),
pole = character(),
available_bikes = integer(),
code_insee = character(),
lng = numeric(),
availability = character(),
availabilitycode = integer(),
etat = character(),
startdate = character(),
langue = character(),
bike_stands = integer(),
last_update = character(),
available_bike_stands = integer(),
gid = integer(),
titre = character(),
status = character(),
commune = character(),
description = character(),
nature = character(),
bonus = character(),
address2 = character(),
address = character(),
lat = numeric(),
last_update_fme = character(),
enddate = character(),
name = character(),
banking = logical(),
nmarrond = integer()
)
dbWriteTable(con, "dispo", df, overwrite = TRUE)
}
source(file.path(Sys.getenv("VELOV_CSV"), "velovpredict", "utils.R"))
dispo_url <- paste0("https://download.data.grandlyon.com/ws/rdata/",
"jcd_jcdecaux.jcdvelov/all.json")
......@@ -55,7 +14,7 @@ filename <- file.path(Sys.getenv("VELOV_CSV"), "data",
paste0(as.integer(Sys.time()), ".csv"))
fwrite(dt, file = filename)
res <- tryCatch(dbWriteTable(con, "dispo", dt, append = TRUE),
res <- tryCatch(write_dispo(dt),
error = function(e) FALSE,
warning = function(e) FALSE)
......@@ -63,7 +22,7 @@ res <- tryCatch(dbWriteTable(con, "dispo", dt, append = TRUE),
for (i in 1:300) {
if (res == FALSE) {
Sys.sleep(5 * 60)
res <- tryCatch(dbWriteTable(con, "dispo", dt, append = TRUE),
res <- tryCatch(write_dispo(dt),
error = function(e) FALSE,
warning = function(e) FALSE)
}
......@@ -84,8 +43,6 @@ if (res == FALSE) {
debug = FALSE)
}
dbDisconnect(con)
if (res == TRUE) {
file.remove(filename)
dt <- data.table(time = Sys.time(),
......
check_dispo_exists <- function(con) {
if (!"dispo" %in% dbListTables(con)) {
# Create disponibilite table
df <- data.frame(
number = integer(),
pole = character(),
available_bikes = integer(),
code_insee = character(),
lng = numeric(),
availability = character(),
availabilitycode = integer(),
etat = character(),
startdate = character(),
langue = character(),
bike_stands = integer(),
last_update = character(),
available_bike_stands = integer(),
gid = integer(),
titre = character(),
status = character(),
commune = character(),
description = character(),
nature = character(),
bonus = character(),
address2 = character(),
address = character(),
lat = numeric(),
last_update_fme = character(),
enddate = character(),
name = character(),
banking = logical(),
nmarrond = integer()
)
dbWriteTable(con, "dispo", df, overwrite = TRUE)
}
}
write_dispo <- function(dt) {
con <- dbConnect(MySQL(),
user = Sys.getenv("VELOV_USER"),
password = Sys.getenv("VELOV_PASSWORD"),
dbname = Sys.getenv("VELOV_DB"),
host = Sys.getenv("VELOV_HOST"),
port = as.numeric(Sys.getenv("VELOV_PORT")))
check_dispo_exists(con)
dbWriteTable(con, "dispo", dt, append = TRUE)
dbDisconnect(con)
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment