Git Product home page Git Product logo

Comments (6)

dengsgo avatar dengsgo commented on May 24, 2024

文件夹删除没有事件通知

可以贴下你的 filegirl.yaml 配置吗,这样会帮助我更快的复现使用场景

from fileboy.

huangpd avatar huangpd commented on May 24, 2024

`# 主配置
core:
# 配置版本号
version: 1.15

监控配置

monitor:
# 要监听的目录
# test1 监听当前目录下 test1 目录
# test1/test2 监听当前目录下 test1/test2 目录
# test1,* 监听当前目录下 test1 目录及其所有子目录(递归)
# .,* 监听当前目录及其所有子目录(递归)
includeDirs:
- .,*

# 不监听的目录
# .idea   忽略.idea目录及其所有子目录的监听
exceptDirs:
    - .idea
    - .git
    - .vscode

# 监听文件的格式,此类文件更改会执行 command 中的命令
# .go   后缀为 .go 的文件更改,会执行 command 中的命令
# .*    所有的文件更改都会执行 command 中的命令
types:
     - .*

# 监听的事件类型,发生此类事件才执行 command 中的命令
# 没有该配置默认监听所有事件
# write   写入文件事件
# rename  重命名文件事件
# remove  移除文件事件
# create  创建文件事件
# chmod   更新文件权限事件(类unix)
events:
    - write
    - rename
    - remove
    - create

命令

command:
# 监听的文件有更改会执行的命令
# 可以有多条命令,会依次执行
# 如有多条命令,每条命令都会等待上一条命令执行完毕后才会执行
# 如遇交互式命令,允许外部获取输入
# 支持变量占位符,运行命令时会替换成实际值:
# {{file}} 文件名(如 a.txt 、test/test2/a.go)
# {{ext}} 文件后缀(如 .go)
# {{event}} 事件(上面的events, 如 write)
# {{changed}} 文件更新的本地时间戳(纳秒,如 1537326690523046400)
# 变量占位符使用示例:cp {{file}} /root/sync -rf 、 myCommand --{{ext}} {{changed}}
exec:

# 文件变更后命令在xx毫秒后才会执行,单位为毫秒
# 一个变更事件(A)如果在定义的延迟时间(t)内, 又有新的文件变更事件(B), 那么A会取消执行。
# B及以后的事件均依次类推,直到事件Z在t内没有新事件产生,Z 会执行
# 合理设置延迟时间,将有效减少冗余和重复任务的执行
# 如果不需要该特性,设置为 0
delayMillSecond: 1000

通知器

notifier:
# 文件更改会向该 url 发送请求(POST 一段 json 文本数据)
# 触发请求的时机和执行 command 命令是一致的
# 请求超时 15 秒
# POST 格式:
# Content-Type: application/json;charset=UTF-8
# User-Agent: FileBoy Net Notifier v1.15
# Body: {"project_folder":"/project/path","file":"main.go","changed":1576567861913824940,"ext":".go","event":"write"}
# 例: http://example.com/notifier/fileboy-listener
# 不启用通知,请留空 ""
callUrl: "http://127.0.0.1:5000/accept"

特殊指令

instruction:
# 可以通过特殊的指令选项来控制 command 的行为,指令可以有多个
# 指令选项解释:
- exec-when-start fileboy启动就绪后,自动执行一次 'exec' 定义的命令
# should-finish 触发执行 'exec' 时(C),如果上一次的命令(L)未退出(还在执行),会等待 L 退出(而不是强制 kill ),直到 L 有明确 exit code 才会开始执行本次命令。
# 在等待 L 退出时,又有新事件触发了命令执行(N),则 C 执行取消,只会保留最后一次的 N 执行
# ignore-stdout 执行 'exec' 产生的 stdout 会被丢弃
# ignore-warn fileboy 自身的 warn 信息会被丢弃
# ignore-info fileboy 自身的 info 信息会被丢弃
# ignore-exec-error 执行 'exec' 出错仍继续执行下面的命令而不退出

#- should-finish
#- exec-when-start
- ignore-warn

`

from fileboy.

huangpd avatar huangpd commented on May 24, 2024
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/fsnotify/fsnotify"
)

type NotifyFile struct {
	watch *fsnotify.Watcher
}

func NewNotifyFile() *NotifyFile {
	w := new(NotifyFile)
	w.watch, _ = fsnotify.NewWatcher()
	return w
}

//监控目录
func (this *NotifyFile) WatchDir(dir string) {
	//通过Walk来遍历目录下的所有子目录
	filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
		//判断是否为目录,监控目录,目录下文件也在监控范围内,不需要加
		if info.IsDir() {
			path, err := filepath.Abs(path)
			if err != nil {
				return err
			}
			err = this.watch.Add(path)
			if err != nil {
				return err
			}
			fmt.Println("监控 : ", path)
		}
		return nil
	})

	go this.WatchEvent() //协程
}

func (this *NotifyFile) WatchEvent() {
	for {
		select {
		case ev := <-this.watch.Events:
			{
				if ev.Op&fsnotify.Create == fsnotify.Create {
					fmt.Println("创建文件 : ", ev.Name)
					//获取新创建文件的信息,如果是目录,则加入监控中
					file, err := os.Stat(ev.Name)
					if err == nil && file.IsDir() {
						this.watch.Add(ev.Name)
						fmt.Println("添加监控 : ", ev.Name)
					}
				}

				if ev.Op&fsnotify.Write == fsnotify.Write {
					//fmt.Println("写入文件 : ", ev.Name)
				}

				if ev.Op&fsnotify.Remove == fsnotify.Remove {
					fmt.Println("删除文件 : ", ev.Name)
					//如果删除文件是目录,则移除监控
					fi, err := os.Stat(ev.Name)
					fmt.Println("dir:", fi)

					if err == nil && fi.IsDir() {
						this.watch.Remove(ev.Name)
						fmt.Println("删除监控 : ", ev.Name)
					}
				}

				if ev.Op&fsnotify.Rename == fsnotify.Rename {
					//如果重命名文件是目录,则移除监控 ,注意这里无法使用os.Stat来判断是否是目录了
					//因为重命名后,go已经无法找到原文件来获取信息了,所以简单粗爆直接remove
					fmt.Println("重命名文件 : ", ev.Name)
					this.watch.Remove(ev.Name)
				}
				if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
					fmt.Println("修改权限 : ", ev.Name)
				}
			}
		case err := <-this.watch.Errors:
			{
				fmt.Println("error : ", err)
				return
			}
		}
	}
}

func main() {
	watch := NewNotifyFile()
	watch.WatchDir("/Users/hpd/ceshi")
	select {}
	return
}

这段代码移除文件夹没有问题

from fileboy.

huangpd avatar huangpd commented on May 24, 2024

我昨天试了试 我的mac版本移除文件夹有问题,windows没有问题

from fileboy.

dengsgo avatar dengsgo commented on May 24, 2024

fielgirl.yaml 中的 command -> delayMillSecond 设置成 0 ,再试试。可以贴一下提供控制台的输出.

from fileboy.

huangpd avatar huangpd commented on May 24, 2024

image
移除是这样的一个事件

from fileboy.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.