Handles SSH/SFTP connection closing gracefully. Adds auto restarting of processes config option. Makes dying a little more uniform. Still need to make dying graceful.
This commit is contained in:
parent
d36b7ff8d6
commit
758ce5cb8e
3 changed files with 79 additions and 28 deletions
|
@ -1,6 +1,8 @@
|
||||||
// Configuration for Hoarder
|
// Configuration for Hoarder
|
||||||
// All fields and values are necessary
|
// All fields and values are necessary
|
||||||
|
|
||||||
|
// Should Hoarder automatically restart when one of its processes errors? (default true if not set)
|
||||||
|
restart_on_error: "true"
|
||||||
// Username for XMLRPC
|
// Username for XMLRPC
|
||||||
xml_user: "testuser"
|
xml_user: "testuser"
|
||||||
// Password for XMLRPC
|
// Password for XMLRPC
|
||||||
|
|
45
hoarder.go
45
hoarder.go
|
@ -66,6 +66,7 @@ func checker(config map[string]string, checkerChan <- chan map[string]string, co
|
||||||
}
|
}
|
||||||
|
|
||||||
syncer, err := NewSync(config["threads"], config["ssh_user"], config["ssh_pass"], config["ssh_server"], config["ssh_port"])
|
syncer, err := NewSync(config["threads"], config["ssh_user"], config["ssh_pass"], config["ssh_server"], config["ssh_port"])
|
||||||
|
defer syncer.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to create a new sync: " + err.Error())
|
log.Println("Failed to create a new sync: " + err.Error())
|
||||||
com <- err
|
com <- err
|
||||||
|
@ -142,6 +143,8 @@ func checker(config map[string]string, checkerChan <- chan map[string]string, co
|
||||||
} else {
|
} else {
|
||||||
log.Println(name + " is not completed, waiting for it to finish")
|
log.Println(name + " is not completed, waiting for it to finish")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
syncer.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
com <- nil
|
com <- nil
|
||||||
|
@ -186,6 +189,7 @@ func scanner(config map[string]string, checkerChan chan <- map[string]string, co
|
||||||
syncer, err := NewSync("1", config["ssh_user"], config["ssh_pass"], config["ssh_server"], config["ssh_port"])
|
syncer, err := NewSync("1", config["ssh_user"], config["ssh_pass"], config["ssh_server"], config["ssh_port"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to create a new sync: " + err.Error())
|
log.Println("Failed to create a new sync: " + err.Error())
|
||||||
|
syncer.Close()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,6 +197,7 @@ func scanner(config map[string]string, checkerChan chan <- map[string]string, co
|
||||||
exists, err := syncer.Exists(destinationTorrent)
|
exists, err := syncer.Exists(destinationTorrent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to see if " + torrentPath + " already exists on the server: " + err.Error())
|
log.Println("Failed to see if " + torrentPath + " already exists on the server: " + err.Error())
|
||||||
|
syncer.Close()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,8 +212,11 @@ func scanner(config map[string]string, checkerChan chan <- map[string]string, co
|
||||||
log.Println("Failed to upload " + torrentPath + " to " + destinationTorrent + ": " + err.Error())
|
log.Println("Failed to upload " + torrentPath + " to " + destinationTorrent + ": " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
syncer.Close()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
syncer.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
downloadInfo := map[string]string{
|
downloadInfo := map[string]string{
|
||||||
|
@ -239,10 +247,14 @@ func scanner(config map[string]string, checkerChan chan <- map[string]string, co
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func die(exitCode int) {
|
||||||
|
log.Println("Quiting")
|
||||||
|
os.Exit(exitCode)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
sigint.ListenForSIGINT(func() {
|
sigint.ListenForSIGINT(func() {
|
||||||
log.Println("Quiting")
|
die(1)
|
||||||
os.Exit(1)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
var configPath string
|
var configPath string
|
||||||
|
@ -252,14 +264,14 @@ func main() {
|
||||||
if configPath == "" {
|
if configPath == "" {
|
||||||
log.Println("Missing argument for configuration file path")
|
log.Println("Missing argument for configuration file path")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
os.Exit(1)
|
die(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Reading configuration file")
|
log.Println("Reading configuration file")
|
||||||
config, err := loadConfig(configPath)
|
config, err := loadConfig(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
os.Exit(1)
|
die(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Successfully read configuration file")
|
log.Println("Successfully read configuration file")
|
||||||
|
@ -268,7 +280,7 @@ func main() {
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
os.Exit(1)
|
die(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Starting the scanner routine")
|
log.Println("Starting the scanner routine")
|
||||||
|
@ -279,17 +291,36 @@ func main() {
|
||||||
checkerCom := make(chan error)
|
checkerCom := make(chan error)
|
||||||
go checker(config, checkerChan, checkerCom)
|
go checker(config, checkerChan, checkerCom)
|
||||||
|
|
||||||
|
restartOnError := true
|
||||||
|
if config["restart_on_error"] != "" {
|
||||||
|
restartOnError = config["restart_on_error"] == "true"
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case err := <-scannerCom:
|
case err := <-scannerCom:
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Scanner failed: " + err.Error())
|
log.Println("Scanner failed: " + err.Error())
|
||||||
os.Exit(1)
|
|
||||||
|
if restartOnError {
|
||||||
|
log.Println("Restarting scanner")
|
||||||
|
go scanner(config, checkerChan, scannerCom)
|
||||||
|
} else {
|
||||||
|
log.Println("Quiting due to scanner error")
|
||||||
|
die(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case err := <-checkerCom:
|
case err := <-checkerCom:
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Checker failed: " + err.Error())
|
log.Println("Checker failed: " + err.Error())
|
||||||
os.Exit(1)
|
|
||||||
|
if restartOnError {
|
||||||
|
log.Println("Restarting checker")
|
||||||
|
go checker(config, checkerChan, checkerCom)
|
||||||
|
} else {
|
||||||
|
log.Println("Quiting due to checker error")
|
||||||
|
die(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
|
|
18
sync.go
18
sync.go
|
@ -58,6 +58,24 @@ func NewSync(threads string, user string, pass string, server string, port strin
|
||||||
return &Sync{sshClient, sftpClients, clientCount}, nil
|
return &Sync{sshClient, sftpClients, clientCount}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close Closes all of the ssh and sftp connections to the SSH server.
|
||||||
|
func (s Sync) Close() error {
|
||||||
|
var returnError error
|
||||||
|
for i := 0; i < s.sftpClientCount; i++ {
|
||||||
|
err := s.sftpClients[i].Close()
|
||||||
|
if err != nil {
|
||||||
|
returnError = err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err := s.sshClient.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return returnError
|
||||||
|
}
|
||||||
|
|
||||||
// Create a new SSH client instance and confirm that we can make sessions
|
// Create a new SSH client instance and confirm that we can make sessions
|
||||||
func newSSHClient(user string, pass string, server string, port string) (*ssh.Client, error) {
|
func newSSHClient(user string, pass string, server string, port string) (*ssh.Client, error) {
|
||||||
sshConfig := &ssh.ClientConfig{
|
sshConfig := &ssh.ClientConfig{
|
||||||
|
|
Loading…
Reference in a new issue