mirror of
https://github.com/simon987/beemer.git
synced 2025-04-20 08:16:42 +00:00
cap maximum number of transfers, don't ignore existing files
This commit is contained in:
parent
50f6b69b40
commit
b0895015ea
@ -12,11 +12,13 @@ NAME:
|
|||||||
beemer - Execute a command on a file after a delay of inactivity
|
beemer - Execute a command on a file after a delay of inactivity
|
||||||
|
|
||||||
GLOBAL OPTIONS:
|
GLOBAL OPTIONS:
|
||||||
--command value, -c value Will be executed on file write. You can use %file and %dir. Example: "rclone move %file remote:/beem/%dir"
|
--transfers value, -t value Number of simultaneous transfers (default: 10)
|
||||||
|
--command value, -c value Will be executed on file write. You can use %file, %name and %dir. Example: "rclone move %file remote:/beem/%dir"
|
||||||
--wait DELAY, -w DELAY Files will be beemed after DELAY of inactivity (default: 10s)
|
--wait DELAY, -w DELAY Files will be beemed after DELAY of inactivity (default: 10s)
|
||||||
--directory DIRECTORY, -d DIRECTORY DIRECTORY to watch. If non-empty, its current files & subdirectories will be ignored
|
--directory DIRECTORY, -d DIRECTORY DIRECTORY to watch.
|
||||||
--help, -h show help
|
--help, -h show help
|
||||||
--version, -v print the version
|
--version, -v print the version
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Examples
|
### Examples
|
||||||
|
61
main.go
61
main.go
@ -16,6 +16,7 @@ type Ctx struct {
|
|||||||
FileMap map[string]*File
|
FileMap map[string]*File
|
||||||
TempDir string
|
TempDir string
|
||||||
BeemCommand func(string, string) (string, []string)
|
BeemCommand func(string, string) (string, []string)
|
||||||
|
BeemChan chan string
|
||||||
}
|
}
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
@ -41,8 +42,15 @@ func main() {
|
|||||||
|
|
||||||
var cmdString string
|
var cmdString string
|
||||||
var watchDir string
|
var watchDir string
|
||||||
|
var transfers int
|
||||||
|
|
||||||
app.Flags = []cli.Flag{
|
app.Flags = []cli.Flag{
|
||||||
|
cli.IntFlag{
|
||||||
|
Name: "transfers, t",
|
||||||
|
Usage: "Number of simultaneous transfers",
|
||||||
|
Destination: &transfers,
|
||||||
|
Value: 10,
|
||||||
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "command, c",
|
Name: "command, c",
|
||||||
Usage: "Will be executed on file write. You can use %file, %name and %dir. " +
|
Usage: "Will be executed on file write. You can use %file, %name and %dir. " +
|
||||||
@ -57,7 +65,7 @@ func main() {
|
|||||||
},
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "directory, d",
|
Name: "directory, d",
|
||||||
Usage: "`DIRECTORY` to watch. If non-empty, its current files & subdirectories will be ignored",
|
Usage: "`DIRECTORY` to watch.",
|
||||||
Destination: &watchDir,
|
Destination: &watchDir,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -67,6 +75,7 @@ func main() {
|
|||||||
if !c.IsSet("directory") {
|
if !c.IsSet("directory") {
|
||||||
return errors.New("Directory must be specified")
|
return errors.New("Directory must be specified")
|
||||||
}
|
}
|
||||||
|
ctx.BeemChan = make(chan string, transfers)
|
||||||
|
|
||||||
initTempDir()
|
initTempDir()
|
||||||
ctx.BeemCommand = parseCommand(cmdString)
|
ctx.BeemCommand = parseCommand(cmdString)
|
||||||
@ -80,13 +89,13 @@ func main() {
|
|||||||
|
|
||||||
go handleWatcherEvents(watcher)
|
go handleWatcherEvents(watcher)
|
||||||
|
|
||||||
logrus.WithField("dir", watchDir).Info("Watching directory for changes")
|
initWatchDir(watchDir, watcher)
|
||||||
err = watcher.Add(watchDir)
|
|
||||||
if err != nil {
|
|
||||||
logrus.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO gracefully handle SIGINT
|
//TODO gracefully handle SIGINT
|
||||||
|
for i := 0; i < transfers; i++ {
|
||||||
|
go beemer()
|
||||||
|
}
|
||||||
|
|
||||||
done := make(chan bool)
|
done := make(chan bool)
|
||||||
<-done
|
<-done
|
||||||
|
|
||||||
@ -99,6 +108,35 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initWatchDir(watchDir string, watcher *fsnotify.Watcher) {
|
||||||
|
|
||||||
|
logrus.WithField("dir", watchDir).Info("Watching directory for changes")
|
||||||
|
|
||||||
|
err := watcher.Add(watchDir)
|
||||||
|
_ = filepath.Walk(watchDir, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if info.IsDir() {
|
||||||
|
err := handleDirChange(fsnotify.Event{
|
||||||
|
Name: path,
|
||||||
|
Op: fsnotify.Create,
|
||||||
|
}, watcher)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
handleFileChange(fsnotify.Event{
|
||||||
|
Name: path,
|
||||||
|
Op: fsnotify.Create,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
logrus.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func getAndResetTimer(name string) *time.Timer {
|
func getAndResetTimer(name string) *time.Timer {
|
||||||
|
|
||||||
file, ok := ctx.FileMap[name]
|
file, ok := ctx.FileMap[name]
|
||||||
@ -185,6 +223,15 @@ func handleWatcherEvents(watcher *fsnotify.Watcher) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func beemer() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case name := <-ctx.BeemChan:
|
||||||
|
beemFile(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func handleFileInactive(t *time.Timer, name string) {
|
func handleFileInactive(t *time.Timer, name string) {
|
||||||
<-t.C
|
<-t.C
|
||||||
|
|
||||||
@ -194,7 +241,7 @@ func handleFileInactive(t *time.Timer, name string) {
|
|||||||
"name": name,
|
"name": name,
|
||||||
}).Infof("has been inactive for %s and will be beemed", InactiveDelay)
|
}).Infof("has been inactive for %s and will be beemed", InactiveDelay)
|
||||||
|
|
||||||
beemFile(name)
|
ctx.BeemChan <- name
|
||||||
}
|
}
|
||||||
|
|
||||||
func beemFile(filename string) {
|
func beemFile(filename string) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user