-
Notifications
You must be signed in to change notification settings - Fork 14
/
runScrape.py
190 lines (140 loc) · 4.43 KB
/
runScrape.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
#!flask/bin/python
if __name__ == "__main__":
import logSetup
import logging
logSetup.initLogging()
# logSetup.initLogging(logging.WARNING)
# Shut up fucking annoying psycopg2 vomit every exec.
import warnings
from sqlalchemy import exc as sa_exc
warnings.filterwarnings("ignore", category=UserWarning, module='psycopg2')
warnings.simplefilter("ignore", category=sa_exc.SAWarning)
# This HAS to be included before the app, to prevent circular dependencies.
# import WebMirror.runtime_engines
import settings
import multiprocessing
import time
import json
import common.RunManager
import WebMirror.rules
import WebMirror.Runner
import WebMirror.UrlUpserter
import RawArchiver.RawRunner
import RawArchiver.RawUrlUpserter
import common.stuck
import common.process
import psutil
import Misc.ls_open_file_handles
import common.redis
import common.management.WebMirrorManage
from settings import NO_PROCESSES
from settings import RAW_NO_PROCESSES
from settings import MAX_DB_SESSIONS
import common.LogBase as LogBase
import common.StatsdMixin as StatsdMixin
class MemoryTracker(LogBase.LoggerMixin, StatsdMixin.InfluxDBMixin):
loggerPath = "Main.MemStats"
influxdb_type = "memory_stats"
influxdb_measurement_name = "server_ram_free"
def save_mem_stats(self):
v = psutil.virtual_memory()
params = {
'mem_total' : v.total,
'mem_available' : v.available,
'mem_percent' : v.percent,
'mem_used' : v.used,
'mem_free' : v.free,
'mem_active' : v.active,
'mem_inactive' : v.inactive,
'mem_buffers' : v.buffers,
'mem_cached' : v.cached,
'mem_shared' : v.shared,
'mem_slab' : v.slab,
}
self.log.info("Memory stats: %s", params)
points = [
{
'measurement' : self.influxdb_measurement_name,
"tags": {
'type' : self.influxdb_type,
},
'time' : int(time.time() * 1e9),
'fields' : params
}
]
self.influx_client.write_points(points)
def go(args):
largv = [tmp.lower() for tmp in args]
common.redis.config_redis()
rules = WebMirror.rules.load_rules()
lowrate = "lowrate" in largv
runner = common.RunManager.Crawler(
main_thread_count = NO_PROCESSES,
raw_thread_count = RAW_NO_PROCESSES,
lowrate = lowrate,
)
if "raw" in largv:
common.process.name_process("raw fetcher management thread")
print("RAW Scrape!")
RawArchiver.RawUrlUpserter.check_init_func()
if not "noreset" in largv:
print("Resetting any in-progress downloads.")
RawArchiver.RawUrlUpserter.resetRawInProgress()
else:
print("Not resetting in-progress downloads.")
RawArchiver.RawUrlUpserter.initializeRawStartUrls()
runner.run_raw()
else:
common.process.name_process("fetcher management thread")
if "noreset" not in largv and not lowrate:
print("Resetting any in-progress downloads.")
WebMirror.UrlUpserter.resetInProgress()
WebMirror.UrlUpserter.resetRedisQueues()
else:
print("Not resetting in-progress downloads.")
#if not "noreset" in largv:
# print("Dropping fetch priority levels.")
# common.management.WebMirrorManage.exposed_drop_priorities()
#else:
# print("Not resetting fetch priority levels.")
WebMirror.UrlUpserter.initializeStartUrls(rules)
runner.run()
print("Main runner returned!")
# print("Thread halted. App exiting.")
def dump_active_items():
print("Dumping active URLs")
active = {
'fetching' : [tmp.decode("utf-8") for tmp in common.redis.get_fetching_urls()],
'processing' : [tmp.decode("utf-8") for tmp in common.redis.get_processing_urls()],
}
with open("active_jobs_at_start_%s.json" % time.time(), "w") as fp:
json.dump(active, fp, sort_keys=True, indent=4)
common.redis.clear_fetching_urls()
common.redis.clear_processing_urls()
def run_in_subprocess():
if 'raw' not in sys.argv:
dump_active_items()
# mon = MemoryTracker()
proc = multiprocessing.Process(target=go, args=(sys.argv, ))
proc.start()
while proc.is_alive():
time.sleep(4)
# mon.save_mem_stats()
# print("Base Subprocessor Runner: %s, %s" % (proc.is_alive(), proc.pid))
print("Main runner has gone away. Committing Suicide")
# If the subprocess has gone away, die hard.
import ctypes
ctypes.string_at(1)
import os
os.kill(0,4)
if __name__ == "__main__":
import sys
largv = [tmp.lower() for tmp in sys.argv]
if "scheduler" in sys.argv:
print("Please use runScheduler.py instead!")
sys.exit(1)
else:
started = False
if not started:
started = True
run_in_subprocess()