Directory Watcher

If you have a question or need help, this is the place to be.
archmisha
Posts: 5
Joined: Mon Apr 25, 2011 8:09 am

Directory Watcher

Post by archmisha » Mon Apr 25, 2011 9:06 am

Hi,
When i start copy large files (*.mkv), DirectoryWatcher event fired before the file finished the copy.
Thus resulting in all the automation process running while the file is still being copying into the monitored directory....
then when the file copy is finished, some-why the directory watcher event fired again and the process actually processes the file.

Is there a way to prevent from the event to be fired until the file finished copying, or recognizing in the event that the file is not ready yet and canceling the vent somehow?

Thanks

eatmeimadanish
Experienced User
Posts: 112
Joined: Thu Oct 01, 2009 5:11 pm

Re: Directory Watcher

Post by eatmeimadanish » Mon Apr 25, 2011 5:55 pm

http://eventghost.org/forum/viewtopic.p ... b90ac179d7

You will want to add a script in your logic to detect if the file is "in use" or not. If the file is locked leave it alone and check back in an interval (30 seconds) once its not locked you can have the script send an event to trigger the next step in the process.

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Directory Watcher

Post by Pako » Tue Apr 26, 2011 8:44 am

One possible solution is here.
The first picture is your current configuration:
DirWatcher-OldConfig.gif
DirWatcher-OldConfig.gif (3.74 KiB) Viewed 15487 times
The second picture is a new configuration:
DirWatcher-NewConfig.gif
DirWatcher-NewConfig.gif (5.04 KiB) Viewed 15487 times
The Python Script contains the following code:

Code: Select all

def CheckFileAccessFunction_1(filePath):
    from os import path
    f = None
    if path.exists(filePath):
        try:
            f = open(filePath, 'ab+', 2)
            if f:
                eg.TriggerEvent("AccessAllowed",prefix="DirectoryWatcher", payload=(filePath,))
#        except IOError, info:
#            print "message =",info
        except:
            eg.scheduler.AddTask(5.0, CheckFileAccessFunction_1, filePath)
        finally:
            if f:
                f.close()

check_list = [item[2][0] for item in eg.scheduler.__dict__['heap'] if item[1] and item[1].__name__ == 'CheckFileAccessFunction_1']
for item in eg.event.payload:
    if item in check_list:
        continue
    else:
        eg.scheduler.AddTask(10.0, CheckFileAccessFunction_1, item)
Pako

archmisha
Posts: 5
Joined: Mon Apr 25, 2011 8:09 am

Re: Directory Watcher

Post by archmisha » Fri Apr 29, 2011 8:18 am

awesome thanks

Can u explain the check_list assignment.
I understand u skip its content

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Directory Watcher

Post by Pako » Fri Apr 29, 2011 8:42 am

archmisha wrote:awesome thanks

Can u explain the check_list assignment.
I understand u skip its content
Please - can type your question in other words?
My English sucks.

Pako

archmisha
Posts: 5
Joined: Mon Apr 25, 2011 8:09 am

Re: Directory Watcher

Post by archmisha » Sat Apr 30, 2011 7:02 pm

No problem.
Please explain the line:

Code: Select all

check_list = [item[2][0] for item in eg.scheduler.__dict__['heap'] if item[1] and item[1].__name__ == 'CheckFileAccessFunction_1']
what does it do?

archmisha
Posts: 5
Joined: Mon Apr 25, 2011 8:09 am

Re: Directory Watcher

Post by archmisha » Sat Apr 30, 2011 7:23 pm

There is a problem with this code.
AccessAllowed is triggered while the file is still being copied into the watched directory
(for example copy from network takes a long time)

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Directory Watcher

Post by Pako » Mon May 02, 2011 12:34 pm

archmisha wrote:No problem.
Please explain the line:

Code: Select all

