Best way to approach file watcher?

agolangf · · 555 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>What is the best way to approach the following scenario? </p> <p>I need to watch a directory in a remote server and check if certain files make it to the directory at a specific time or within an interval. Say I have 12 files that should be in the directory at 10 am. How should I go about doing this? Should I use a 3rd party library to make this quicker and faster? Does even one exist? Or should I just make a file watcher that exists on the machine that sends notifications? </p> <hr/>**评论:**<br/><br/>jerf: <pre><p>If you can get away with just running a scheduled job that makes whatever assertions you are interested in, that&#39;s the easiest thing by far.</p> <p>If you need instant feedback, there is an <a href="https://github.com/fsnotify/fsnotify">fsnotify</a> for cross-platform notification.</p> <p>However, be aware that the lowest common denominator for file watching is really, <em>really</em> low, and in particular, does not include &#34;watch a directory recursively&#34;, because Linux does not support that.</p> <p>You should also be aware that when that gives you instant feedback, that gives you <em>really</em> instant feedback; if a large file is being written, you&#39;ll be notified about it potentially when just the first block has been written, and will get a lot of notifications as the file is written. You&#39;ll need to debounce that yourself, and AFAIK, you&#39;ll need to figure out for yourself when the file is &#34;done&#34; being written. (This is a characteristic of watching the file system, not fsnotify in particular.)</p> <p>If you don&#39;t need <em>instant</em> feedback, you should probably just poll every so often.</p></pre>LiquidShadowFox: <pre><p>No it doesn&#39;t need to be instant, within 30 minutes should be fine for example. So what do you suggest? I poll the directory using ftp or something else? </p></pre>jerf: <pre><p>Yeah... it may feel klunky, but it&#39;s got the best bang for the buck by far.</p></pre>saltytrashcan: <pre><p>I would recommend rsync, either push or pull. You can use SSH keys for authentication. If you use the flags -avz it will handle compression (in transit) and recursion for you.</p></pre>epiris: <pre><p>Sounds like you can get away with simply traversing the list of files from the interesting directory. What’s up in the air it sounds like is how to obtain that list, you would need to describe more details about the remote constraint. Does it have a running service you could interface with? Ssh? Etc.</p></pre>radovskyb: <pre><p>Feel free to look at watcher (<a href="https://github.com/radovskyb/watcher" rel="nofollow">https://github.com/radovskyb/watcher</a>) if you need recursive directory polling (been a little while since I&#39;ve made any updates)</p></pre>Emacs24: <pre><p>I don&#39;t know all details, so local watcher on remote machine + notifications looks like an optimal apprach. It is hardly the best task to be solved in golang though. I dare to say bash + <a href="https://www.opennet.ru/man.shtml?topic=inotifywait&amp;category=1" rel="nofollow">inotify tools</a> + cron are better in this case.</p></pre>zevdg: <pre><p>If you only need to check it at specified times, I&#39;d just use <a href="https://golang.org/pkg/net/http/#FileServer" rel="nofollow">https://golang.org/pkg/net/http/#FileServer</a> on the remote machine and just write a simple http query and drop it in a cron job on the client machine. Being notified when files appear isn&#39;t too hard, but it&#39;s complexity you can avoid if you don&#39;t need real time updates.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

555 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传