Skip to content

Commit 4839d36

Browse files
committed
implement logging timestamps and record container ID in a file
1 parent 52c276d commit 4839d36

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

cwltool/job.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import subprocess
1212
import sys
1313
import tempfile
14+
import datetime
1415
from io import open
1516
from typing import (IO, Any, Callable, Dict, Iterable, List, MutableMapping, Text,
1617
Tuple, Union, cast)
@@ -363,6 +364,8 @@ def add_volumes(self, pathmapper, runtime):
363364
docker_windows_path_adjust(vol.target)))
364365

365366
def run(self, pull_image=True, rm_container=True,
367+
record_container_id=False, cidfile_dir="/tmp/",
368+
cidfile_prefix="",
366369
rm_tmpdir=True, move_outputs="move", **kwargs):
367370
# type: (bool, bool, bool, Text, **Any) -> None
368371

@@ -463,6 +466,25 @@ def run(self, pull_image=True, rm_container=True,
463466
# directory." but spec might change to designated temp directory.
464467
# runtime.append("--env=HOME=/tmp")
465468
runtime.append(u"--env=HOME=%s" % self.builder.outdir)
469+
470+
# add parameters to docker to write a container ID file
471+
if record_container_id:
472+
if cidfile_dir:
473+
if not os.path.isdir(cidfile_dir):
474+
_logger.error("--cidfile-dir %s error:\n%s", cidfile_dir,
475+
cidfile_dir + " is not a directory or directory doesn't exist, please check it first")
476+
exit(-1)
477+
if not os.path.exists(cidfile_dir):
478+
_logger.error("--cidfile-dir %s error:\n%s", cidfile_dir,
479+
"directory doesn't exist, please create it first")
480+
exit(-1)
481+
else:
482+
cidfile_dir = "/tmp/"
483+
cidfile_name = datetime.datetime.now().strftime("%Y%m%d%H%M%S-%f")+".cid"
484+
if cidfile_prefix != "":
485+
cidfile_name = cidfile_prefix + "-" + cidfile_name
486+
cidfile_path = cidfile_dir + cidfile_name
487+
runtime.append(u"--cidfile=%s" % cidfile_path)
466488

467489
for t, v in self.environment.items():
468490
runtime.append(u"--env=%s=%s" % (t, v))

cwltool/main.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ def arg_parser(): # type: () -> argparse.ArgumentParser
8484
default=True, help="Do not delete Docker container used by jobs after they exit",
8585
dest="rm_container")
8686

87+
parser.add_argument("--record-container-id", action="store_true", default=False,
88+
help="If enabled, a file with suffix \".cid\" will be created storing the container ID under CIDFILE_DIR",
89+
dest="record_container_id")
90+
91+
parser.add_argument("--cidfile-dir", type=Text,
92+
help="Directory for storing cidfiles. Default at /tmp/",
93+
default="/tmp/",
94+
dest="cidfile_dir")
95+
96+
parser.add_argument("--cidfile-prefix", type=Text,
97+
help="Give a prefix to cidfile. Final file name will be followed by a timestamp. Default empty.",
98+
default="",
99+
dest="cidfile_prefix")
100+
87101
parser.add_argument("--tmpdir-prefix", type=Text,
88102
help="Path prefix for temporary directories",
89103
default="tmp")
@@ -161,6 +175,7 @@ def arg_parser(): # type: () -> argparse.ArgumentParser
161175
exgroup.add_argument("--verbose", action="store_true", help="Default logging")
162176
exgroup.add_argument("--quiet", action="store_true", help="Only print warnings and errors.")
163177
exgroup.add_argument("--debug", action="store_true", help="Print even more logging")
178+
parser.add_argument("--logtstp", action="store_true", help="Print timestamps with logging")
164179

165180
parser.add_argument("--js-console", action="store_true", help="Enable javascript console output")
166181
parser.add_argument("--user-space-docker-cmd",
@@ -774,6 +789,7 @@ def main(argsl=None, # type: List[str]
774789
'cachedir': None,
775790
'quiet': False,
776791
'debug': False,
792+
'logtstp': False,
777793
'js_console': False,
778794
'version': False,
779795
'enable_dev': False,
@@ -802,6 +818,10 @@ def main(argsl=None, # type: List[str]
802818
_logger.setLevel(logging.WARN)
803819
if args.debug:
804820
_logger.setLevel(logging.DEBUG)
821+
if args.logtstp:
822+
formatter = logging.Formatter("[%(asctime)s] %(message)s",
823+
"%Y-%m-%d %H:%M:%S")
824+
stderr_handler.setFormatter(formatter)
805825

806826
if args.version:
807827
print(versionfunc())

0 commit comments

Comments
 (0)