func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) { timer := time.AfterFunc(*requestTimeout, func() { t.t.CancelRequest(req) log.Warn("Canceled request for %s", req.URL) }) defer timer.Stop() resp, err := t.t.RoundTrip(req) return resp, err }
func runClient(ctx *cli.Context) { if len(setting.Cfg.Sync.RemoteAddr) == 0 { log.Fatal("Remote address cannot be empty") } watcher, err := fsnotify.NewWatcher() if err != nil { log.Fatal("Fail to create new watcher: %v", err) } defer watcher.Close() go func() { for { select { case e := <-watcher.Events: if isTmpFile(e.Name) { continue } if e.Op&fsnotify.Create == fsnotify.Create || e.Op&fsnotify.Write == fsnotify.Write { if com.IsDir(e.Name) { log.Warn("Hasn't support directory yet") continue } sendFile(strings.TrimPrefix(e.Name, setting.WorkDir+"/")) } } } }() if err = watcher.Add(setting.WorkDir); err != nil { log.Fatal("Fail to watch directory(%s): %v", setting.WorkDir, err) } dirs, err := com.GetAllSubDirs(setting.WorkDir) if err != nil { log.Fatal("Fail to get subdirectories(%s): %v", setting.WorkDir, err) } for _, dir := range dirs { if err = watcher.Add(path.Join(setting.WorkDir, dir)); err != nil { log.Fatal("Fail to watch directory(%s): %v", path.Join(setting.WorkDir, dir), err) } } log.Info("Start watching...") select {} }
func notify(cmds [][]string) { runningLock.Lock() defer func() { runningCmd = nil runningLock.Unlock() }() for _, cmd := range cmds { command := exec.Command(cmd[0], cmd[1:]...) command.Stdout = os.Stdout command.Stderr = os.Stderr if err := command.Start(); err != nil { log.Error("Fail to start command %v - %v", cmd, err) fmt.Print("\x07") return } log.Debug("Running %v", cmd) runningCmd = command done := make(chan error) go func() { done <- command.Wait() }() isShutdown := false select { case err := <-done: if isShutdown { return } else if err != nil { log.Warn("Fail to execute command %v - %v", cmd, err) fmt.Print("\x07") return } case <-shutdown: isShutdown = true gracefulKill() return } } log.Info("Notify operations are done!") }
func runServer(ctx *cli.Context) { if len(setting.Cfg.Sync.ListenAddr) == 0 { log.Fatal("Listen address cannot be empty") } l, err := net.Listen("tcp", setting.Cfg.Sync.ListenAddr) if err != nil { log.Fatal("Fail to start server on listening(%s): %v", setting.Cfg.Sync.ListenAddr, err) } log.Info("Listening on %s...", setting.Cfg.Sync.ListenAddr) for { conn, err := l.Accept() if err != nil { if ne, ok := err.(net.Error); !ok || !ne.Temporary() { log.Warn("Network error when accpet: %v", err) } continue } go receiveHandler(conn) } }
func notify(cmds [][]string) { runningLock.Lock() defer func() { runningCmd = nil runningLock.Unlock() }() for _, cmd := range cmds { command := exec.Command(cmd[0], cmd[1:]...) command.Stdout = os.Stdout command.Stderr = os.Stderr if err := command.Start(); err != nil { log.Error("Fail to start command %v", cmd) fmt.Print("\x07") return } runningCmd = command done := make(chan error) go func() { done <- command.Wait() }() select { case err := <-done: if err != nil { if strings.Contains(err.Error(), "signal: killed") { return } log.Warn("Fail to execute command %v", cmd) fmt.Print("\x07") return } } } log.Info("Notify operations are done!") }