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
=== modified file 'lib/ubuntu_repository_cache/metadata_sync.py'
--- lib/ubuntu_repository_cache/metadata_sync.py 2022-08-08 22:21:29 +0000
+++ lib/ubuntu_repository_cache/metadata_sync.py 2022-08-09 09:19:39 +0000
@@ -258,7 +258,7 @@
258 return os.path.basename(dest)258 return os.path.basename(dest)
259259
260260
261def main(environment, log, mirror_archive=mirror_archive, check_call=subprocess.check_call):261def main(environment, log, mirror_archive=mirror_archive, check_output=subprocess.check_output):
262 """The main function for this script.262 """The main function for this script.
263263
264 This is called at the very bottom of this file should the file be executed264 This is called at the very bottom of this file should the file be executed
@@ -293,19 +293,26 @@
293293
294 log("Executing hook: {}".format(cmd))294 log("Executing hook: {}".format(cmd))
295295
296 errors = []
296 # Work around Juju instability issues causing juju-run to abort297 # Work around Juju instability issues causing juju-run to abort
297 # (LP:1977798).298 # (LP:1977798).
298 max_retries = 10299 max_retries = 10
299 for attempt in range(1, max_retries):300 for attempt in range(1, max_retries):
300 try:301 try:
301 check_call(cmd, stdout=subprocess.DEVNULL)302 output = check_output(cmd, stderr=subprocess.STDOUT)
303 msg = "{}".format(output)
304 log(msg)
302 except subprocess.CalledProcessError as e:305 except subprocess.CalledProcessError as e:
303 # Save the exact exception and reuse when we need to306 # Save the exact exception and reuse when we need to
304 # reraise it after exhausting all available attempts.307 # reraise it after exhausting all available attempts.
305 exp = e308 exp = e
306 # Exponential backoff.309 # Exponential backoff.
307 sleep = attempt * random.randint(10, 60)310 sleep = random.randint(attempt, 10 * attempt)
308 log("Attempt {} failed, sleeping {} - retrying with {}".format(attempt, sleep, meta_ver))311 msg = "Attempt {} failed, sleeping {} - retrying with {}".format(attempt, sleep, meta_ver)
312 log(msg)
313 log(e.output.decode())
314 errors.append(msg)
315 errors.append(e.output.decode())
309 metric_name = 'ubuntu_repository_cache_metadata_sync_failures'316 metric_name = 'ubuntu_repository_cache_metadata_sync_failures'
310 label = 'meta_ver={}'.format(meta_ver)317 label = 'meta_ver={}'.format(meta_ver)
311 send_to_influx(render_influx(metric_name, label, attempt))318 send_to_influx(render_influx(metric_name, label, attempt))
@@ -315,7 +322,10 @@
315 else:322 else:
316 # Exhausted all available attempts, let's re-raise the323 # Exhausted all available attempts, let's re-raise the
317 # exception.324 # exception.
318 log("Failed after all available attempts with {}".format(meta_ver))325 msg = "Failed after all available attempts with {}".format(meta_ver)
326 log(msg)
327 print(msg)
328 print('\n'.join(errors))
319 metric_name = 'ubuntu_repository_cache_metadata_sync_failures'329 metric_name = 'ubuntu_repository_cache_metadata_sync_failures'
320 label = 'meta_ver={}'.format(meta_ver)330 label = 'meta_ver={}'.format(meta_ver)
321 send_to_influx(render_influx(metric_name, label, max_retries))331 send_to_influx(render_influx(metric_name, label, max_retries))
322332
=== modified file 'lib/ubuntu_repository_cache/tests/test_metadata_sync.py'
--- lib/ubuntu_repository_cache/tests/test_metadata_sync.py 2021-06-08 01:19:59 +0000
+++ lib/ubuntu_repository_cache/tests/test_metadata_sync.py 2022-08-09 09:19:39 +0000
@@ -154,7 +154,7 @@
154 "LOCAL_UNIT": "unit/0",154 "LOCAL_UNIT": "unit/0",
155 "UNIT_PATH": "something",155 "UNIT_PATH": "something",
156 }156 }
157 result = metadata_sync.main(env, InvocationRecorder(), mirror_archive=noop, check_call=noop)157 result = metadata_sync.main(env, InvocationRecorder(), mirror_archive=noop, check_output=noop)
158 self.assertEqual("Called", result)158 self.assertEqual("Called", result)
159159
160 def test_main_calls_juju_run(self):160 def test_main_calls_juju_run(self):
@@ -173,10 +173,10 @@
173 def fake_mirror_archive(*args, **kwargs):173 def fake_mirror_archive(*args, **kwargs):
174 return "some_meta_var"174 return "some_meta_var"
175175
176 fake_check_call = InvocationRecorder()176 fake_check_output = InvocationRecorder()
177177
178 result = metadata_sync.main(178 result = metadata_sync.main(
179 env, InvocationRecorder(), mirror_archive=fake_mirror_archive, check_call=fake_check_call179 env, InvocationRecorder(), mirror_archive=fake_mirror_archive, check_output=fake_check_output
180 )180 )
181181
182 self.assertEqual("some_meta_var", result)182 self.assertEqual("some_meta_var", result)
@@ -192,11 +192,11 @@
192 'ubuntu-repository-cache-sync some_meta_var',192 'ubuntu-repository-cache-sync some_meta_var',
193 ],193 ],
194 ),194 ),
195 {'stdout': -3},195 {'stderr': -2},
196 ]196 ]
197 ]197 ]
198198
199 self.assertEqual(expected, fake_check_call.invocations)199 self.assertEqual(expected, fake_check_output.invocations)
200200
201201
202def _filter_dict(d, keys):202def _filter_dict(d, keys):

Subscribers

People subscribed via source and target branches