Created
June 18, 2015 16:12
-
-
Save kevmoo/dd35441e37c0407dc50a to your computer and use it in GitHub Desktop.
When using App Engine, docker containers are constantly restarting. Use this to keep a finger on the logs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env dart | |
import 'dart:async'; | |
import 'dart:convert'; | |
import 'dart:io'; | |
const _splitter = const LineSplitter(); | |
const _waitDuration = const Duration(milliseconds: 250); | |
const _green = '\u001b[32m'; | |
const _red = '\u001b[31m'; | |
const _yellow = '\u001b[33m'; | |
const _noColor = '\u001b[0m'; | |
main() async { | |
while (true) { | |
var result = Process.runSync('docker', ['ps', '-q']); | |
var containers = _splitter.convert(result.stdout); | |
if (containers.isEmpty) { | |
_log('No Containers. Waiting $_waitDuration', _green); | |
await new Future.delayed(_waitDuration); | |
continue; | |
} | |
var first = containers.first; | |
_log('Active containers: ${containers.join(', ')}', _green); | |
_log(' Tailing: $first', _green); | |
await _tailProcess(first); | |
} | |
} | |
void _log(String value, [String color]) { | |
if (color != null) stdout.write(color); | |
stdout.write(value); | |
if (color != null) stdout.write(_noColor); | |
stdout.writeln(); | |
} | |
Future _tailProcess(String container) async { | |
Process proc = await Process.start('docker', ['logs', '-f', container]); | |
var values = await Future.wait( | |
[proc.exitCode, _drainStd(proc.stdout), _drainStd(proc.stderr, _red)]); | |
_log('Container $container closed.', _green); | |
} | |
Future _drainStd(Stream<List<int>> std, [String color]) async { | |
await std | |
.transform(SYSTEM_ENCODING.decoder) | |
.transform(_splitter) | |
.forEach((line) { | |
_log(line, color); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment