-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add log tailing to logfetch tool #383
Changes from 2 commits
0e60376
dc24f92
c17f0a9
5086f44
4092300
4a60cae
8cff7e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import os | ||
import sys | ||
import logfetch_base | ||
import requests | ||
import time | ||
import threading | ||
from singularity_request import get_json_response | ||
|
||
|
||
TAIL_LOG_FORMAT = '{0}/sandbox/{1}/read' | ||
READ_INTERVAL = 5 | ||
|
||
def tail_logs(args): | ||
if args.requestId: | ||
sys.stderr.write('Fetching tasks\n') | ||
tasks = [str(t) for t in logfetch_base.tasks_for_request(args)] | ||
else: | ||
tasks = [args.taskId] | ||
sys.stderr.write('Tailing logs for tasks:\n') | ||
for t in tasks: | ||
sys.stderr.write('{0}\n'.format(t)) | ||
sys.stderr.write('ctrl+c to exit\n') | ||
try: | ||
threads = [] | ||
for task in tasks: | ||
thread = LogStreamer(args, task) | ||
threads += [thread] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a huge nitpick i know, but just use |
||
thread.start() | ||
while True: # main thread needs something to do so it doesn't kill the others | ||
time.sleep(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better practice is to call |
||
except KeyboardInterrupt: | ||
sys.stdout.write('Stopping tail') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
sys.exit(0) | ||
|
||
class LogStreamer(threading.Thread): | ||
def __init__(self, args, task): | ||
threading.Thread.__init__(self) | ||
self.daemon = True | ||
self.Args = args | ||
self.Task = task | ||
|
||
def run(self): | ||
self.stream_log_for_task(self.Args, self.Task) | ||
|
||
def stream_log_for_task(self, args, task): | ||
uri = TAIL_LOG_FORMAT.format(logfetch_base.base_uri(args), task) | ||
path = '{0}/{1}'.format(task, args.tail) | ||
keep_trying = True | ||
try: | ||
offset = self.get_initial_offset(uri, path) | ||
except ValueError: | ||
sys.stderr.write('Could not tail logs for task {0}, check that the task is still active and that the slave it runs on has not been decommissioned\n'.format(task)) | ||
keep_trying = False | ||
while keep_trying: | ||
try: | ||
offset = self.fetch_new_log_data(uri, path, offset, args.grep) | ||
time.sleep(5) | ||
except ValueError: | ||
sys.stderr.write('Could not tail logs for task {0}, check that the task is still active and that the slave it runs on has not been decommissioned\n'.format(task)) | ||
keep_trying = False | ||
|
||
def get_initial_offset(self, uri, path): | ||
params = {"path" : path} | ||
return requests.get(uri, params=params).json()['offset'] | ||
|
||
def fetch_new_log_data(self, uri, path, offset, grep): | ||
params = { | ||
"path" : path, | ||
"offset" : offset | ||
} | ||
if grep: | ||
params['grep'] = grep | ||
response = requests.get(uri, params=params).json() | ||
sys.stdout.write(response['data']) | ||
return offset + len(response['data'].encode('utf-8')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we mimic the
tail
command and use-t
as well (make people think less)?