-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabbitmq_entry_translator.py
61 lines (48 loc) · 1.75 KB
/
rabbitmq_entry_translator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import random
import sys
import time
import requests
from api.decorator import separate_process
from api.rabbitmq import RabbitMqConsumer
service_port = int(sys.argv[1])
class SimpleEntryTranslatorClientFeeder(object):
def __init__(self):
self.consumer = RabbitMqConsumer('edit', callback_function=self.on_page_edit)
def run(self):
self.consumer.run()
def on_page_edit(self, **arguments):
cool_down = True
while cool_down:
resp = requests.get(f'http://localhost:{service_port}/jobs')
data = resp.json()
if data['jobs'] < 10:
print(f'There are {data["jobs"]} jobs currently in progress')
cool_down = False
else:
print(f'COOLING DOWN: sleeping for 15 seconds as there are {data["jobs"]} jobs currently in progress')
time.sleep(15)
site = arguments.get('site', 'en')
title = arguments.get('title', '')
print(f'>>> {site} :: {title} <<<')
# roll = random.randint(0, 100)
route = 'wiktionary_page_async'
# if roll < 50:
# route = 'wiktionary_page_async'
# else:
# route = 'wiktionary_page'
url = f'http://localhost:{service_port}/{route}/{site}'
print(url)
resp = requests.post(url, json={'title': title})
print(resp.status_code)
if resp.status_code != 200:
print('Error! ', resp.status_code)
if 400 <= resp.status_code < 600:
print('Error! ', resp.status_code, resp.json())
if __name__ == '__main__':
@separate_process
def runner():
bot = SimpleEntryTranslatorClientFeeder()
bot.run()
for i in range(2):
runner()
time.sleep(1.5)