Revert file on error

This commit is contained in:
simon 2019-09-24 22:08:05 -04:00
parent 0af56e8306
commit ffd540e157
3 changed files with 26 additions and 4 deletions

View File

@ -1,10 +1,13 @@
package main package main
import ( import (
"fmt"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"os" "os"
"os/exec" "os/exec"
"path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"sync" "sync"
@ -26,6 +29,7 @@ type Beemer struct {
tarMaxCount int tarMaxCount int
closing bool closing bool
excludePattern *regexp.Regexp excludePattern *regexp.Regexp
failDir string
} }
type File struct { type File struct {
@ -197,7 +201,13 @@ func (b *Beemer) beemTar() {
logrus.Error(err) logrus.Error(err)
} }
b.executeBeemCommand(name, name) err = b.executeBeemCommand(name, name)
if err != nil {
logrus.WithError(err).Error("Error during beem command! Moved tar file to failDir")
_ = os.Mkdir(b.failDir, 0700)
err = moveFile(name, path.Join(b.failDir, path.Base(name)))
logrus.Info(err)
}
} }
func (b *Beemer) handleFileInactive(t *time.Timer, name string) { func (b *Beemer) handleFileInactive(t *time.Timer, name string) {
@ -218,7 +228,7 @@ func (b *Beemer) handleFileInactive(t *time.Timer, name string) {
b.beemChan <- name b.beemChan <- name
} }
func (b *Beemer) executeBeemCommand(oldName string, newName string) { func (b *Beemer) executeBeemCommand(oldName string, newName string) error {
name, args := b.beemCommand(newName, filepath.Dir(oldName)) name, args := b.beemCommand(newName, filepath.Dir(oldName))
@ -231,7 +241,11 @@ func (b *Beemer) executeBeemCommand(oldName string, newName string) {
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
logrus.WithField("name", oldName).WithError(err).Error(string(out)) return err
}
if cmd.ProcessState.ExitCode() != 0 {
return errors.New(fmt.Sprintf("Exit code: %d", cmd.ProcessState.ExitCode()))
} }
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
@ -245,6 +259,7 @@ func (b *Beemer) executeBeemCommand(oldName string, newName string) {
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
logrus.WithField("name", oldName).Error(err) logrus.WithField("name", oldName).Error(err)
} }
return nil
} }
func (b *Beemer) beemFile(filename string) { func (b *Beemer) beemFile(filename string) {
@ -254,6 +269,10 @@ func (b *Beemer) beemFile(filename string) {
if b.tar != nil { if b.tar != nil {
b.tarChan <- newName b.tarChan <- newName
} else { } else {
b.executeBeemCommand(filename, newName) err := b.executeBeemCommand(filename, newName)
if err != nil {
logrus.WithError(err).Error("Error during beem command, reverting file")
_ = moveFile(newName, filename)
}
} }
} }

2
cli.go
View File

@ -8,6 +8,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"regexp" "regexp"
"strings"
"sync" "sync"
"syscall" "syscall"
"time" "time"
@ -89,6 +90,7 @@ func main() {
beemWg: &sync.WaitGroup{}, beemWg: &sync.WaitGroup{},
tarWg: &sync.WaitGroup{}, tarWg: &sync.WaitGroup{},
tarMaxCount: tarMaxCount, tarMaxCount: tarMaxCount,
failDir: strings.TrimRight(watchDir, "/") + ".fail",
} }
if excludePattern != "" { if excludePattern != "" {

1
jenkins/Jenkinsfile vendored
View File

@ -5,6 +5,7 @@ remote.user = env.DEPLOY_USER
remote.identityFile = '/var/lib/jenkins/.ssh/id_rsa' remote.identityFile = '/var/lib/jenkins/.ssh/id_rsa'
remote.knownHosts = '/var/lib/jenkins/.ssh/known_hosts' remote.knownHosts = '/var/lib/jenkins/.ssh/known_hosts'
remote.allowAnyHosts = true remote.allowAnyHosts = true
remote.port = 2299
logLevel = 'FINER' logLevel = 'FINER'
pipeline { pipeline {