|
|
@ -25,19 +25,24 @@ import datetime
|
|
|
|
import timeago
|
|
|
|
import timeago
|
|
|
|
|
|
|
|
|
|
|
|
import threading
|
|
|
|
import threading
|
|
|
|
|
|
|
|
import queue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from flask import Flask, render_template, request, send_file, send_from_directory, safe_join, abort, redirect, url_for
|
|
|
|
from flask import Flask, render_template, request, send_file, send_from_directory, safe_join, abort, redirect, url_for
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Local
|
|
|
|
# Local
|
|
|
|
import store
|
|
|
|
import store
|
|
|
|
import fetch_site_status
|
|
|
|
import fetch_site_status
|
|
|
|
|
|
|
|
running_update_threads = []
|
|
|
|
ticker_thread = None
|
|
|
|
ticker_thread = None
|
|
|
|
|
|
|
|
|
|
|
|
datastore = store.ChangeDetectionStore()
|
|
|
|
datastore = store.ChangeDetectionStore()
|
|
|
|
messages = []
|
|
|
|
messages = []
|
|
|
|
extra_stylesheets = []
|
|
|
|
extra_stylesheets = []
|
|
|
|
running_update_threads = {}
|
|
|
|
|
|
|
|
|
|
|
|
update_q = queue.Queue()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__, static_url_path='/static')
|
|
|
|
app = Flask(__name__, static_url_path='/static')
|
|
|
|
app.config['STATIC_RESOURCES'] = "/app/static"
|
|
|
|
app.config['STATIC_RESOURCES'] = "/app/static"
|
|
|
@ -52,9 +57,9 @@ app.config['TEMPLATES_AUTO_RELOAD'] = True
|
|
|
|
# running or something similar.
|
|
|
|
# running or something similar.
|
|
|
|
@app.template_filter('format_last_checked_time')
|
|
|
|
@app.template_filter('format_last_checked_time')
|
|
|
|
def _jinja2_filter_datetime(watch_obj, format="%Y-%m-%d %H:%M:%S"):
|
|
|
|
def _jinja2_filter_datetime(watch_obj, format="%Y-%m-%d %H:%M:%S"):
|
|
|
|
global running_update_threads
|
|
|
|
# Worker thread tells us which UUID it is currently processing.
|
|
|
|
if watch_obj['uuid'] in running_update_threads:
|
|
|
|
for t in running_update_threads:
|
|
|
|
if running_update_threads[watch_obj['uuid']].is_alive():
|
|
|
|
if t.current_uuid == watch_obj['uuid']:
|
|
|
|
return "Checking now.."
|
|
|
|
return "Checking now.."
|
|
|
|
|
|
|
|
|
|
|
|
if watch_obj['last_checked'] == 0:
|
|
|
|
if watch_obj['last_checked'] == 0:
|
|
|
@ -261,8 +266,8 @@ def selfcheck():
|
|
|
|
if not uuid in path:
|
|
|
|
if not uuid in path:
|
|
|
|
output = "Something weird in {}, suspected incorrect snapshot path.".format(uuid)
|
|
|
|
output = "Something weird in {}, suspected incorrect snapshot path.".format(uuid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return output
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/static/<string:group>/<string:filename>", methods=['GET'])
|
|
|
|
@app.route("/static/<string:group>/<string:filename>", methods=['GET'])
|
|
|
|
def static_content(group, filename):
|
|
|
|
def static_content(group, filename):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
@ -292,17 +297,12 @@ def api_delete():
|
|
|
|
return redirect(url_for('main_page'))
|
|
|
|
return redirect(url_for('main_page'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/api/checknow", methods=['GET'])
|
|
|
|
@app.route("/api/checknow", methods=['GET'])
|
|
|
|
def api_watch_checknow():
|
|
|
|
def api_watch_checknow():
|
|
|
|
global messages
|
|
|
|
global messages
|
|
|
|
|
|
|
|
|
|
|
|
uuid = request.args.get('uuid')
|
|
|
|
uuid = request.args.get('uuid')
|
|
|
|
|
|
|
|
update_q.put(uuid)
|
|
|
|
running_update_threads[uuid] = fetch_site_status.perform_site_check(uuid=uuid,
|
|
|
|
|
|
|
|
datastore=datastore)
|
|
|
|
|
|
|
|
running_update_threads[uuid].start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tag = request.args.get('tag')
|
|
|
|
tag = request.args.get('tag')
|
|
|
|
return redirect(url_for('main_page', tag=tag))
|
|
|
|
return redirect(url_for('main_page', tag=tag))
|
|
|
@ -310,49 +310,64 @@ def api_watch_checknow():
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/api/recheckall", methods=['GET'])
|
|
|
|
@app.route("/api/recheckall", methods=['GET'])
|
|
|
|
def api_watch_recheckall():
|
|
|
|
def api_watch_recheckall():
|
|
|
|
import fetch_site_status
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global running_update_threads
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
for uuid, watch in datastore.data['watching'].items():
|
|
|
|
for uuid, watch in datastore.data['watching'].items():
|
|
|
|
i = i + 1
|
|
|
|
update_q.put(uuid)
|
|
|
|
|
|
|
|
|
|
|
|
running_update_threads[watch['uuid']] = fetch_site_status.perform_site_check(uuid=uuid,
|
|
|
|
|
|
|
|
datastore=datastore)
|
|
|
|
|
|
|
|
running_update_threads[watch['uuid']].start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return "{} triggered recheck of {} watches.".format(i, len(datastore.data['watching']))
|
|
|
|
return "Triggered recheck of {} watches.".format(len(datastore.data['watching']))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Can be used whenever, launch threads that need launching to update the stored information
|
|
|
|
# Requests for checking on the site use a pool of thread Workers managed by a Queue.
|
|
|
|
def launch_checks():
|
|
|
|
class Worker(threading.Thread):
|
|
|
|
import fetch_site_status
|
|
|
|
|
|
|
|
global running_update_threads
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current_uuid = None
|
|
|
|
|
|
|
|
|
|
|
|
minutes = datastore.data['settings']['requests']['minutes_between_check']
|
|
|
|
def __init__(self, q, *args, **kwargs):
|
|
|
|
for uuid, watch in datastore.data['watching'].items():
|
|
|
|
self.q = q
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
#@Todo https://pymotw.com/2/Queue/
|
|
|
|
def run(self):
|
|
|
|
if watch['last_checked'] <= time.time() - (minutes * 60):
|
|
|
|
try:
|
|
|
|
running_update_threads[watch['uuid']] = fetch_site_status.perform_site_check(uuid=uuid,
|
|
|
|
while True:
|
|
|
|
datastore=datastore)
|
|
|
|
uuid = self.q.get() # Blocking
|
|
|
|
running_update_threads[watch['uuid']].start()
|
|
|
|
self.current_uuid = uuid
|
|
|
|
|
|
|
|
fetch_site_status.perform_site_check(uuid=uuid, datastore=datastore)
|
|
|
|
|
|
|
|
self.current_uuid = None # Done
|
|
|
|
|
|
|
|
self.q.task_done()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# Thread runner to check every minute
|
|
|
|
# Thread runner to check every minute, look for new watches to feed into the Queue.
|
|
|
|
def ticker_thread_check_time_launch_checks():
|
|
|
|
def ticker_thread_check_time_launch_checks():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Spin up Workers.
|
|
|
|
|
|
|
|
for _ in range(datastore.data['settings']['requests']['workers']):
|
|
|
|
|
|
|
|
new_worker = Worker(update_q)
|
|
|
|
|
|
|
|
running_update_threads.append(new_worker)
|
|
|
|
|
|
|
|
new_worker.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Every minute check for new UUIDs to follow up on
|
|
|
|
while True:
|
|
|
|
while True:
|
|
|
|
launch_checks()
|
|
|
|
minutes = datastore.data['settings']['requests']['minutes_between_check']
|
|
|
|
|
|
|
|
for uuid, watch in datastore.data['watching'].items():
|
|
|
|
|
|
|
|
if watch['last_checked'] <= time.time() - (minutes * 60):
|
|
|
|
|
|
|
|
update_q.put(uuid)
|
|
|
|
|
|
|
|
|
|
|
|
time.sleep(60)
|
|
|
|
time.sleep(60)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Thread runner, this helps with thread/write issues when there are many operations that want to update the JSON
|
|
|
|
# Thread runner, this helps with thread/write issues when there are many operations that want to update the JSON
|
|
|
|
# by just running periodically in one thread.
|
|
|
|
# by just running periodically in one thread, according to python, dict updates are threadsafe.
|
|
|
|
def save_datastore():
|
|
|
|
def save_datastore():
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
if datastore.needs_write:
|
|
|
|
while True:
|
|
|
|
datastore.sync_to_json()
|
|
|
|
if datastore.needs_write:
|
|
|
|
time.sleep(5)
|
|
|
|
datastore.sync_to_json()
|
|
|
|
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
def main(argv):
|
|
|
|
ssl_mode = False
|
|
|
|
ssl_mode = False
|
|
|
@ -378,6 +393,7 @@ def main(argv):
|
|
|
|
|
|
|
|
|
|
|
|
# @todo handle ctrl break
|
|
|
|
# @todo handle ctrl break
|
|
|
|
ticker_thread = threading.Thread(target=ticker_thread_check_time_launch_checks).start()
|
|
|
|
ticker_thread = threading.Thread(target=ticker_thread_check_time_launch_checks).start()
|
|
|
|
|
|
|
|
|
|
|
|
save_data_thread = threading.Thread(target=save_datastore).start()
|
|
|
|
save_data_thread = threading.Thread(target=save_datastore).start()
|
|
|
|
|
|
|
|
|
|
|
|
# @todo finalise SSL config, but this should get you in the right direction if you need it.
|
|
|
|
# @todo finalise SSL config, but this should get you in the right direction if you need it.
|
|
|
|