From 564c27a00a3c8577329b0b88320eb4a77cafc6cb Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Sat, 15 Sep 2018 18:15:21 +0200 Subject: [PATCH] microservicified --- README.md | 2 +- app/core/cron.py | 22 ++++++++++++---------- app/core/mailer.py | 4 ++-- app/interface/form.py | 2 +- app/run.py | 4 ++++ 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 72017aa..e04cad4 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Stacosys can be hosted on the same server or on a different server than the blog Python 3.7 -pip libs: flask peewee pyrss2gen markdown clize flask-apscheduler profig +pip libs: flask peewee pyrss2gen markdown clize flask-apscheduler profig requests ### Ways of improvement diff --git a/app/core/cron.py b/app/core/cron.py index fcd8137..b9dbdb6 100644 --- a/app/core/cron.py +++ b/app/core/cron.py @@ -26,14 +26,11 @@ def cron(func): def fetch_mail_answers(): for msg in mailer.fetch(): - m = re.search(r"\[(\d+)\:(\w+)\]", msg["subject"]) - if m: + if re.search(r".*STACOSYS.*\[(\d+)\:(\w+)\]", msg["subject"], re.DOTALL): full_msg = mailer.get(msg["id"]) - if full_msg: - reply_comment_email(full_msg) + if full_msg and reply_comment_email(full_msg['email']): mailer.delete(msg["id"]) - @cron def submit_new_comment(): @@ -42,10 +39,10 @@ def submit_new_comment(): comment_list = ( "author: %s" % comment.author_name, "site: %s" % comment.author_site, - "date: %s" % comment.create, + "date: %s" % comment.created, "url: %s" % comment.url, "", - "%s" % comment.message, + "%s" % comment.content, "", ) comment_text = "\n".join(comment_list) @@ -54,11 +51,15 @@ def submit_new_comment(): ) # send email - site = Site.select().where(Site.id == Comment.site).get() + site = Site.get(Site.id == comment.site) subject = "STACOSYS %s: [%d:%s]" % (site.name, comment.id, site.token) mailer.send(site.admin_email, subject, email_body) logger.debug("new comment processed ") + # update comment + comment.notified = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + comment.save() + def reply_comment_email(data): @@ -82,7 +83,7 @@ def reply_comment_email(data): comment = Comment.select().where(Comment.id == comment_id).get() except: logger.warn("unknown comment %d" % comment_id) - return + return True if comment.published: logger.warn("ignore already published email. token %d" % comment_id) @@ -103,7 +104,7 @@ def reply_comment_email(data): if message[:2].upper() == "SP": # SPAM if comment.ip: logger.info( - "SPAM comment from %s: %d" % (client_ips[comment_id], comment_id) + "SPAM comment from %s: %d" % (comment.ip, comment_id) ) else: logger.info("cannot identify SPAM source: %d" % comment_id) @@ -126,3 +127,4 @@ def reply_comment_email(data): email_body = get_template("approve_comment").render(original=message) mailer.send(from_email, "Re: " + subject, email_body) + return True \ No newline at end of file diff --git a/app/core/mailer.py b/app/core/mailer.py index 8e54651..3697c49 100644 --- a/app/core/mailer.py +++ b/app/core/mailer.py @@ -22,7 +22,7 @@ def fetch(): def get(id): payload = None - r = requests.get(config.get(config.MAILER_URL) + "/mbox/" + id) + r = requests.get(config.get(config.MAILER_URL) + "/mbox/" + str(id)) if r.status_code == 200: payload = r.json() return payload @@ -41,4 +41,4 @@ def send(to_email, subject, message): def delete(id): - requests.delete(config.get(config.MAILER_URL) + "/mbox/" + id) + requests.delete(config.get(config.MAILER_URL) + "/mbox/" + str(id)) diff --git a/app/interface/form.py b/app/interface/form.py index 00cc149..42e04ad 100644 --- a/app/interface/form.py +++ b/app/interface/form.py @@ -42,7 +42,7 @@ def new_form_comment(): url = data.get("url", "") author_name = data.get("author", "").strip() author_gravatar = data.get("email", "").strip() - author_site = data.get("site", "").to_lower().strip() + author_site = data.get("site", "").lower().strip() if author_site and author_site[:4] != "http": author_site = "http://" + author_site message = data.get("message", "") diff --git a/app/run.py b/app/run.py index c57aa3a..c171eb6 100644 --- a/app/run.py +++ b/app/run.py @@ -26,6 +26,10 @@ class JobConfig(object): JOBS = [] + SCHEDULER_EXECUTORS = { + 'default': {'type': 'threadpool', 'max_workers': 20} + } + def __init__(self, mail_polling_seconds, new_comment_polling_seconds): self.JOBS = [ {