Skip to content

Commit

Permalink
Reconnect to the db if ping failed + removed panic
Browse files Browse the repository at this point in the history
  • Loading branch information
fdurand committed Apr 1, 2019
1 parent 4c07779 commit bb8b3ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
11 changes: 6 additions & 5 deletions go/dhcp/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/gorilla/mux"
"github.com/inverse-inc/packetfence/go/api-frontend/unifiedapierrors"
"github.com/inverse-inc/packetfence/go/log"
"github.com/inverse-inc/packetfence/go/pfconfigdriver"
"github.com/inverse-inc/packetfence/go/sharedutils"
dhcp "github.com/krolaw/dhcp4"
Expand Down Expand Up @@ -189,7 +190,7 @@ func handleReleaseIP(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json; charset=UTF-8")
res.WriteHeader(http.StatusOK)
if err := json.NewEncoder(res).Encode(result); err != nil {
panic(err)
log.LoggerWContext(ctx).Error("Error releasing IP: " + err.Error())
}
}

Expand All @@ -213,7 +214,7 @@ func handleOverrideOptions(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json; charset=UTF-8")
res.WriteHeader(http.StatusOK)
if err := json.NewEncoder(res).Encode(result); err != nil {
panic(err)
log.LoggerWContext(ctx).Error("Error adding MAC options: " + err.Error())
}
}

Expand All @@ -237,7 +238,7 @@ func handleOverrideNetworkOptions(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json; charset=UTF-8")
res.WriteHeader(http.StatusOK)
if err := json.NewEncoder(res).Encode(result); err != nil {
panic(err)
log.LoggerWContext(ctx).Error("Error adding network options: " + err.Error())
}
}

Expand All @@ -254,7 +255,7 @@ func handleRemoveOptions(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json; charset=UTF-8")
res.WriteHeader(http.StatusOK)
if err := json.NewEncoder(res).Encode(result); err != nil {
panic(err)
log.LoggerWContext(ctx).Error("Error removing MAC options: " + err.Error())
}
}

Expand All @@ -271,7 +272,7 @@ func handleRemoveNetworkOptions(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json; charset=UTF-8")
res.WriteHeader(http.StatusOK)
if err := json.NewEncoder(res).Encode(result); err != nil {
panic(err)
log.LoggerWContext(ctx).Error("Error removing betwork options: " + err.Error())
}
}

Expand Down
9 changes: 9 additions & 0 deletions go/dhcp/keysoption.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
)

func MysqlInsert(key string, value string) bool {
if err := MySQLdatabase.PingContext(ctx); err != nil {
log.LoggerWContext(ctx).Error("Unable to ping database, reconnect: " + err.Error())
}
rows, err := MySQLdatabase.Query("replace into key_value_storage values(?,?)", "/dhcpd/"+key, value)
defer rows.Close()
if err != nil {
Expand All @@ -16,6 +19,9 @@ func MysqlInsert(key string, value string) bool {
}

func MysqlGet(key string) (string, string) {
if err := MySQLdatabase.PingContext(ctx); err != nil {
log.LoggerWContext(ctx).Error("Unable to ping database, reconnect: " + err.Error())
}
rows, err := MySQLdatabase.Query("select id, value from key_value_storage where id = ?", "/dhcpd/"+key)
defer rows.Close()
if err != nil {
Expand All @@ -36,6 +42,9 @@ func MysqlGet(key string) (string, string) {
}

func MysqlDel(key string) bool {
if err := MySQLdatabase.PingContext(ctx); err != nil {
log.LoggerWContext(ctx).Error("Unable to ping database, reconnect: " + err.Error())
}
rows, err := MySQLdatabase.Query("delete from key_value_storage where id = ?", "/dhcpd/"+key)
defer rows.Close()
if err != nil {
Expand Down

0 comments on commit bb8b3ed

Please sign in to comment.