Last active
October 26, 2017 07:52
-
-
Save hto/20b5e69a8c0ccb1b41e70c23bdbaf492 to your computer and use it in GitHub Desktop.
users | go run main.go -mysqlHost=localhost -mysqlUsername=root -mysqlPassword=root -mysqlDB=headball -mysqlPort=8889
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io" | |
"os" | |
"time" | |
"flag" | |
"strings" | |
"log" | |
"encoding/json" | |
"github.com/go-redis/redis" | |
_ "github.com/go-sql-driver/mysql" | |
"database/sql" | |
) | |
var redisClient *redis.Client | |
var ( | |
redisPort = flag.String("redisPort", "6379", "Redis Port") | |
redisQPort = flag.String("redisQPort", "6379", "Redis Port") | |
mysqlHost = flag.String("mysqlHost", "localhost", "Mysql Host") | |
mysqlPort = flag.String("mysqlPort", "3306", "Mysql Port") | |
mysqlDB = flag.String("mysqlDB", "headball", "Mysql Database") | |
mysqlUsername = flag.String("mysqlUsername", "root", "Mysql UserName") | |
mysqlPassword = flag.String("mysqlPassword", "root", "Mysql User Password") | |
) | |
// | |
type Users struct { | |
UserName string `json:"username"` | |
Email string `json:"email"` | |
Password string `json:"password"` | |
Country string `json:"country"` | |
JoinTime string `json:"jointime"` | |
Status string `json:"status"` | |
Admin string `json:"admin"` | |
LastLogin int `json:"lastlogin"` | |
Lang string `json:"lang"` | |
IsBot string `json:"isbot"` | |
SelectUsername string `json:"selectusername"` | |
JerseyColor int `json:"jerseycolor"` | |
SessionId int `json:"sessionid"` | |
DeviceType int `json:"deviceType"` | |
AppVersion int `json:"appVersion"` | |
} | |
var ( | |
Error *log.Logger | |
) | |
func Init(errorHandle io.Writer) { | |
f, _ := os.OpenFile("/var/log/syncUsers.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666) | |
Error = log.New(f, | |
"ERROR: ", | |
log.Ldate|log.Ltime|log.Lshortfile) | |
} | |
func main(){ | |
Init(os.Stderr) | |
flag.Parse() | |
redisClient := redis.NewClient(&redis.Options{ | |
Addr: "localhost:" + *redisPort, | |
Password: "", // no password set | |
DB: 0, // use default DB | |
}) | |
redisQClient := redis.NewClient(&redis.Options{ | |
Addr: "localhost:" + *redisQPort, | |
Password: "", // no password set | |
DB: 0, // use default DB | |
}) | |
_, err := redisClient.Ping().Result() | |
if err != nil { | |
Error.Println("Redis Not Connected - 1") | |
} | |
_, err = redisQClient.Ping().Result() | |
if err != nil { | |
Error.Println("RedisQ Not Connected - 1") | |
} | |
db, errDb := sql.Open("mysql", *mysqlUsername + ":" + *mysqlPassword + "@tcp(" + *mysqlHost + ":" + *mysqlPort + ")/" + *mysqlDB) | |
if errDb != nil { | |
Error.Println("Mysql Not Connected - 1") | |
} | |
defer db.Close() | |
// Sonsuz Döngü | |
for { | |
err = db.Ping() | |
if err != nil { | |
// Db Erişimi yok ise | |
time.Sleep(15 * time.Second) | |
Error.Println("Mysql Not Connected - 2") | |
db, errDb = sql.Open("mysql", *mysqlUsername + ":" + *mysqlPassword + "@tcp(" + *mysqlHost + ":" + *mysqlPort + ")/" + *mysqlDB) | |
if errDb != nil { | |
Error.Println("Mysql Not Connected - 3") | |
} | |
continue | |
} | |
_, err = redisClient.Ping().Result() | |
if err != nil { | |
Error.Println("Redis Not Connected - 2") | |
time.Sleep(15 * time.Second) | |
redisClient = redis.NewClient(&redis.Options{ | |
Addr: "localhost:" + *redisPort, | |
Password: "", // no password set | |
DB: 0, // use default DB | |
}) | |
continue | |
} | |
_, err = redisQClient.Ping().Result() | |
if err != nil { | |
Error.Println("RedisQ Not Connected - 2") | |
time.Sleep(15 * time.Second) | |
redisQClient = redis.NewClient(&redis.Options{ | |
Addr: "localhost:" + *redisQPort, | |
Password: "", // no password set | |
DB: 0, // use default DB | |
}) | |
continue | |
} | |
txn, errBegin := db.Begin() | |
if errBegin != nil { | |
Error.Println(errBegin) | |
Error.Println("begin txn err") | |
} | |
update, errDb := db.Prepare("update users set "+ | |
"username=? ,"+ | |
"email=? ,"+ | |
"password=? ,"+ | |
"country=? ,"+ | |
"jointime=? ,"+ | |
"status=? ,"+ | |
"admin=? ,"+ | |
"lastlogin=? ,"+ | |
"lang=? ,"+ | |
"isbot=? ,"+ | |
"selectusername=? ,"+ | |
"jerseycolor=? ,"+ | |
"sessionid=? ,"+ | |
"deviceType=? ,"+ | |
"appVersion=? "+ | |
"where id=?") | |
if errDb != nil { | |
Error.Println(errDb) | |
Error.Println("Mysql Update Error !!") | |
continue | |
} | |
for i := 0; i < 999; i++ { | |
queue := redisClient.SPop("queue:users") | |
if queue != nil && len(queue.Val()) > 0 { | |
userData := redisClient.HGet("users", queue.Val()) | |
if userData.Val() == "" { | |
continue | |
} | |
dec := json.NewDecoder(strings.NewReader(userData.Val())) | |
var users Users | |
err := dec.Decode(&users); | |
if err != nil { | |
Error.Println(err) | |
Error.Println("User not found") | |
continue | |
} | |
_, updateErr := update.Exec( | |
users.UserName, | |
users.Email, | |
users.Password, | |
users.Country, | |
users.JoinTime, | |
users.Status, | |
users.Admin, | |
users.LastLogin, | |
users.Lang, | |
users.IsBot, | |
users.SelectUsername, | |
users.JerseyColor, | |
users.SessionId, | |
users.DeviceType, | |
users.AppVersion, | |
queue.Val()) | |
if updateErr != nil { | |
Error.Println(updateErr) | |
Error.Println("Mysql Update Error - 2 - !!") | |
continue | |
} | |
} | |
} | |
errCommit := txn.Commit() | |
if errCommit != nil { | |
Error.Println(errCommit) | |
} | |
// 15sn dinlenme | |
time.Sleep(15 * time.Second) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment