2019-01-07 16:11:04 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import redis
|
|
|
|
import time
|
|
|
|
import gzip
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
redis_server = redis.StrictRedis(
|
|
|
|
host="localhost",
|
|
|
|
port=6379,
|
2019-01-08 10:09:57 +01:00
|
|
|
db=0)
|
2019-01-07 16:11:04 +01:00
|
|
|
|
|
|
|
type = 1
|
2019-01-08 10:09:57 +01:00
|
|
|
max_timestamp = 60*5
|
2019-01-07 16:11:04 +01:00
|
|
|
|
|
|
|
def gzip_file(filepath):
|
|
|
|
with open(filepath, 'rb') as f:
|
|
|
|
content = f.read()
|
|
|
|
with gzip.open(filepath+'.gz', 'wb') as f2:
|
|
|
|
f2.write(content)
|
|
|
|
os.remove(filepath)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
stream_name = 'stream:{}'.format(type)
|
|
|
|
|
|
|
|
#group_name = 'group_stream:{}'.format(type)
|
|
|
|
#try:
|
|
|
|
# redis_server.xgroup_create(stream_name, group_name)
|
|
|
|
#except:
|
|
|
|
# pass
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
#print(redis_server.xpending(stream_name, group_name))
|
|
|
|
|
|
|
|
#res = redis_server.xreadgroup(group_name, 'consumername', {stream_name: '>'}, count=1)
|
|
|
|
res = redis_server.xread({stream_name: '0'}, count=1, block=500)
|
|
|
|
if res:
|
|
|
|
id = res[0][1][0][0]
|
|
|
|
data = res[0][1][0][1]
|
|
|
|
if id and data:
|
2019-01-08 10:09:57 +01:00
|
|
|
#print(id.decode())
|
2019-01-07 16:11:04 +01:00
|
|
|
#print(data)
|
|
|
|
|
|
|
|
date = datetime.datetime.now().strftime("%Y/%m/%d")
|
2019-01-08 10:09:57 +01:00
|
|
|
dir_path = os.path.join('data', date, data[b'uuid'].decode())
|
2019-01-07 16:11:04 +01:00
|
|
|
filename = ''
|
2019-01-08 10:09:57 +01:00
|
|
|
data_timestamp = data[b'timestamp'].decode()
|
2019-01-07 16:11:04 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
it = os.scandir(dir_path)
|
|
|
|
for entry in it:
|
|
|
|
if not entry.name.endswith(".gz") and entry.is_file():
|
|
|
|
filename = entry.name
|
|
|
|
break
|
|
|
|
filepath = os.path.join(dir_path, filename)
|
2019-01-08 10:09:57 +01:00
|
|
|
|
2019-01-08 16:29:44 +01:00
|
|
|
#if os.path.getsize(filepath) > 500000000: #bytes
|
|
|
|
# gzip_file(filepath)
|
|
|
|
# filename = data_timestamp
|
2019-01-07 16:11:04 +01:00
|
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
os.makedirs(dir_path)
|
|
|
|
# # TODO: use contexte manager in python 3.6
|
|
|
|
it = []
|
|
|
|
# #
|
|
|
|
|
|
|
|
if not filename:
|
2019-01-08 10:09:57 +01:00
|
|
|
filename = data_timestamp
|
|
|
|
|
|
|
|
if int(data_timestamp) - int(filename) > max_timestamp:
|
|
|
|
gzip_file(filepath)
|
|
|
|
filename = data_timestamp
|
2019-01-07 16:11:04 +01:00
|
|
|
|
2019-01-08 10:09:57 +01:00
|
|
|
with open(os.path.join(dir_path, filename), 'ab') as f:
|
|
|
|
f.write(data[b'message'])
|
2019-01-07 16:11:04 +01:00
|
|
|
|
2019-01-08 10:09:57 +01:00
|
|
|
#redis_server.xack(stream_name, group_name, id)
|
2019-01-07 16:11:04 +01:00
|
|
|
redis_server.xdel(stream_name, id)
|
|
|
|
else:
|
|
|
|
time.sleep(10)
|