func StartTcpServer(addr string, chStop chan int) { chAppStop = chStop go func() { tcpAddr, err := net.ResolveTCPAddr("tcp4", addr) listener, err := net.ListenTCP("tcp", tcpAddr) if err != nil { Logger.Error(LogConf.NetCoreServer, LogConf.SERVER, "Listening error: %v", err) os.Exit(-1) } defer listener.Close() serverRunning = true Logger.Info(LogConf.NetCoreServer, LogConf.SERVER, "NetCore Start OK...") for { if !serverRunning { break } conn, err := listener.AcceptTCP() if err != nil { Logger.Error(LogConf.NetCoreServer, LogConf.SERVER, "Error accept: %v", err) } else { newClient(conn) } } chAppStop <- ErrConf.TCP_SERVER_ERR }() }
func (handler *Handler) resolveTask(task *models.Task) { err := handler.bbs.ResolvingTask(task) if err != nil { logger.Info("handler.resolving-failed", map[string]interface{}{ "task": task.Guid, "error": err.Error(), }) return } err = handler.natsClient.Publish(task.ReplyTo, task.ToJSON()) if err != nil { logger.Error("handler.publish-failed", map[string]interface{}{ "task": task.Guid, "error": err.Error(), }) return } err = handler.bbs.ResolveTask(task) if err != nil { logger.Error("handler.resolve-failed", map[string]interface{}{ "task": task.Guid, "error": err.Error(), }) return } logger.Info("handler.resolved", map[string]interface{}{ "task": task.Guid, }) }
func main() { flag.Parse() v := appVersion() if version { fmt.Println(v) os.Exit(0) } logger.Info("main", v) cfg, err := loadConfig() if err != nil { logger.Error("main", "The configuration could not be loaded", "error", err, "path", conf) os.Exit(1) } // Start service q := qubot.Init(cfg) q.Handle(handlers.PingHandler, handlers.TauntHandler) err = q.Start() if err != nil { logger.Error("main", "Qubot could not be started", "error", err) q.Close() os.Exit(1) } // More advanced management on signals here: https://goo.gl/fuylKX sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGINT, // aka os.Interrupt syscall.SIGTERM, syscall.SIGUSR1) SELECT: select { case sig := <-sigChan: switch sig { case syscall.SIGINT, syscall.SIGTERM: q.Close() case syscall.SIGUSR1: q.Report() goto SELECT } case <-q.Done(): logger.Info("main", "Qubot stopped") } logger.Info("main", "¡Adiós!") }
func NewAgent(agent **Client, addr string, stype uint32, timerName string) int { agentClient := newAgentClient(addr) if agentClient == nil { return ErrConf.AGENT_ERR } *agent = agentClient //timerKey := timerName + fmt.Sprintf("%d", agentClient.Idx) if timerName != "" { timer := agentHeartbeatTmer.NewTimer(timerName+fmt.Sprintf("%d", agentClient.Idx), int64(NetConf.HEARTBEART_TIME), int64(NetConf.HEARTBEART_TIME), func() { Logger.Error(LogConf.NetCoreAgent, LogConf.SERVER, "Agent heart %v %v", lastHeart, time.Now().UTC()) lastHeart = time.Now().UTC() agentClient.HeartBeat() }, true) agentClient.AddCloseCB("courierClose", func(client *Client) { Logger.Info(LogConf.NetCoreAgent, LogConf.SERVER, "Server close, server type: %d", stype) agentHeartbeatTmer.DeleteTimer(timer) go NewAgent(agent, addr, stype, timerName) }) } go func() { time.Sleep(time.Second) agentClient.Regist(stype) }() return 0 }
func (handler *Handler) runTask(task *models.Task) { defer handler.releaseMemory(task.MemoryMB) logger.Info("task.claimed", map[string]interface{}{ "task": task.Guid, }) sleepForARandomInterval("task.create-container", 500, 1000, map[string]interface{}{ "task": task.Guid, }) logger.Info("task.start", map[string]interface{}{ "task": task.Guid, }) err := handler.bbs.StartTask(task, "container") if err != nil { logger.Error("task.start-failed", map[string]interface{}{ "task": task.Guid, "error": err.Error(), }) return } sleepForARandomInterval("task.run", 5000, 5001, map[string]interface{}{ "task": task.Guid, }) logger.Info("task.completing", map[string]interface{}{ "task": task.Guid, }) err = handler.bbs.CompleteTask(task, false, "", "") if err != nil { logger.Error("task.complete-failed", map[string]interface{}{ "task": task.Guid, "error": err.Error(), }) return } logger.Info("task.completed", map[string]interface{}{ "task": task.Guid, }) }
func (msqlMgr *MysqlMgr) start(addr string) { var err error if msqlMgr.db, err = sql.Open("mysql", "user:password@/dbname"); err != nil { Logger.Error(LogConf.DBMgrMysql, LogConf.SERVER, "MysqlMgr %d Open Error: %v", msqlMgr.idx, err) msqlMgr.restart(addr) return } msqlMgr.restarting = false Logger.Info(LogConf.DBMgrMysql, LogConf.SERVER, "MysqlMgr %d Open Success!", msqlMgr.idx) }
func newAgentClient(addr string) *Client { var err error var conn *net.TCPConn if conn, err = dial(addr); err != nil { Logger.Error(LogConf.NetCoreAgent, LogConf.SERVER, "Agent dial addr %s error: %v", addr, err) return nil } agent := newClient(conn) return agent }
func (handler *Handler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { var task *models.Task err := json.NewDecoder(request.Body).Decode(&task) if err != nil { logger.Error("handler.malformed-payload", map[string]interface{}{ "error": err.Error(), }) writer.WriteHeader(http.StatusBadRequest) return } sleepForARandomInterval("handler.hesitate", 0, 100, map[string]interface{}{ "task": task.Guid, }) ok := handler.reserveMemory(task.MemoryMB) if !ok { logger.Info("handler.full", map[string]interface{}{ "task": task.Guid, }) writer.WriteHeader(http.StatusServiceUnavailable) return } logger.Info("claiming.runonce", map[string]interface{}{ "task": task.Guid, }) err = handler.bbs.ClaimTask(task, executorID) if err != nil { handler.releaseMemory(task.MemoryMB) logger.Info("handler.claim-failed", map[string]interface{}{ "task": task.Guid, "error": err.Error(), }) writer.WriteHeader(http.StatusConflict) return } go handler.runTask(task) writer.WriteHeader(http.StatusCreated) }
func (client *Client) start() bool { if err := client.conn.SetKeepAlive(true); err != nil { Logger.Error(LogConf.NetCoreClient, client.Idx, "%d SetKeepAlive error: %v", client.Idx, err) return false } if err := client.conn.SetKeepAlivePeriod(NetConf.KEEP_ALIVE_TIME); err != nil { Logger.Error(LogConf.NetCoreClient, client.Idx, "%d SetKeepAlivePeriod error: %v", client.Idx, err) return false } if err := (*client.conn).SetReadBuffer(NetConf.RECV_BUF_LEN); err != nil { Logger.Error(LogConf.NetCoreClient, client.Idx, "%d SetReadBuffer error: %v", client.Idx, err) return false } if err := (*client.conn).SetWriteBuffer(NetConf.SEND_BUF_LEN); err != nil { Logger.Error(LogConf.NetCoreClient, client.Idx, "%d SetWriteBuffer error: %v", client.Idx, err) return false } go sendCoroutine(client) return true }
func (eventMgr *EventMgr) NewListener(tag interface{}, event interface{}, handler EventHandler) bool { eventMgr.mutex.Lock() defer eventMgr.mutex.Unlock() if _, ok := eventMgr.listenerMap[tag]; ok { Logger.Error(LogConf.EventMgr, LogConf.SERVER, "NewListener Error: listener %v exist!", tag) return false } eventMgr.listenerMap[tag] = event if eventMgr.listeners[event] == nil { eventMgr.listeners[event] = make(map[interface{}]EventHandler) } eventMgr.listeners[event][tag] = handler return true }
func (q *Qubot) connect() error { // Don't get too far until we validate our credentials. resp, err := q.client.AuthTest() if err != nil { logger.Error("qubot", "Authentication request test failed") return err } logger.Info("qubot", "Authentication request test succeeded", "url", resp.URL) q.wg.Add(1) go func() { defer q.wg.Done() // ManageConnection will exit sometime after q.rtm.Disconnect() q.rtm.ManageConnection() }() return err }
func (client *Client) HandleMsg(msg *NetMsg) bool { Logger.Info(LogConf.NetCoreClient, client.Idx, "HandleMsg, client Idx: %d, Cmd: %d", client.Idx, msg.Cmd) //Logger.Info(LogConf.NetCoreClient, client.Idx, "msg handler 111 %d %d %v", client.Idx, msg.Cmd, msg.Buf) if handlerMap[msg.Cmd] != nil { //Logger.Info(LogConf.NetCoreClient, client.Idx, "msg handler 222 %d %d %v", client.Idx, msg.Cmd, msg.Buf) if !handlerMap[msg.Cmd](client, msg) { client.Stop() return false } } else { Logger.Error(LogConf.NetCoreClient, client.Idx, "no handler for cmd %d, echo--->", msg.Cmd) client.Stop() return false } return true }
func (mongoMgr *MongoMgr) start(addr string) { var err error // 3 new goroutines created when mgo.Dial success mongoMgr.session, err = mgo.DialWithTimeout(addr, DBConf.DIAL_TIMEOUT) if err != nil { Logger.Error(LogConf.DBMgrMongo, LogConf.SERVER, "MongoMgr %d Open Error: %v", mongoMgr.idx, err) mongoMgr.restart(addr) return } mongoMgr.session.SetMode(mgo.Monotonic, true) mongoMgr.db = mongoMgr.session.DB(DBConf.MONGO_DB_NAME) mongoMgr.restarting = false Logger.Info(LogConf.DBMgrMongo, LogConf.SERVER, "MongoMgr %d Open Success!", mongoMgr.idx) }
func msgCoroutine(client *Client) { defer client.Clear() for { select { case <-client.chStop: Logger.Info(LogConf.NetCoreClient, client.idx, "msgCoroutine exit.") return case msg := <-client.recvQ: if handlerMap[msg.Cmd] != nil { handlerMap[msg.Cmd](client, msg) } else { Logger.Error(LogConf.NetCoreClient, client.idx, "no handler for cmd %d, %s", msg.Cmd, "echo--->") client.EchoMsg(msg) } } } }
func (client *Client) HandleMsg(msg *NetMsg) bool { Logger.Info(LogConf.NetCoreClient, client.Idx, "HandleMsg, client Idx: %d, Cmd: %d", client.Idx, msg.Cmd) cb, ok := handlerMap[msg.Cmd] //Logger.Info(LogConf.NetCoreClient, client.Idx, "msg handler 111 %d %d %v", client.Idx, msg.Cmd, msg.Buf) if ok { if msg.Cmd != Commen.MsgConf.ClientRegist && client.valid { return cb(client, msg) } else { goto Err } } else { Logger.Error(LogConf.NetCoreClient, client.Idx, "no handler for cmd %d, echo--->", msg.Cmd) goto Err } Err: client.Stop() return false }
func HandlePanic() { if err := recover(); err != nil { errstr := fmt.Sprintf("%sruntime error: %v\ntraceback:\n", SEPARATOR, err) i := 1 for { pc, file, line, ok := runtime.Caller(i) errstr += fmt.Sprintf(" stack: %d %v [file: %s] [func: %s] [line: %d]\n", i, ok, file, runtime.FuncForPC(pc).Name(), line) i++ if !ok || i > MAX_STACK { break } } errstr += SEPARATOR Logger.Error(LogConf.Panic, LogConf.SERVER, errstr) } }
// onConnectedEvent retrieves information about the team and persist it. func (q *Qubot) onConnectedEvent(_ *slack.ConnectedEvent) error { info := q.rtm.GetInfo() for _, user := range info.Users { if user.Name == q.config.Slack.Nickname { q.me = &user continue } for _, iu := range ignoreUserList { if user.Name == iu || user.ID == iu { continue } } if user.IsBot { // and user.Deleted? continue } q.users[user.ID] = &user // Persist user to the database err := q.db.Update(func(tx *Tx) error { u, err := tx.User(user.ID) if err != nil { return err } if u != nil { return nil } return tx.SaveUser(&User{ ID: user.ID, Name: user.Name, Email: user.Profile.Email, Creation: time.Now(), }) }) if err != nil { logger.Error("qubot", "...", "error", err) } } logger.Info("qubot", fmt.Sprintf("%d users have been identified (not incluing me or slackbot)", len(q.users))) return nil }
func NewEventMgr(tag interface{}) *EventMgr { instanceMutex.Lock() defer instanceMutex.Unlock() if _, ok := instanceMap[tag]; ok { Logger.Error(LogConf.EventMgr, LogConf.SERVER, "NewEventMgr Error: EventMgr %v exist!", tag) return nil } eventMgr := &EventMgr{ listenerMap: make(map[interface{}]interface{}), listeners: make(map[interface{}]map[interface{}]EventHandler), mutex: &sync.Mutex{}, valid: true, } if instanceMap == nil { instanceMap = make(map[interface{}]*EventMgr) } instanceMap[tag] = eventMgr return eventMgr }