Last active
December 16, 2015 07:58
-
-
Save fjallstrom/5402355 to your computer and use it in GitHub Desktop.
simple watchfolder daemon. using mysql to tag files as processed, which isn't perfect. can be changed to redis, mongo or memcache easily.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
import datetime | |
import time | |
import logging | |
try: | |
import MySQLdb as db | |
except ImportError, e: | |
logging.warning(e) | |
connection = None | |
watchfolder = "watchfolder" #relative to where script is running | |
donefolder = "donefolder" #relative to where script is running | |
def diff(a, b): | |
b = set(b) | |
return [aa for aa in a if aa not in b] | |
def watchmenow(): | |
logging.basicConfig(filename="watchfolder.log", level=logging.DEBUG) | |
logging.info("starting watchfolder worker") | |
logging.warning(os.getpid()) | |
lastfilelist = [] | |
try: | |
connection = db.connect("DBHOST", "DBUSER", "DBPASS", "DBNAME"); | |
connection.autocommit(True) | |
cursor = connection.cursor() | |
except db.Error, e: | |
logging.warning(e) | |
sys.exit() | |
while True: | |
try: | |
# find new files, ie the difference between the sets | |
filelist = os.listdir(watchfolder) | |
newfiles = diff(filelist, lastfilelist); | |
for newfile in newfiles: | |
# which file | |
logging.info("found new file: %s", (newfile)) | |
# move file to vhosted folder | |
os.rename(os.path.join(watchfolder, newfile), os.path.join(donefolder, newfile)) | |
logging.info("moving file to vhost: %s", (newfile)) | |
# tag as ready in db | |
sql = "UPDATE filtered_images SET status = 1 WHERE filename = %s" | |
cursor.execute(sql, (newfile)) | |
logging.info("updated mysql status for file: %s", (newfile)) | |
# remove from filelist | |
lastfilelist = filelist | |
except Exception, e: | |
logging.warning(e) | |
time.sleep(10) | |
continue | |
time.sleep(0.7) | |
if __name__ == "__main__": | |
watchmenow() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment