Fetch the repository succeeded.
This action will force synchronization from 请叫我code哥/ruyangmao, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
# -*- encoding=utf8 -*-
__author__ = "code哥"
import sys
import os
import time
from conn_pool import get_redis, REDIS_APP_RUN_LOG
rs = get_redis()
class Tail(object):
def __init__(self, tailed_file):
self.check_file_validity(tailed_file)
self.tailed_file = tailed_file
self.callback = sys.stdout.write
self.register_callback(print_log)
self.follow(s=1)
def follow(self, s=1):
with open(self.tailed_file) as file_:
# Go to the end of file
file_.seek(0, 2)
while True:
curr_position = file_.tell()
line = file_.readline()
if not line:
file_.seek(curr_position)
time.sleep(s)
else:
self.callback(line)
def register_callback(self, func):
self.callback = func
def check_file_validity(self, file_):
if not os.access(file_, os.F_OK):
raise TailError("File '%s' does not exist" % (file_))
if not os.access(file_, os.R_OK):
raise TailError("File '%s' not readable" % (file_))
if os.path.isdir(file_):
raise TailError("File '%s' is a directory" % (file_))
class TailError(Exception):
def __init__(self, msg):
self.message = msg
def __str__(self):
return self.message
def print_log(msg):
rs.rpush(REDIS_APP_RUN_LOG, msg)
# 保存最新500条
rs.ltrim(REDIS_APP_RUN_LOG, 0, 500 - 1)
def main():
Tail('{path}/run.log'.format(path=os.path.dirname(os.path.abspath(__file__))))
if __name__ == '__main__':
main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。