summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHorus32015-02-20 16:49:27 +0100
committerHorus32015-02-20 16:49:27 +0100
commitffe1b892b4c01c40215a1b8b5697fc35b81186b5 (patch)
treef4bf64faacb340e7587e638767abc11cff00524a
parent9fd1b6a54c77f78df1031a620fe3fb3887eda56d (diff)
downloadstatuspage-ffe1b892b4c01c40215a1b8b5697fc35b81186b5.tar.gz
Add caching struct as json in redis.
-rw-r--r--app/Makefile6
-rw-r--r--app/fetch.go25
-rw-r--r--app/utilities.go14
3 files changed, 36 insertions, 9 deletions
diff --git a/app/Makefile b/app/Makefile
index c614492..41a813f 100644
--- a/app/Makefile
+++ b/app/Makefile
@@ -7,6 +7,12 @@ endif
ifndef STATUS_DB_IMPORT_DRIVER
export STATUS_DB_IMPORT_DRIVER:=github.com/mattn/go-sqlite3
endif
+ifndef STATUS_REDIS_SERVER
+ export STATUS_REDIS_SERVER:=127.0.0.1
+endif
+ifndef STATUS_REDIS_PORT
+ export STATUS_REDIS_PORT:=6379
+endif
IMPORT_FILE:=import.go
diff --git a/app/fetch.go b/app/fetch.go
index a03504e..82bbb7d 100644
--- a/app/fetch.go
+++ b/app/fetch.go
@@ -1,13 +1,15 @@
package main
import (
+ "encoding/json"
"fmt"
// "time"
+ "log"
)
func run() {
c := c
- c.AddFunc("@every 30s", healthCheck)
+ c.AddFunc("@every 10s", healthCheck)
c.Start()
jobs := c.Entries()
@@ -16,12 +18,31 @@ func run() {
}
}
+const (
+ cache_prefix = "status_"
+)
+
func healthCheck() {
h := []Host{}
db := Db
db.Find(&h)
- for _, v := range h {
+ c := pool.Get()
+ defer c.Close()
+
+ for k, v := range h {
+ // save struct in redis as serialized json
+ // TODO
+ j, err := json.Marshal(v)
+ if err != nil {
+ log.Println("Error: ", err)
+ continue
+ }
+
+ c.Do("SET", cache_prefix+string(k), j)
+
+ fmt.Println("Json: ", j)
+
fmt.Printf("Id: %v, Url: %v \n", v.Id, v.Url)
if resp, _, err := HttpGet(v.Url); err != nil {
diff --git a/app/utilities.go b/app/utilities.go
index 75967b1..5c665e2 100644
--- a/app/utilities.go
+++ b/app/utilities.go
@@ -3,14 +3,15 @@ package main
import (
"crypto/md5"
"fmt"
- // "github.com/garyburd/redigo/redis"
+ "github.com/garyburd/redigo/redis"
"golang.org/x/crypto/bcrypt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
- // "time"
+ "os"
+ "time"
)
// Returns headers as map and the content of a webpage as string
@@ -49,17 +50,17 @@ func RandomKey() string {
return string(key)
}
-//var pool = newPool()
+var pool = newPool()
-/*
// Creates a pool with connections to Redis
func newPool() *redis.Pool {
return &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
- //c, err := redis.Dial("tcp", ":6379")
- c, err := redis.Dial("tcp", revel.Config.StringDefault("redis.server", "127.0.0.1")+":"+revel.Config.StringDefault("redis.port", "6379"))
+ redis_server := os.Getenv("STATUS_REDIS_SERVER")
+ redis_port := os.Getenv("STATUS_REDIS_PORT")
+ c, err := redis.Dial("tcp", redis_server+":"+redis_port)
if err != nil {
return nil, err
}
@@ -71,7 +72,6 @@ func newPool() *redis.Pool {
},
}
}
-*/
// Hashs password with bcrypt and returns the string
func HashPassword(password string) (string, error) {