check_list = [item[2][0] for item in eg.scheduler.__dict__['heap'] if item[1] and item[1].__name__ == 'CheckFileAccessFunction_1']
what does it do?
With my English is quite difficult to explain. But I will try:
1. Open PyCrust window (using the Help-Python shell menu).
2. In the panel Namespace, find the item eg.scheduler.__dict__ ['heap'] and click on it.
On the right you will see a list of currently scheduled tasks (the list Value:).
3. If the copy is in the progress (into your checked folder), there will be something like this:
(1304335977.0699999, <function CheckFileAccessFunction_1 at 0x05724DB0>, (u'F:\\FileTransfer\\Sample.mkv',), {})
This means that each schedule is recorded with a single tuple with the following items: [0] = start time, [1] = function to run, [2] = function arguments
4. Now you can already understand how it works, but I will add something else: search keywords Comprehensions List (for example here).
5. How works the filter if item[1] and item[1].__name__ == 'CheckFileAccessFunction_1'.
if item[1] is there for the reason that in the list of schedules has also been the "schedule" (1404328946.711, None, None, None), so that the item[1] = None and therefore can not be item[1].__name__.
item[1].__name__ is there for this reason that we are only interested in schedules related to our functions CheckFileAccessFunction_1.

I hope this is comprehensible.

Pako

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Directory Watcher

Post by Pako » Mon May 02, 2011 12:40 pm

archmisha wrote:There is a problem with this code.
AccessAllowed is triggered while the file is still being copied into the watched directory
(for example copy from network takes a long time)
I do not know why. It does not matter how long it is copying.
This problem occurs only on network drives or, in normal as well?

Pako

archmisha
Posts: 5
Joined: Mon Apr 25, 2011 8:09 am

Re: Directory Watcher

Post by archmisha » Thu May 19, 2011 7:47 pm

Pako, Thank you for the explanation. It is good :)

The above code does not work for me when copying on network, when copy local it is so fast that I dont see if there is problem
My fix to this is to add the following:

Code: Select all

f = open(filePath, 'ab+', 2)
            fd = f.fileno()
            size = path.getsize(filePath)            
            msvcrt.locking(fd, msvcrt.LK_RLCK, size)
            msvcrt.locking(fd, msvcrt.LK_UNLCK, size)
            if f:
                  <trigger the rest of the process>
This way I try to lock file to check if copy is finished.

Lunatixz
Posts: 15
Joined: Wed Jul 07, 2010 7:16 pm

Re: Directory Watcher

Post by Lunatixz » Wed Feb 29, 2012 3:45 pm

I'm getting an error when I try to trigger the python script:
Traceback (most recent call last):
Python script "19", line 18, in <module>
for item in eg.event.payload:
TypeError: 'NoneType' object is not iterable

Any Suggestions?

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Directory Watcher

Post by Pako » Wed Feb 29, 2012 4:17 pm

Maybe you do something wrong?

Pako

Lunatixz
Posts: 15
Joined: Wed Jul 07, 2010 7:16 pm

Re: Directory Watcher

Post by Lunatixz » Wed Feb 29, 2012 4:22 pm

Pako wrote:Maybe you do something wrong?

Pako
does the script have to be configured? does it have to be in the folder the directory watcher is triggered in?

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Directory Watcher

Post by Pako » Wed Feb 29, 2012 4:44 pm

Excuse me for my previous answer to your question but I could not do better. Even now, it's not any better.
How can we know which script (exactly) you use?
How can we know how you incorporate it into your configuration?
You do not know how to attach an attachment?
Preferably in such a way that everyone can accurately simulate what your situation.

Pako

celtics101
Posts: 2
Joined: Mon Jun 25, 2012 5:37 am

Re: Directory Watcher

Post by celtics101 » Mon Jun 25, 2012 5:43 am

Hi! ive been using eventghost to automate my media center, and i have just recently tried to use it with a program called "iview downloader" - (basicaly Australia's version of iplayer). Anyway, the program saves an online stream, so the file appears way before it finishes downloading. (thats why ive tried to use this script) however, it dosent seem to register that the files are still being written, any ideas?

Post Reply