Folks,

I’m working on revamping the Gallery/viewer application that I wrote in Django.

One issue I’ve had is that it’s a dynamic on the fly thumbnail creator, but 
I’ve used a homebrew (fairly successfully) cached system to keep track of the 
directories & file contents.  Unlike many of the other gallery style 
applications I don’t require deliberate pre-scanning, the files are detected, 
and thumbnailed in realtime.  

But I realized that I can use Watchdog to simplify that logic, and eliminate 
the need for any caching.

My idea is to have a table that is cleared at every startup, and it just 
contains the directories that have been scanned for that execution of the 
program.  If watchdog has detected a change in a directory, it’s just removed 
from that table, which makes it a candidate for rescanning the next time 
someone views that directory.

But the big kicker is that I can’t find any documentation that can help me add 
watchdog monitoring into Django.  

Now, I know that Django does something similar in the Dev server (which is what 
I primarily use right now)…  And I’ve found a stackoverflow, whichs that I can 
use Signal to capture the SIGINT on shutdown (allowing me to stop watchdog )…  
I should be able to start watchdog in the __init__  file in the APP directory 
as well…  

Now this is the sample app that I used:

import sys
import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler

def on_created(event):
    print(f"hey, {event.src_path} has been created!")

def on_deleted(event):
    print(f"what the f**k! Someone deleted {event.src_path}!")

def on_modified(event):
    print(f"hey buddy, {event.src_path} has been modified")

def on_moved(event):
    print(f"ok ok ok, someone moved {event.src_path} to {event.dest_path}")

if __name__ == "__main__”:

#       <— startup —> 
    patterns = ["*"]
    ignore_patterns = None
    ignore_directories = False
    case_sensitive = True
    my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, 
ignore_directories, case_sensitive)

    my_event_handler.on_created = on_created
    my_event_handler.on_deleted = on_deleted
    my_event_handler.on_modified = on_modified
    my_event_handler.on_moved = on_moved

    path = sys.argv[1]#"."
    go_recursively = True
    my_observer = Observer()
    my_observer.schedule(my_event_handler, path, recursive=go_recursively)

    my_observer.start()

# stop startup

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
# shutdown
        my_observer.stop()
        my_observer.join()


Nothing fancy, just a proof of concept based on code that I found online.

Anyone have any suggestions?

I’m thinking that  I could add the startup .. stop startup to an function in 
the __init__ and run that at startup.
And that capturing the SIGINT, and when SIGINT is tripped, I can do the 
observer.stop & join?

Would it be that simple?  Am I missing something?  Is there a better way to do 
this in Django?  
I don’t want to have to stand up a celery server, if I can run this, or 
something equivalent inside of Django…

I haven’t been able to (yet) trackdown STATRELOADER, which appears to be the 
Django built-in equivalent, so I can’t tell if I can repurpose that to do this 
instead?

I’m open to suggestions…

        - Benjamin

 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/D8BB20B3-0063-4D93-BB39-A39FD66CD860%40schollnick.net.

Reply via email to