Skip to content

Commit

Permalink
add timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
ixqbar committed Sep 30, 2018
1 parent 6a95193 commit f87a14d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

### version
```
v0.0.7
v0.0.8
```

### usage
Expand Down Expand Up @@ -97,7 +97,7 @@ $redis_handle->rawCommand('exists', '/web/images/hello.gif');

### deps
* https://github.com/jonnywang/go-kits/redis
* https://github.com/jlaffaye/ftp
* https://github.com/jonnywang/ftp

### faq
* qq群 233415606
5 changes: 3 additions & 2 deletions src/ftpSync/const.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ftpSync

const VERSION = "0.0.7"
const VERSION = "0.0.8"
const TRY_CONNECT_FTP_SERVER_MAX_NUM = 3
const DEPEND_PROCESS_TIMEOUT_SECONDS = 30
const SYNC_TO_FTP_MAX_NUM = 3
const SYNC_TO_FTP_AGAINT_LATER_SECONDS = 3
const CHECK_FTP_CONNECTION_STATE_INTERVAL_SECONDS = 15
const CHECK_FTP_CONNECTION_STATE_INTERVAL_SECONDS = 15
const SYNC_TO_FTP_READ_WRITE_TIMEOUT_SECONDS = 60
11 changes: 10 additions & 1 deletion src/ftpSync/syncFtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ftpSync

import (
"errors"
"github.com/jlaffaye/ftp"
"github.com/jonnywang/ftp"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -79,6 +79,7 @@ func (obj *SyncFtp) Init() {
for {
select {
case syncFile := <-obj.syncFileChannel:
Logger.Printf("got sync channel file %v", syncFile)
obj.Put(syncFile.LocalFile, syncFile.RemoteFile, syncFile.NumberTimes)
default:
break F
Expand Down Expand Up @@ -134,6 +135,8 @@ func (obj *SyncFtp) doDependReadyAction() {
pid := obj.dependProcess.Pid
Logger.Printf("wait depend process pid=%d", pid)
obj.dependProcess.Wait()
obj.dependProcess = nil
obj.syncFtpServer = nil
Logger.Printf("wait depend process pid=%d success", pid)
}()

Expand Down Expand Up @@ -190,6 +193,8 @@ func (obj *SyncFtp) ftpServerStateIsActive() bool {
continue
}

fs.SetOpsTimeout(time.Second * SYNC_TO_FTP_READ_WRITE_TIMEOUT_SECONDS)

err = fs.Login(GConfig.FtpServerUser, GConfig.FtpServerPassword)
if err != nil {
Logger.Printf("login ftp server %s failed %v", GConfig.FtpServerAddress, err)
Expand Down Expand Up @@ -240,6 +245,8 @@ func (obj *SyncFtp) Put(localFile, remoteFile string, numberTimes int) bool {
obj.Lock()
defer obj.Unlock()

Logger.Printf("ready to put %s to %s success", localFile, remoteFile)

reader, err := os.Open(localFile)
if err != nil {
Logger.Printf("open %s failed %v", localFile, err)
Expand Down Expand Up @@ -298,6 +305,8 @@ func (obj *SyncFtp) Put(localFile, remoteFile string, numberTimes int) bool {
}
}

Logger.Printf("start sync %s to %s", localFile, remoteFile)

err = obj.syncFtpServer.Stor(remoteFile, reader)
if err != nil {
Logger.Printf("sync %s to %s failed %v", localFile, remoteFile, err)
Expand Down

0 comments on commit f87a14d

Please sign in to comment.