summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-08-05 14:15:32 +0200
committerBernhard Posselt <nukeawhale@gmail.com>2013-08-05 14:16:09 +0200
commit9e922dddf3cd161bd1357c21f6834fd4b7323eef (patch)
tree0c9e98b35f0625a199c69cb4c2b196d1df20e543
parent867c56336eef6ccd219c9d3c951de9c865f1e177 (diff)
implemented threading, not tested yet
-rw-r--r--bin/updater.py76
1 files changed, 48 insertions, 28 deletions
diff --git a/bin/updater.py b/bin/updater.py
index 669324f58..65236ae0c 100644
--- a/bin/updater.py
+++ b/bin/updater.py
@@ -27,18 +27,38 @@ import sys
import time
import json
import argparse
-import queue
+import threading
import urllib.request
import urllib.error
+class UpdateThread(threading.Thread):
+
+ lock = threading.Lock()
+
+ def __init__(self, feeds, update_url):
+ super().__init__()
+ self.feeds = feeds
+ self.update_url = update_url
+
+
+ def run(self):
+ with lock:
+ if len(self.feeds) > 0:
+ feed = self.feeds.pop()
+ # call the update method of one feed
+ data = urllib.parse.urlencode(feed)
+ urllib.request.urlopen(self.update_url, data)
+ self.run()
+ else:
+ self.exit()
+
class Updater:
- def __init__(self, base_url, user, password, threads):
+ def __init__(self, base_url, thread_num, interval):
- self.threads = threads
- self.user = user
- self.password = password
+ self.thread_num = thread_num
+ self.interval = interval
self.base_url = base_url
if self.base_url[-1] != '/':
@@ -52,19 +72,25 @@ class Updater:
def run(self):
try:
- auth = urllib.request.HTTPPasswordMgrWithDefaultRealm()
- auth.add_password(None, self.base_url, self.user, self.password)
- auth_handler = urllib.request.HTTPBasicAuthHandler(auth)
-
- opener = urllib.request.build_opener(auth_handler)
- urllib.request.install_opener(opener)
-
+ # run the cleanup request and get all the feeds to update
urllib.request.urlopen(self.cleanup_url)
feeds_response = urllib.request.urlopen(self.all_feeds_url)
feeds_json = str( feeds_response.read() )
feeds = json.loads(feeds_json)
- # TODO: create feeds requests and thread the requests
+ # start thread_num for feeds
+ threads = []
+ for i in range(0, self.thread_num):
+ thread = UpdateThread(feeds, self.update_url)
+ thread.start()
+ threads.append(thread)
+
+ for thread in threads:
+ thread.join()
+
+ # wait until the interval finished to run again
+ time.sleep(self.interval)
+ self.run()
# TODO: also check for the other URLErrors
@@ -73,18 +99,6 @@ class Updater:
exit(1)
-class Daemon:
-
- def run(self, timeout, runner):
- """
- This is for running the updater with a certain timeout between the
- updates
- """
- runner.run()
- time.sleep(timeout)
- run(timeout, runner)
-
-
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--threads',
@@ -102,9 +116,15 @@ def main():
help='The URL where owncloud is installed')
args = parser.parse_args()
- updater = Updater(args.url, args.user, args.password, args.threads)
- daemon = Daemon()
- daemon.run(args.interval, updater)
+ # register user and password for a certain url
+ auth = urllib.request.HTTPPasswordMgrWithDefaultRealm()
+ auth.add_password(None, args.url, args.user, args.password)
+ auth_handler = urllib.request.HTTPBasicAuthHandler(auth)
+ opener = urllib.request.build_opener(auth_handler)
+ urllib.request.install_opener(opener)
+
+ # create the updater and run the threads
+ updater = Updater(args.url, args.threads, args.interval)
if __name__ == '__main__':