Commit eb31b078 by Owo Sugiana

Bug fixed thread di Windows

1 parent ffcd6afc
...@@ -44,23 +44,24 @@ class Pantau(FileSystemEventHandler): ...@@ -44,23 +44,24 @@ class Pantau(FileSystemEventHandler):
set_log_file(event.src_path) set_log_file(event.src_path)
def save_log(line): def read_log(log_file, db_url):
line = line.rstrip() def save_log(line):
match = REGEX.search(line) line = line.rstrip()
if not match: match = REGEX.search(line)
return if not match:
print([line]) return
db_session = my_registry['db_session'] print([line])
line_id = sha256(line.encode('utf-8')).hexdigest() line_id = sha256(line.encode('utf-8')).hexdigest()
row = LogFile(line_id=line_id, line=line) row = LogFile(line_id=line_id, line=line)
try: try:
with transaction.manager: with transaction.manager:
db_session.add(row) db_session.add(row)
except IntegrityError: except IntegrityError:
return return
def read_log(log_file): engine = create_engine(db_url)
factory = sessionmaker(bind=engine)
db_session = factory()
with BacaFile(log_file) as f: with BacaFile(log_file) as f:
while True: while True:
line = f.readline() line = f.readline()
...@@ -78,10 +79,7 @@ def main(argv=sys.argv[1:]): ...@@ -78,10 +79,7 @@ def main(argv=sys.argv[1:]):
option = pars.parse_args(argv) option = pars.parse_args(argv)
conf = ConfigParser() conf = ConfigParser()
conf.read(option.conf) conf.read(option.conf)
engine = create_engine(conf.get('main', 'db_url')) db_url = conf.get('main', 'db_url')
factory = sessionmaker(bind=engine)
my_registry['db_session'] = db_session = factory()
register(db_session)
log_dir = conf.get('main', 'log_dir') log_dir = conf.get('main', 'log_dir')
handler = Pantau() handler = Pantau()
observer = Observer() observer = Observer()
...@@ -98,7 +96,7 @@ def main(argv=sys.argv[1:]): ...@@ -98,7 +96,7 @@ def main(argv=sys.argv[1:]):
log_file = my_registry['log_file'] log_file = my_registry['log_file']
del my_registry['log_file'] del my_registry['log_file']
my_registry['last_log_file'] = log_file my_registry['last_log_file'] = log_file
thread = Process(target=read_log, args=(log_file,)) thread = Process(target=read_log, args=(log_file, db_url))
my_registry['read_log'] = thread my_registry['read_log'] = thread
print('Start read file ...') print('Start read file ...')
thread.start() thread.start()
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!