8 from arvados.events import subscribe
11 def main(arguments=None):
12 logger = logging.getLogger('arvados.arv-ws')
14 parser = argparse.ArgumentParser()
15 parser.add_argument('-u', '--uuid', type=str, default="", help="Filter events on object_uuid")
16 parser.add_argument('-f', '--filters', type=str, default="", help="Arvados query filter to apply to log events (JSON encoded)")
17 parser.add_argument('-s', '--start-time', type=str, default="", help="Arvados query filter to fetch log events created at or after this time. This will be server time in UTC. Allowed format: YYYY-MM-DD or YYYY-MM-DD hh:mm:ss")
19 group = parser.add_mutually_exclusive_group()
20 group.add_argument('--poll-interval', default=15, type=int, help="If websockets is not available, specify the polling interval, default is every 15 seconds")
21 group.add_argument('--no-poll', action='store_false', dest='poll_interval', help="Do not poll if websockets are not available, just fail")
23 group = parser.add_mutually_exclusive_group()
24 group.add_argument('-p', '--pipeline', type=str, default="", help="Supply pipeline uuid, print log output from pipeline and its jobs")
25 group.add_argument('-j', '--job', type=str, default="", help="Supply job uuid, print log output from jobs")
27 args = parser.parse_args(arguments)
30 global known_component_jobs
34 known_component_jobs = set()
37 def update_subscribed_components(components):
38 global known_component_jobs
42 if "job" in components[c]:
43 pipeline_jobs.add(components[c]["job"]["uuid"])
44 if known_component_jobs != pipeline_jobs:
45 ws.unsubscribe(filters)
46 filters = [['object_uuid', 'in', [args.pipeline] + list(pipeline_jobs)]]
47 ws.subscribe([['object_uuid', 'in', [args.pipeline] + list(pipeline_jobs)]])
48 known_component_jobs = pipeline_jobs
50 api = arvados.api('v1')
53 filters += [ ['object_uuid', '=', args.uuid] ]
56 filters += json.loads(args.filters)
59 filters += [ ['object_uuid', '=', args.job] ]
62 filters += [ ['object_uuid', '=', args.pipeline] ]
66 filters += [ ['created_at', '>=', args.start_time] ]
75 if 'event_type' in ev and (args.pipeline or args.job):
76 if ev['event_type'] in ('stderr', 'stdout'):
77 sys.stdout.write(ev["properties"]["text"])
78 elif ev["event_type"] in ("create", "update"):
79 if ev["object_kind"] == "arvados#pipelineInstance":
80 update_subscribed_components(ev["properties"]["new_attributes"]["components"])
82 if ev["object_kind"] == "arvados#pipelineInstance" and args.pipeline:
83 if ev["properties"]["new_attributes"]["state"] in ("Complete", "Failed", "Paused"):
86 if ev["object_kind"] == "arvados#job" and args.job:
87 if ev["properties"]["new_attributes"]["state"] in ("Complete", "Failed", "Cancelled"):
89 elif 'status' in ev and ev['status'] == 200:
95 ws = subscribe(arvados.api('v1'), filters, on_message, poll_fallback=args.poll_interval, last_log_id=last_log_id)
98 c = api.pipeline_instances().get(uuid=args.pipeline).execute()
99 update_subscribed_components(c["components"])
100 if c["state"] in ("Complete", "Failed", "Paused"):
103 except KeyboardInterrupt:
105 except Exception as e: