2016-02-04 14:22:51 +00:00
|
|
|
#!/usr/bin/env python
|
2014-08-11 09:04:09 +00:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
"""
|
|
|
|
The ZMQ_Sub_Indexer Module
|
|
|
|
============================
|
|
|
|
|
|
|
|
The ZMQ_Sub_Indexer modules is fetching the list of files to be processed
|
|
|
|
and index each file with a full-text indexer (Whoosh until now).
|
|
|
|
|
|
|
|
"""
|
2014-08-14 15:55:18 +00:00
|
|
|
import time
|
|
|
|
from packages import Paste
|
2014-08-11 09:04:09 +00:00
|
|
|
from pubsublogger import publisher
|
|
|
|
|
|
|
|
from whoosh.index import create_in, exists_in, open_dir
|
2014-08-14 15:55:18 +00:00
|
|
|
from whoosh.fields import Schema, TEXT, ID
|
2014-08-11 09:04:09 +00:00
|
|
|
import os
|
2017-03-14 09:37:31 +00:00
|
|
|
from os.path import join, getsize
|
2014-08-11 09:04:09 +00:00
|
|
|
|
2014-08-29 17:37:56 +00:00
|
|
|
from Helper import Process
|
2014-08-11 09:04:09 +00:00
|
|
|
|
2017-03-14 09:37:31 +00:00
|
|
|
# Config variable
|
|
|
|
INDEX_SIZE_THRESHOLD = 500 #Mb
|
|
|
|
TIME_WAIT = 1.0 #sec
|
|
|
|
|
|
|
|
# return in bytes
|
2017-03-15 10:51:35 +00:00
|
|
|
def check_index_size(indexname):
|
2017-03-14 09:37:31 +00:00
|
|
|
global baseindexpath
|
2017-03-15 10:51:35 +00:00
|
|
|
the_index_name = indexname if indexname != "0" else "old_index"
|
|
|
|
the_index_name = join(baseindexpath, the_index_name)
|
2017-03-14 09:37:31 +00:00
|
|
|
cur_sum = 0
|
|
|
|
for root, dirs, files in os.walk(the_index_name):
|
|
|
|
cur_sum += sum(getsize(join(root, name)) for name in files)
|
|
|
|
return cur_sum
|
|
|
|
|
2017-03-15 10:51:35 +00:00
|
|
|
def move_index_into_old_index_folder(baseindexpath):
|
|
|
|
command_move = "mv {} {}"
|
|
|
|
command_dir = "mkdir {}"
|
|
|
|
os.system(command_dir.format(join(baseindexpath, "old_index")))
|
|
|
|
for files in os.listdir(baseindexpath):
|
|
|
|
if not files == "old_index":
|
|
|
|
os.system(command_move.format(join(baseindexpath, files), join(join(baseindexpath, "old_index"), files)))
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
|
2014-08-19 17:07:07 +00:00
|
|
|
if __name__ == "__main__":
|
2014-08-22 15:35:40 +00:00
|
|
|
publisher.port = 6380
|
2014-08-19 17:07:07 +00:00
|
|
|
publisher.channel = "Script"
|
|
|
|
|
2014-08-29 17:37:56 +00:00
|
|
|
config_section = 'Indexer'
|
2014-08-11 09:04:09 +00:00
|
|
|
|
2014-08-29 17:37:56 +00:00
|
|
|
p = Process(config_section)
|
2014-08-11 09:04:09 +00:00
|
|
|
|
|
|
|
# Indexer configuration - index dir and schema setup
|
2017-03-15 10:51:35 +00:00
|
|
|
baseindexpath = join(os.environ['AIL_HOME'],
|
2014-08-29 17:37:56 +00:00
|
|
|
p.config.get("Indexer", "path"))
|
2017-03-15 10:51:35 +00:00
|
|
|
indexRegister_path = join(os.environ['AIL_HOME'],
|
2017-03-14 09:37:31 +00:00
|
|
|
p.config.get("Indexer", "register"))
|
2014-08-29 17:37:56 +00:00
|
|
|
indexertype = p.config.get("Indexer", "type")
|
2014-08-11 09:04:09 +00:00
|
|
|
if indexertype == "whoosh":
|
2014-08-19 17:07:07 +00:00
|
|
|
schema = Schema(title=TEXT(stored=True), path=ID(stored=True,
|
|
|
|
unique=True),
|
|
|
|
content=TEXT)
|
2017-03-14 09:37:31 +00:00
|
|
|
if not os.path.exists(baseindexpath):
|
|
|
|
os.mkdir(baseindexpath)
|
|
|
|
|
|
|
|
# create the index register if not present
|
2017-03-15 10:51:35 +00:00
|
|
|
time_now = int(time.time())
|
|
|
|
if not os.path.isfile(indexRegister_path): #index are not organised
|
|
|
|
#move all files to old_index folder
|
|
|
|
move_index_into_old_index_folder(baseindexpath)
|
|
|
|
#create all_index.txt
|
2017-03-14 09:37:31 +00:00
|
|
|
with open(indexRegister_path, 'w') as f:
|
2017-03-15 10:51:35 +00:00
|
|
|
f.write(str(time_now))
|
|
|
|
#create dir
|
|
|
|
os.system("mkdir "+join(baseindexpath, str(time_now)))
|
2017-03-14 09:37:31 +00:00
|
|
|
|
|
|
|
with open(indexRegister_path, "r") as f:
|
|
|
|
allIndex = f.read()
|
2017-03-15 10:51:35 +00:00
|
|
|
allIndex = allIndex.split(',') # format [time1,time2]
|
2017-03-14 09:37:31 +00:00
|
|
|
allIndex.sort()
|
|
|
|
|
2017-03-15 10:51:35 +00:00
|
|
|
try:
|
|
|
|
indexname = allIndex[-1].strip('\n\r')
|
|
|
|
except IndexError as e:
|
|
|
|
indexname = time_now
|
|
|
|
|
|
|
|
indexpath = join(baseindexpath, str(indexname))
|
2017-03-14 09:37:31 +00:00
|
|
|
if not exists_in(indexpath):
|
|
|
|
ix = create_in(indexpath, schema)
|
|
|
|
else:
|
|
|
|
ix = open_dir(indexpath)
|
|
|
|
|
2017-03-15 10:51:35 +00:00
|
|
|
last_refresh = time_now
|
2014-08-11 09:04:09 +00:00
|
|
|
|
|
|
|
# LOGGING #
|
2014-08-29 17:37:56 +00:00
|
|
|
publisher.info("ZMQ Indexer is Running")
|
2014-08-11 09:04:09 +00:00
|
|
|
|
|
|
|
while True:
|
2014-08-14 15:55:18 +00:00
|
|
|
try:
|
2014-08-29 17:37:56 +00:00
|
|
|
message = p.get_from_set()
|
2014-08-11 09:04:09 +00:00
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
if message is not None:
|
2014-08-29 17:37:56 +00:00
|
|
|
PST = Paste.Paste(message)
|
2014-08-11 09:04:09 +00:00
|
|
|
else:
|
2014-08-29 17:37:56 +00:00
|
|
|
publisher.debug("Script Indexer is idling 1s")
|
2014-08-11 09:04:09 +00:00
|
|
|
time.sleep(1)
|
|
|
|
continue
|
2014-08-14 15:55:18 +00:00
|
|
|
docpath = message.split(" ", -1)[-1]
|
2014-08-11 09:04:09 +00:00
|
|
|
paste = PST.get_p_content()
|
2017-03-15 10:51:35 +00:00
|
|
|
print "Indexing - "+indexname+" :", docpath
|
2017-03-14 09:37:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
if time.time() - last_refresh > TIME_WAIT: #avoid calculating the index's size at each message
|
|
|
|
last_refresh = time.time()
|
2017-03-15 10:51:35 +00:00
|
|
|
if check_index_size(indexname) > INDEX_SIZE_THRESHOLD*(1000*1000):
|
|
|
|
timestamp = int(time.time())
|
|
|
|
indexpath = join(baseindexpath, str(timestamp))
|
|
|
|
ix = create_in(indexpath, schema)
|
|
|
|
indexname = str(timestamp)
|
2017-03-14 09:37:31 +00:00
|
|
|
## Correctly handle the file
|
|
|
|
with open(indexRegister_path, "a") as f:
|
2017-03-15 10:51:35 +00:00
|
|
|
f.write(","+str(timestamp))
|
2017-03-14 09:37:31 +00:00
|
|
|
|
|
|
|
|
2014-08-11 09:04:09 +00:00
|
|
|
if indexertype == "whoosh":
|
|
|
|
indexwriter = ix.writer()
|
2014-08-14 15:55:18 +00:00
|
|
|
indexwriter.update_document(
|
|
|
|
title=unicode(docpath, errors='ignore'),
|
|
|
|
path=unicode(docpath, errors='ignore'),
|
|
|
|
content=unicode(paste, errors='ignore'))
|
2014-08-11 09:04:09 +00:00
|
|
|
indexwriter.commit()
|
|
|
|
except IOError:
|
2014-08-14 15:55:18 +00:00
|
|
|
print "CRC Checksum Failed on :", PST.p_path
|
2014-08-19 17:07:07 +00:00
|
|
|
publisher.error('Duplicate;{};{};{};CRC Checksum Failed'.format(
|
|
|
|
PST.p_source, PST.p_date, PST.p_name))
|