Commit 43d944f1 authored by Romain Courteaud's avatar Romain Courteaud

Allow to quickly check one site status with memory DB

parent b2fbb79f
......@@ -28,6 +28,10 @@ class WebBot:
def __init__(self, **kw):
self.config = createConfiguration(**kw)
def closeDB(self):
if hasattr(self, "_db"):
self._db.close()
def initDB(self):
self._db = LogDB(self.config["SQLITE"])
self._db.createTables()
......@@ -98,9 +102,6 @@ class WebBot:
# XXX Check HTTP Cache
def status(self):
# XXX
self.initDB()
# Report the bot status
print("# STATUS")
status = reportStatus(self._db).get()
......@@ -206,11 +207,8 @@ class WebBot:
def stop(self):
self._running = False
logStatus(self._db, "stop")
if hasattr(self, "_db"):
self._db.close()
def run(self):
self.initDB()
def crawl(self):
status_id = logStatus(self._db, "start")
logConfiguration(self._db, status_id, self.config)
......@@ -228,9 +226,29 @@ class WebBot:
except:
# XXX Put traceback in the log?
logStatus(self._db, "error")
self.stop()
raise
def run(self, mode):
if mode not in ["crawl", "status"]:
raise NotImplementedError("Unexpected mode: %s" % mode)
if self.config["SQLITE"] == ":memory:":
# Crawl/report are mandatory when using memory
mode = "all"
self.initDB()
try:
if mode in ["crawl", "all"]:
self.crawl()
if mode in ["status", "all"]:
self.status()
except:
self.closeDB()
raise
else:
self.closeDB()
def create_bot(**kw):
return WebBot(**kw)
......@@ -2,11 +2,16 @@ import click
import sys
from urlchecker_bot import create_bot
@click.command(short_help="Runs url checker bot.")
@click.option('--run', '-r', help="The bot operation to run.",
@click.option(
"--run",
"-r",
help="The bot operation mode to run.",
show_default=True,
default='status',
type=click.Choice(['crawl', 'status']))
default="status",
type=click.Choice(["crawl", "status"]),
)
@click.option(
"--sqlite", "-s", help="The path of the sqlite DB. (default: :memory:)"
)
......@@ -32,12 +37,7 @@ def runUrlChecker(run, sqlite, dns, url, domain, configuration):
if dns:
mapping["DNS"] = dns
bot = create_bot(cfgfile=configuration, mapping=mapping)
if run == 'status':
return bot.status()
elif run == 'crawl':
return bot.run()
else:
raise NotImplementedError('Unexpected run: %s' % run)
return bot.run(run)
if __name__ == "__main__":
......
......@@ -29,6 +29,10 @@ def createConfiguration(
get_default_resolver().nameservers
)
if config[CONFIG_SECTION]["SQLITE"] == ":memory:":
# Do not loop when using temporary DB
config[CONFIG_SECTION]["INTERVAL"] = "-1"
return config[CONFIG_SECTION]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment