From 8cae96b225f6183b298a75a0071d5855ab9aa2e3 Mon Sep 17 00:00:00 2001
From: Colm Talbot <colm.talbot@ligo.org>
Date: Fri, 8 May 2020 21:58:28 -0500
Subject: [PATCH] Generic exit code

---
 bilby/core/sampler/base_sampler.py | 6 +++++-
 bilby/core/sampler/cpnest.py       | 7 ++++++-
 bilby/core/sampler/dynesty.py      | 2 +-
 bilby/core/sampler/ptemcee.py      | 2 +-
 bilby/core/sampler/pymultinest.py  | 1 +
 bilby/core/sampler/ultranest.py    | 2 +-
 6 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/bilby/core/sampler/base_sampler.py b/bilby/core/sampler/base_sampler.py
index a5ea48eb..6bf70b88 100644
--- a/bilby/core/sampler/base_sampler.py
+++ b/bilby/core/sampler/base_sampler.py
@@ -70,6 +70,8 @@ class Sampler(object):
         only advisable for testing environments
     result: bilby.core.result.Result
         Container for the results of the sampling run
+    exit_code: int
+        System exit code to return on interrupt
     kwargs: dict
         Dictionary of keyword arguments that can be used in the external sampler
 
@@ -91,7 +93,7 @@ class Sampler(object):
             self, likelihood, priors, outdir='outdir', label='label',
             use_ratio=False, plot=False, skip_import_verification=False,
             injection_parameters=None, meta_data=None, result_class=None,
-            likelihood_benchmark=False, soft_init=False,
+            likelihood_benchmark=False, soft_init=False, exit_code=130,
             **kwargs):
         self.likelihood = likelihood
         if isinstance(priors, PriorDict):
@@ -114,6 +116,8 @@ class Sampler(object):
         self._constraint_parameter_keys = list()
         self._initialise_parameters()
 
+        self.exit_code = exit_code
+
         if not soft_init:
             self._verify_parameters()
             self._time_likelihood()
diff --git a/bilby/core/sampler/cpnest.py b/bilby/core/sampler/cpnest.py
index 4c46e02f..63f45478 100644
--- a/bilby/core/sampler/cpnest.py
+++ b/bilby/core/sampler/cpnest.py
@@ -105,7 +105,12 @@ class Cpnest(NestedSampler):
                 logger.info(
                     "Attempting to rerun with kwarg {} removed".format(kwarg))
                 self.kwargs.pop(kwarg)
-        out.run()
+        try:
+            out.run()
+        except SystemExit as e:
+            import sys
+            logger.info(f"Caught exit code {e.args[0]}, exiting with signal {self.exit_code}")
+            sys.exit(self.exit_code)
 
         if self.plot:
             out.plot()
diff --git a/bilby/core/sampler/dynesty.py b/bilby/core/sampler/dynesty.py
index 6dcf9fd1..a2d0be34 100644
--- a/bilby/core/sampler/dynesty.py
+++ b/bilby/core/sampler/dynesty.py
@@ -147,6 +147,7 @@ class Dynesty(NestedSampler):
         super(Dynesty, self).__init__(likelihood=likelihood, priors=priors,
                                       outdir=outdir, label=label, use_ratio=use_ratio,
                                       plot=plot, skip_import_verification=skip_import_verification,
+                                      exit_code=exit_code,
                                       **kwargs)
         self.n_check_point = n_check_point
         self.check_point = check_point
@@ -164,7 +165,6 @@ class Dynesty(NestedSampler):
 
         self.resume_file = '{}/{}_resume.pickle'.format(self.outdir, self.label)
         self.sampling_time = datetime.timedelta()
-        self.exit_code = exit_code
 
         try:
             signal.signal(signal.SIGTERM, self.write_current_state_and_exit)
diff --git a/bilby/core/sampler/ptemcee.py b/bilby/core/sampler/ptemcee.py
index 52afd644..0e9b71dc 100644
--- a/bilby/core/sampler/ptemcee.py
+++ b/bilby/core/sampler/ptemcee.py
@@ -156,6 +156,7 @@ class Ptemcee(MCMCSampler):
             use_ratio=use_ratio,
             plot=plot,
             skip_import_verification=skip_import_verification,
+            exit_code=exit_code,
             **kwargs
         )
 
@@ -169,7 +170,6 @@ class Ptemcee(MCMCSampler):
         signal.signal(signal.SIGALRM, self.write_current_state_and_exit)
 
         # Checkpointing inputs
-        self.exit_code = exit_code
         self.resume = resume
         self.check_point_deltaT = check_point_deltaT
         self.check_point_plot = check_point_plot
diff --git a/bilby/core/sampler/pymultinest.py b/bilby/core/sampler/pymultinest.py
index 8fd24230..f962c314 100644
--- a/bilby/core/sampler/pymultinest.py
+++ b/bilby/core/sampler/pymultinest.py
@@ -84,6 +84,7 @@ class Pymultinest(NestedSampler):
             use_ratio=use_ratio,
             plot=plot,
             skip_import_verification=skip_import_verification,
+            exit_code=exit_code,
             **kwargs
         )
         self._apply_multinest_boundaries()
diff --git a/bilby/core/sampler/ultranest.py b/bilby/core/sampler/ultranest.py
index 50dd4fe7..1cba66f4 100644
--- a/bilby/core/sampler/ultranest.py
+++ b/bilby/core/sampler/ultranest.py
@@ -91,10 +91,10 @@ class Ultranest(NestedSampler):
             use_ratio=use_ratio,
             plot=plot,
             skip_import_verification=skip_import_verification,
+            exit_code=exit_code,
             **kwargs,
         )
         self._apply_ultranest_boundaries()
-        self.exit_code = exit_code
 
         signal.signal(signal.SIGTERM, self.write_current_state_and_exit)
         signal.signal(signal.SIGINT, self.write_current_state_and_exit)
-- 
GitLab