forked from Charcoal-SE/SmokeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.py
194 lines (160 loc) · 7.19 KB
/
ws.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# coding=utf-8
# requires https://pypi.python.org/pypi/websocket-client/
from excepthook import uncaught_exception, install_thread_excepthook
import sys
sys.excepthook = uncaught_exception
install_thread_excepthook()
# !! Important! Be careful when adding code/imports before this point.
# Our except hook is installed here, so any errors before this point
# won't be caught if they're not in a try-except block.
# Hence, please avoid adding code before this comment; if it's necessary,
# test it thoroughly.
import os
# noinspection PyPackageRequirements
import websocket
import getpass
from threading import Thread
import traceback
from bodyfetcher import BodyFetcher
import chatcommunicate
from datetime import datetime
from utcdate import UtcDate
from spamhandling import check_if_spam_json
from globalvars import GlobalVars
from datahandling import load_files, filter_auto_ignored_posts
from metasmoke import Metasmoke
from deletionwatcher import DeletionWatcher
import json
import time
import requests
# noinspection PyPackageRequirements
from tld.utils import update_tld_names, TldIOError
from helpers import log
from tasks import Tasks
import chatcommands
try:
update_tld_names()
except TldIOError as ioerr:
with open('errorLogs.txt', 'a') as errlogs:
if "permission denied:" in str(ioerr).lower():
if "/usr/local/lib/python2.7/dist-packages/" in str(ioerr):
errlogs.write("WARNING: Cannot update TLD names, due to `tld` being system-wide installed and not "
"user-level installed. Skipping TLD names update. \n")
if "/home/" in str(ioerr) and ".local/lib/python2.7/site-packages/tld/" in str(ioerr):
errlogs.write("WARNING: Cannot read/write to user-space `tld` installation, check permissions on the "
"path. Skipping TLD names update. \n")
errlogs.close()
pass
elif "certificate verify failed" in str(ioerr).lower():
# Ran into this error in testing on Windows, best to throw a warn if we get this...
errlogs.write("WARNING: Cannot verify SSL connection for TLD names update; skipping TLD names update.")
errlogs.close()
pass
else:
raise ioerr
if "ChatExchangeU" in os.environ:
username = os.environ["ChatExchangeU"]
else:
username = input("Username: ")
if "ChatExchangeP" in os.environ:
password = os.environ["ChatExchangeP"]
else:
password = getpass.getpass("Password: ")
# We need an instance of bodyfetcher before load_files() is called
GlobalVars.bodyfetcher = BodyFetcher()
load_files()
filter_auto_ignored_posts()
GlobalVars.s = "[ " + GlobalVars.chatmessage_prefix + " ] " \
"SmokeDetector started at [rev " +\
GlobalVars.commit_with_author +\
"](" + GlobalVars.bot_repository + "/commit/" +\
GlobalVars.commit['id'] +\
") (running on " +\
GlobalVars.location +\
")"
GlobalVars.s_reverted = "[ " + GlobalVars.chatmessage_prefix + " ] " \
"SmokeDetector started in [reverted mode](" + \
"https://charcoal-se.org/smokey/SmokeDetector-Statuses#reverted-mode) " \
"at [rev " + \
GlobalVars.commit_with_author + \
"](" + GlobalVars.bot_repository + "/commit/" + \
GlobalVars.commit['id'] + \
") (running on " +\
GlobalVars.location +\
")"
GlobalVars.standby_message = "[ " + GlobalVars.chatmessage_prefix + " ] " \
"SmokeDetector started in [standby mode](" + \
"https://charcoal-se.org/smokey/SmokeDetector-Statuses#standby-mode) " + \
"at [rev " +\
GlobalVars.commit_with_author +\
"](" + GlobalVars.bot_repository + "/commit/" +\
GlobalVars.commit['id'] +\
") (running on " +\
GlobalVars.location +\
")"
GlobalVars.standby_mode = "standby" in sys.argv
chatcommunicate.init(username, password)
Tasks.periodic(Metasmoke.send_status_ping, interval=60)
Tasks.periodic(Metasmoke.check_last_pingtime, interval=30)
if GlobalVars.standby_mode:
chatcommunicate.tell_rooms_with("debug", GlobalVars.standby_message)
Metasmoke.send_status_ping()
while GlobalVars.standby_mode:
time.sleep(3)
chatcommunicate.join_command_rooms()
# noinspection PyProtectedMember
def check_socket_connections():
for client in chatcommunicate._clients.values():
if client.last_activity and (datetime.utcnow() - client.last_activity).total_seconds() >= 60:
os._exit(10)
# noinspection PyProtectedMember
def restart_automatically():
Metasmoke.send_statistics()
os._exit(1)
Tasks.periodic(check_socket_connections, interval=90)
Tasks.later(restart_automatically, after=21600)
log('info', GlobalVars.location)
log('info', GlobalVars.metasmoke_host)
DeletionWatcher.update_site_id_list()
ws = websocket.create_connection("wss://qa.sockets.stackexchange.com/")
ws.send("155-questions-active")
if "first_start" in sys.argv and GlobalVars.on_master:
chatcommunicate.tell_rooms_with("debug", GlobalVars.s)
elif "first_start" in sys.argv and not GlobalVars.on_master:
chatcommunicate.tell_rooms_with("debug", GlobalVars.s_reverted)
Tasks.periodic(Metasmoke.send_statistics, interval=600)
metasmoke_ws_t = Thread(name="metasmoke websocket", target=Metasmoke.init_websocket)
metasmoke_ws_t.start()
while True:
try:
a = ws.recv()
if a is not None and a != "":
action = json.loads(a)["action"]
if action == "hb":
ws.send("hb")
if action == "155-questions-active":
is_spam, reason, why = check_if_spam_json(a)
t = Thread(name="bodyfetcher post enqueing",
target=GlobalVars.bodyfetcher.add_to_queue,
args=(a, True if is_spam else None))
t.start()
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
now = datetime.utcnow()
delta = now - UtcDate.startup_utc_date
seconds = delta.total_seconds()
tr = traceback.format_exc()
exception_only = ''.join(traceback.format_exception_only(type(e), e))\
.strip()
n = os.linesep
logged_msg = str(now) + " UTC" + n + exception_only + n + tr + n + n
log('error', logged_msg)
with open("errorLogs.txt", "a") as f:
f.write(logged_msg)
if seconds < 180 and exc_type != websocket.WebSocketConnectionClosedException\
and exc_type != KeyboardInterrupt and exc_type != SystemExit and exc_type != requests.ConnectionError:
# noinspection PyProtectedMember
os._exit(4)
ws = websocket.create_connection("ws://qa.sockets.stackexchange.com/")
ws.send("155-questions-active")
chatcommunicate.tell_rooms_with("debug", "Recovered from `" + exception_only + "`")