【问题标题】:Watchdog observer not running in container看门狗观察者不在容器中运行
【发布时间】:2018-04-24 20:40:04
【问题描述】:

我正在使用watchdog 来监视文件目录中的文件系统事件。如果这个 watcher 脚本检测到一个事件,我想在数据库中创建一个条目(在本例中为 Mongodb)。要监视的文件系统实际上是将其文件系统链接到我的主机的 Docker 卷。所有代码都在 docker 容器中运行。我可以附加到系统中的任何容器并使用 pymongo 正确地将条目添加到数据库中。

我还可以在主机上运行 watcher 并且所有工作都按预期工作(包括如果容器中的链接文件系统对文件系统发生更改,则按预期工作。

但是,当我在容器中运行观察程序代码时,似乎永远不会调用由文件系统事件触发的事件处理程序中的方法。肯定没有创建 db 条目。

有3个代码来源:

  1. 看门狗目录'watcher'包括一个watcher和一个handler [watchdog_classes]

    import time
    from watchdog.observers import Observer
    import watchdog.events as events
    from data_persist import persistance_interface
    
    db_interface= persistance_interface()
    
    class RepoWatcher:
    
    
    
       def __init__(self, dir_root='/targer_dir/'):
            print(dir_root)
            self.observer = Observer()
            self.dir_root = dir_root
    
        def run(self):
            event_handler = CustomEventHandler()
            self.observer.schedule(event_handler, self.dir_root, recursive=True)
            self.observer.start()
            try:
                while True:
                    time.sleep(5)
            except KeyboardInterrupt:
                self.observer.stop()
                print("Shutting down...")
    
            self.observer.join()
    
    class CustomEventHandler(events.FileSystemEventHandler):
        @staticmethod
        def on_any_event(event):
    
            # Renamed files or dirs
            if isinstance(event, events.FileSystemMovedEvent):
                print("moved")
                db_interface.persist_one({'rename': 'renamed'})
    
            # Created files or dirs
            elif event.event_type == 'created':
                print("created")
                db_interface.persist_one({'create': 'creation'})
    
            # Deleted files or dirs
            elif isinstance(event, events.FileDeletedEvent):
                print("deleted")
                db_interface.persist_one({'deletion': 'deleted'})
    
  2. 用于写入 Mongodb 的类[data_persist 模块]

    import pymongo
    from pymongo import MongoClient
    
    class persistance_interface():
        def __init__(self):
        self.client = MongoClient('db', 27017)
        self.db = self.client.filesystemeventsdb
        self.filesystemevents_collection = self.db.filesystemevents_collection
    
    def persist_one(self, data):
        self.asset_collection.insert_one(data)
    
  3. 触发观察者的脚本[watcher.py]

    import watchdog_classes
    
    watcher = watchdog_classes.RepoWatcher()
    watcher.run()
    

我在同一个 Docker 容器中运行 3 个代码源,mongodb 在另一个容器中。

docker-compose.yaml 看起来像这样:

version: '3'
services:

  db:
    image: tutum/mongodb
    ports:
      - "27017:27017"
    environment:
      - AUTH=no
    entrypoint: "usr/bin/mongod"

  sentry:
    build: ./Docker/sentry
    entrypoint: "python -u run_watcher.py"
    volumes:
      - "C:\\Users\\username\\Desktop\\HostVolume:/Container_Volume"

  web:
    build: ./Docker/site
    ports:
      - "8000:8000"
    command: python -u manage.py runserver 0.0.0.0:8000
    volumes:
      - "C:\\Users\\username\\Desktop\\HostVolume:/Container_Volume"

为什么不调用看门狗处理程序事件?

【问题讨论】:

    标签: python-3.x docker python-watchdog


    【解决方案1】:

    我的问题的根源似乎不在我的代码中,但 Docker for windows 不会通知容器您从 Windows 所做的任何文件更改。这意味着它只能看到容器内对卷所做的更改。

    我找到了一种解决方法here,尽管它需要主机目录连续运行脚本。不理想,但至少它不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多