Merge lp:~hloeung/ubuntu-repository-cache/improve-logging-of-juju-run-failures into lp:ubuntu-repository-cache

Proposed by Haw Loeung
Status: Merged
Approved by: Haw Loeung
Approved revision: 370
Merged at revision: 368
Proposed branch: lp:~hloeung/ubuntu-repository-cache/improve-logging-of-juju-run-failures
Merge into: lp:ubuntu-repository-cache
Diff against target: 94 lines (+20/-10)
2 files modified
lib/ubuntu_repository_cache/metadata_sync.py (+15/-5)
lib/ubuntu_repository_cache/tests/test_metadata_sync.py (+5/-5)
To merge this branch: bzr merge lp:~hloeung/ubuntu-repository-cache/improve-logging-of-juju-run-failures
Reviewer Review Type Date Requested Status
Barry Price Approve
Canonical IS Reviewers Pending
Review via email: mp+428081@code.launchpad.net

Commit message

Improve logging and output of errors from juju-run failures

We save both STDOUT and STDERR then log it as well as output it for
inclusion in the cron e-mails.

To post a comment you must log in.
Revision history for this message
Barry Price (barryprice) wrote :

LGTM +1

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 368

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/ubuntu_repository_cache/metadata_sync.py'
2--- lib/ubuntu_repository_cache/metadata_sync.py 2022-08-08 22:21:29 +0000
3+++ lib/ubuntu_repository_cache/metadata_sync.py 2022-08-09 09:19:39 +0000
4@@ -258,7 +258,7 @@
5 return os.path.basename(dest)
6
7
8-def main(environment, log, mirror_archive=mirror_archive, check_call=subprocess.check_call):
9+def main(environment, log, mirror_archive=mirror_archive, check_output=subprocess.check_output):
10 """The main function for this script.
11
12 This is called at the very bottom of this file should the file be executed
13@@ -293,19 +293,26 @@
14
15 log("Executing hook: {}".format(cmd))
16
17+ errors = []
18 # Work around Juju instability issues causing juju-run to abort
19 # (LP:1977798).
20 max_retries = 10
21 for attempt in range(1, max_retries):
22 try:
23- check_call(cmd, stdout=subprocess.DEVNULL)
24+ output = check_output(cmd, stderr=subprocess.STDOUT)
25+ msg = "{}".format(output)
26+ log(msg)
27 except subprocess.CalledProcessError as e:
28 # Save the exact exception and reuse when we need to
29 # reraise it after exhausting all available attempts.
30 exp = e
31 # Exponential backoff.
32- sleep = attempt * random.randint(10, 60)
33- log("Attempt {} failed, sleeping {} - retrying with {}".format(attempt, sleep, meta_ver))
34+ sleep = random.randint(attempt, 10 * attempt)
35+ msg = "Attempt {} failed, sleeping {} - retrying with {}".format(attempt, sleep, meta_ver)
36+ log(msg)
37+ log(e.output.decode())
38+ errors.append(msg)
39+ errors.append(e.output.decode())
40 metric_name = 'ubuntu_repository_cache_metadata_sync_failures'
41 label = 'meta_ver={}'.format(meta_ver)
42 send_to_influx(render_influx(metric_name, label, attempt))
43@@ -315,7 +322,10 @@
44 else:
45 # Exhausted all available attempts, let's re-raise the
46 # exception.
47- log("Failed after all available attempts with {}".format(meta_ver))
48+ msg = "Failed after all available attempts with {}".format(meta_ver)
49+ log(msg)
50+ print(msg)
51+ print('\n'.join(errors))
52 metric_name = 'ubuntu_repository_cache_metadata_sync_failures'
53 label = 'meta_ver={}'.format(meta_ver)
54 send_to_influx(render_influx(metric_name, label, max_retries))
55
56=== modified file 'lib/ubuntu_repository_cache/tests/test_metadata_sync.py'
57--- lib/ubuntu_repository_cache/tests/test_metadata_sync.py 2021-06-08 01:19:59 +0000
58+++ lib/ubuntu_repository_cache/tests/test_metadata_sync.py 2022-08-09 09:19:39 +0000
59@@ -154,7 +154,7 @@
60 "LOCAL_UNIT": "unit/0",
61 "UNIT_PATH": "something",
62 }
63- result = metadata_sync.main(env, InvocationRecorder(), mirror_archive=noop, check_call=noop)
64+ result = metadata_sync.main(env, InvocationRecorder(), mirror_archive=noop, check_output=noop)
65 self.assertEqual("Called", result)
66
67 def test_main_calls_juju_run(self):
68@@ -173,10 +173,10 @@
69 def fake_mirror_archive(*args, **kwargs):
70 return "some_meta_var"
71
72- fake_check_call = InvocationRecorder()
73+ fake_check_output = InvocationRecorder()
74
75 result = metadata_sync.main(
76- env, InvocationRecorder(), mirror_archive=fake_mirror_archive, check_call=fake_check_call
77+ env, InvocationRecorder(), mirror_archive=fake_mirror_archive, check_output=fake_check_output
78 )
79
80 self.assertEqual("some_meta_var", result)
81@@ -192,11 +192,11 @@
82 'ubuntu-repository-cache-sync some_meta_var',
83 ],
84 ),
85- {'stdout': -3},
86+ {'stderr': -2},
87 ]
88 ]
89
90- self.assertEqual(expected, fake_check_call.invocations)
91+ self.assertEqual(expected, fake_check_output.invocations)
92
93
94 def _filter_dict(d, keys):

Subscribers

People subscribed via source and target branches