You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
5.0 KiB
105 lines
5.0 KiB
4 years ago
|
import threading
|
||
|
import queue
|
||
3 years ago
|
import time
|
||
4 years ago
|
|
||
|
# Requests for checking on the site use a pool of thread Workers managed by a Queue.
|
||
|
class update_worker(threading.Thread):
|
||
|
current_uuid = None
|
||
|
|
||
|
def __init__(self, q, notification_q, app, datastore, *args, **kwargs):
|
||
|
self.q = q
|
||
|
self.app = app
|
||
|
self.notification_q = notification_q
|
||
|
self.datastore = datastore
|
||
|
super().__init__(*args, **kwargs)
|
||
|
|
||
|
def run(self):
|
||
3 years ago
|
from changedetectionio import fetch_site_status
|
||
4 years ago
|
|
||
|
update_handler = fetch_site_status.perform_site_check(datastore=self.datastore)
|
||
|
|
||
|
while not self.app.config.exit.is_set():
|
||
|
|
||
|
try:
|
||
|
uuid = self.q.get(block=False)
|
||
|
except queue.Empty:
|
||
|
pass
|
||
|
|
||
|
else:
|
||
|
self.current_uuid = uuid
|
||
3 years ago
|
from changedetectionio import content_fetcher
|
||
4 years ago
|
|
||
|
if uuid in list(self.datastore.data['watching'].keys()):
|
||
3 years ago
|
|
||
|
changed_detected = False
|
||
|
contents = ""
|
||
|
update_obj= {}
|
||
|
|
||
4 years ago
|
try:
|
||
3 years ago
|
now = time.time()
|
||
|
changed_detected, update_obj, contents = update_handler.run(uuid)
|
||
|
|
||
|
# Always record that we atleast tried
|
||
|
self.datastore.update_watch(uuid=uuid, update_obj={'fetch_time': round(time.time() - now, 3)})
|
||
4 years ago
|
|
||
4 years ago
|
except PermissionError as e:
|
||
|
self.app.logger.error("File permission error updating", uuid, str(e))
|
||
3 years ago
|
except content_fetcher.EmptyReply as e:
|
||
|
self.datastore.update_watch(uuid=uuid, update_obj={'last_error':str(e)})
|
||
|
|
||
4 years ago
|
except Exception as e:
|
||
3 years ago
|
self.app.logger.error("Exception reached processing watch UUID:%s - %s", uuid, str(e))
|
||
3 years ago
|
self.datastore.update_watch(uuid=uuid, update_obj={'last_error': str(e)})
|
||
|
|
||
4 years ago
|
else:
|
||
3 years ago
|
if update_obj:
|
||
4 years ago
|
try:
|
||
3 years ago
|
self.datastore.update_watch(uuid=uuid, update_obj=update_obj)
|
||
4 years ago
|
if changed_detected:
|
||
4 years ago
|
|
||
4 years ago
|
# A change was detected
|
||
4 years ago
|
newest_version_file_contents = ""
|
||
3 years ago
|
fname = self.datastore.save_history_text(watch_uuid=uuid, contents=contents)
|
||
|
|
||
|
# Update history with the stripped text for future reference, this will also mean we save the first
|
||
|
# Should always be keyed by string(timestamp)
|
||
|
self.datastore.update_watch(uuid, {"history": {str(update_obj["last_checked"]): fname}})
|
||
|
|
||
4 years ago
|
watch = self.datastore.data['watching'][uuid]
|
||
|
|
||
3 years ago
|
print (">> Change detected in UUID {} - {}".format(uuid, watch['url']))
|
||
|
|
||
|
# Get the newest snapshot data to be possibily used in a notification
|
||
4 years ago
|
newest_key = self.datastore.get_newest_history_key(uuid)
|
||
|
if newest_key:
|
||
|
with open(watch['history'][newest_key], 'r') as f:
|
||
|
newest_version_file_contents = f.read().strip()
|
||
|
|
||
|
n_object = {
|
||
3 years ago
|
'watch_url': watch['url'],
|
||
4 years ago
|
'uuid': uuid,
|
||
|
'current_snapshot': newest_version_file_contents
|
||
|
}
|
||
|
|
||
4 years ago
|
# Did it have any notification alerts to hit?
|
||
|
if len(watch['notification_urls']):
|
||
3 years ago
|
print(">>> Notifications queued for UUID from watch {}".format(uuid))
|
||
4 years ago
|
n_object['notification_urls'] = watch['notification_urls']
|
||
4 years ago
|
self.notification_q.put(n_object)
|
||
|
|
||
|
# No? maybe theres a global setting, queue them all
|
||
|
elif len(self.datastore.data['settings']['application']['notification_urls']):
|
||
3 years ago
|
print(">>> Watch notification URLs were empty, using GLOBAL notifications for UUID: {}".format(uuid))
|
||
4 years ago
|
n_object['notification_urls'] = self.datastore.data['settings']['application']['notification_urls']
|
||
4 years ago
|
self.notification_q.put(n_object)
|
||
3 years ago
|
else:
|
||
|
print(">>> NO notifications queued, watch and global notification URLs were empty.")
|
||
4 years ago
|
|
||
4 years ago
|
except Exception as e:
|
||
|
print("!!!! Exception in update_worker !!!\n", e)
|
||
|
|
||
|
self.current_uuid = None # Done
|
||
|
self.q.task_done()
|
||
|
|
||
|
self.app.config.exit.wait(1)
|