From cedac41b106aa4f9ad24cbf67bfd7b20a668ac09 Mon Sep 17 00:00:00 2001 From: Yax Date: Sat, 20 Jan 2018 10:03:41 +0100 Subject: [PATCH] on microservice way --- app/conf/schema.py | 4 ---- app/core/processor.py | 41 +++++++++++++++++++++++++--------------- app/interface/zclient.py | 1 + app/stacosys.py | 8 ++++---- config.json | 1 - 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/app/conf/schema.py b/app/conf/schema.py index f76d18a..bcd7d15 100644 --- a/app/conf/schema.py +++ b/app/conf/schema.py @@ -121,9 +121,6 @@ json_schema = """ "type": "object", "additionalProperties": false, "properties": { - "active": { - "type": "boolean" - }, "host": { "type": "string" }, @@ -135,7 +132,6 @@ json_schema = """ } }, "required": [ - "active", "host", "pub_port", "sub_port" diff --git a/app/core/processor.py b/app/core/processor.py index f75b986..0321ee7 100644 --- a/app/core/processor.py +++ b/app/core/processor.py @@ -25,10 +25,10 @@ queue = Queue() proc = None env = None -if config.zmq['active']: - context = zmq.Context() - zpub = context.socket(zmq.PUB) - zpub.connect('tcp://127.0.0.1:{}'.format(config.zmq['sub_port'])) + +context = zmq.Context() +zpub = context.socket(zmq.PUB) +zpub.connect('tcp://127.0.0.1:{}'.format(config.zmq['sub_port'])) class Processor(Thread): @@ -78,7 +78,7 @@ def new_comment(data): if config.security['private']: author_gravatar = author_email author_email = '' - else: + else: author_gravatar = md5(author_email.lower()) # create a new comment row @@ -151,6 +151,9 @@ def reply_comment_email(data): logger.warn('ignore empty email') return + # accept email: request to delete + send_deletion_order(data) + # safe logic: no answer or unknown answer is a go for publishing if message[:2].upper() == 'NO': # report event @@ -344,7 +347,8 @@ def report(token): 'name': row.name, 'email': row.email}) email_body = get_template('report').render(secret=config.security['secret'], - root_url=config.http['root_url'], + root_url=config.http[ + 'root_url'], standbys=standbys, published=published, rejected=rejected, @@ -373,7 +377,8 @@ def rss(token, onstart=False): -Comment.published).limit(10): item_link = "%s://%s%s" % (config.rss['proto'], site.url, row.url) items.append(PyRSS2Gen.RSSItem( - title='%s - %s://%s%s' % (config.rss['proto'], row.author_name, site.url, row.url), + title='%s - %s://%s%s' % (config.rss['proto'], + row.author_name, site.url, row.url), link=item_link, description=md.convert(row.content), guid=PyRSS2Gen.Guid('%s/%d' % (item_link, row.id)), @@ -391,18 +396,24 @@ def rss(token, onstart=False): def mail(to_email, subject, message): - headers = {'Content-Type': 'application/json; charset=utf-8'} - msg = { + zmsg = { + 'topic': 'email:sendmail', 'to': to_email, 'subject': subject, 'content': message } - # do something smart here - # r = requests.post(config.MAIL_URL, data=json.dumps(msg), headers=headers) - if r.status_code in (200, 201): - logger.debug('Email for %s posted' % to_email) - else: - logger.warn('Cannot post email for %s' % to_email) + + # TODO test broker failure and find alternative + zpub.send_string(json.dumps(zmsg, indent=False, sort_keys=False)) + logger.debug('Email for %s posted' % to_email) + + #logger.warn('Cannot post email for %s' % to_email) + + +def send_deletion_order(zmsg): + zmsg['topic'] = 'email:delete' + zpub.send_string(json.dumps(zmsg, indent=False, sort_keys=False)) + logger.debug('Email accepted. Deletion request sent for %s' % zmsg) def get_template(name): diff --git a/app/interface/zclient.py b/app/interface/zclient.py index a467428..63c18d7 100644 --- a/app/interface/zclient.py +++ b/app/interface/zclient.py @@ -39,5 +39,6 @@ class Consumer(Thread): def start(): + logger.info('start zclient') c = Consumer() c.start() diff --git a/app/stacosys.py b/app/stacosys.py index 8d26708..36d3553 100644 --- a/app/stacosys.py +++ b/app/stacosys.py @@ -7,6 +7,7 @@ from clize import clize, run from jsonschema import validate from conf import config, schema + def load_json(filename): jsondoc = None with open(filename, 'rt') as json_file: @@ -19,10 +20,9 @@ def stacosys_server(config_pathname): # load and validate startup config conf = load_json(config_pathname) - json_schema = json.loads(schema.json_schema) + json_schema = json.loads(schema.json_schema) v = validate(conf, json_schema) - print('validation: {}'.format(v)) - + # set configuration config.general = conf['general'] config.http = conf['http'] @@ -34,4 +34,4 @@ def stacosys_server(config_pathname): from core import app if __name__ == '__main__': - run(stacosys_server) \ No newline at end of file + run(stacosys_server) diff --git a/config.json b/config.json index 7b6be9e..0a2ec66 100644 --- a/config.json +++ b/config.json @@ -19,7 +19,6 @@ "file": "comments.xml" }, "zmq": { - "active": true, "host": "127.0.0.1", "pub_port": 7701, "sub_port": 7702