]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Fix #7266 About Failing Runscript after not aborting job
authorMichal Rakowski <michal.rakowski@baculasystems.com>
Wed, 17 Feb 2021 07:27:42 +0000 (08:27 +0100)
committerEric Bollengier <eric@baculasystems.com>
Fri, 30 Jun 2023 16:34:25 +0000 (18:34 +0200)
bacula/src/dird/admin.c
bacula/src/dird/backup.c
bacula/src/dird/job.c
bacula/src/dird/mac.c
bacula/src/dird/restore.c
bacula/src/dird/vbackup.c
bacula/src/dird/verify.c
bacula/src/lib/jcr.c
bacula/src/lib/protos.h
bacula/src/lib/runscript.c

index 5f4a86fc8c774e70aa1522c6e35132646d1b7e58..860cf81f089605641bc33ee9ce9448e241405ee2 100644 (file)
@@ -77,6 +77,15 @@ void admin_cleanup(JCR *jcr, int TermCode)
 
    Dmsg0(100, "Enter admin_cleanup()\n");
 
+   /* Job needs to be marked as terminated before running the after runscript */
+   jcr->setJobStatus(TermCode);
+
+   run_scripts(jcr, jcr->job->RunScripts, "AfterJob");
+
+   /* Runscript could have changed JobStatus,
+    * now check if it should be changed in the report or not */
+   TermCode = compareJobStatus(TermCode, jcr->JobStatus);
+
    update_job_end(jcr, TermCode);
 
    if (!db_get_job_record(jcr, jcr->db, &jcr->jr)) {
@@ -88,7 +97,11 @@ void admin_cleanup(JCR *jcr, int TermCode)
    msg_type = M_INFO;                 /* by default INFO message */
    switch (jcr->JobStatus) {
    case JS_Terminated:
-      term_msg = _("Admin OK");
+      if (jcr->JobErrors) {
+         term_msg = _("Admin OK -- with warnings");
+      } else {
+         term_msg = _("Admin OK");
+      }
       break;
    case JS_FatalError:
    case JS_ErrorTerminated:
index e716ad4c1e92ab1bfb86bc9504518d77cc69c42b..a97d261351aadd6a9dbe187e99ee79fa9107dac7 100644 (file)
@@ -993,6 +993,15 @@ void backup_cleanup(JCR *jcr, int TermCode)
    Dmsg2(100, "Enter backup_cleanup %d %c\n", TermCode, TermCode);
    memset(&cr, 0, sizeof(cr));
 
+   /* Job needs to be marked as terminated before running the after runscript */
+   jcr->setJobStatus(TermCode);
+
+   run_scripts(jcr, jcr->job->RunScripts, "AfterJob");
+
+   /* Runscript could have changed JobStatus,
+    * now check if it should be changed in the report or not */
+   TermCode = compareJobStatus(TermCode, jcr->JobStatus);
+
 #ifdef xxxx
    /* The current implementation of the JS_Warning status is not
     * completed. SQL part looks to be ok, but the code is using
index 274913d5fe1272538d115046c0f2f59582d870f6..d59a9a580b3519066176aaf83b5c1586e179be1b 100644 (file)
@@ -509,8 +509,6 @@ static void *job_thread(void *arg)
       break;
    }
 
-   run_scripts(jcr, jcr->job->RunScripts, "AfterJob");
-
    /* Send off any queued messages */
    if (jcr->msg_queue && jcr->msg_queue->size() > 0) {
       dequeue_messages(jcr);
index 52c79bb01fbff31f522a5b2471247edd918eebc0..76ac57a1df770a74277a5cdc12ece9b86c1ce5c0 100644 (file)
@@ -703,7 +703,9 @@ void mac_cleanup(JCR *jcr, int TermCode, int writeTermCode)
    remove_dummy_jobmedia_records(jcr);
 
    Dmsg2(100, "Enter mac_cleanup %d %c\n", TermCode, TermCode);
-   update_job_end(jcr, TermCode);
+
+   /* Job needs to be marked as terminated before running the after runscript */
+   jcr->setJobStatus(TermCode);
 
    /*
     * Check if we actually did something.
@@ -848,6 +850,13 @@ void mac_cleanup(JCR *jcr, int TermCode, int writeTermCode)
          db_sql_query(wjcr->db, query.c_str(), NULL, NULL);
       }
    }
+   run_scripts(jcr, jcr->job->RunScripts, "AfterJob");
+
+   /* Runscript could have changed JobStatus,
+    * now check if it should be changed in the report or not */
+   TermCode = compareJobStatus(TermCode, jcr->JobStatus);
+
+   update_job_end(jcr, TermCode);
 
    switch (jcr->JobStatus) {
    case JS_Terminated:
index 7b49410f82338025133d884099b80759a4b8971e..89e09e3502bdba26e577d296309325971d5b5ca3 100644 (file)
@@ -658,6 +658,16 @@ void restore_cleanup(JCR *jcr, int TermCode)
    utime_t RunTime;
 
    Dmsg0(20, "In restore_cleanup\n");
+
+   /* Job needs to be marked as terminated before running the after runscript */
+   jcr->setJobStatus(TermCode);
+
+   run_scripts(jcr, jcr->job->RunScripts, "AfterJob");
+
+   /* Runscript could have changed JobStatus,
+    * now check if it should be changed in the report or not */
+   TermCode = compareJobStatus(TermCode, jcr->JobStatus);
+
    update_job_end(jcr, TermCode);
 
    memset(&cr, 0, sizeof(cr));
index c9060f153bc7f45cb3de5b572a55f5c4b63998ab..85a65977d12fbcd9119836f7161c6ca0ea6af35b 100644 (file)
@@ -402,6 +402,15 @@ void vbackup_cleanup(JCR *jcr, int TermCode)
    Dmsg2(100, "Enter vbackup_cleanup %d %c\n", TermCode, TermCode);
    memset(&cr, 0, sizeof(cr));
 
+   /* Job needs to be marked as terminated before running the after runscript */
+   jcr->setJobStatus(TermCode);
+
+   run_scripts(jcr, jcr->job->RunScripts, "AfterJob");
+
+   /* Runscript could have changed JobStatus,
+    * now check if it should be changed in the report or not */
+   TermCode = compareJobStatus(TermCode, jcr->JobStatus);
+
    jcr->jr.JobLevel = L_FULL;   /* we want this to appear as a Full backup */
    jcr->JobFiles = jcr->SDJobFiles;
    jcr->JobBytes = jcr->SDJobBytes;
index f03f1a43298e0e84fcc3ebb1ffedc493bc190bd0..cc26c46e8474a7bcca3dc7c0d10a98003f00673f 100644 (file)
@@ -456,6 +456,15 @@ void verify_cleanup(JCR *jcr, int TermCode)
       TermCode = JS_ErrorTerminated;
    }
 
+   /* Job needs to be marked as terminated before running the after runscript */
+   jcr->setJobStatus(TermCode);
+
+   run_scripts(jcr, jcr->job->RunScripts, "AfterJob");
+
+   /* Runscript could have changed JobStatus,
+    * now check if it should be changed in the report or not */
+   TermCode = compareJobStatus(TermCode, jcr->JobStatus);
+
    update_job_end(jcr, TermCode);
 
    if (job_canceled(jcr)) {
index 7ab1fbffd43beadf50e2dad2de56753448e9a246..16be87520927ffc6fb9ec08b2a44d96fb19e6d7e 100644 (file)
@@ -923,22 +923,16 @@ void JCR::setJobStarted()
    job_started_time = time(NULL);
 }
 
-static pthread_mutex_t status_lock = PTHREAD_MUTEX_INITIALIZER;
-
-void JCR::setJobStatus(int newJobStatus)
+/*
+ * Compare two job statuses, return one with higher priority.
+ */
+int compareJobStatus(int oldJobStatus, int newJobStatus)
 {
    int priority, old_priority;
-   int oldJobStatus = JobStatus;
+   int ret_status = oldJobStatus;
 
-   P(status_lock);
    priority = get_status_priority(newJobStatus);
    old_priority = get_status_priority(oldJobStatus);
-
-   Dmsg2(800, "set_jcr_job_status(%ld, %c)\n", JobId, newJobStatus);
-
-   /* Update wait_time depending on newJobStatus and oldJobStatus */
-   update_wait_time(this, newJobStatus);
-
    /*
     * For a set of errors, ... keep the current status
     *   so it isn't lost. For all others, set it.
@@ -955,13 +949,31 @@ void JCR::setJobStatus(int newJobStatus)
        priority == 0 && old_priority == 0)) {
       Dmsg4(800, "Set new stat. old: %c,%d new: %c,%d\n",
             (oldJobStatus==0)?'0':oldJobStatus, old_priority, newJobStatus, priority);
-      JobStatus = newJobStatus;     /* replace with new status */
+      ret_status = newJobStatus;     /* replace with new status */
    }
 
-   if (oldJobStatus != JobStatus) {
+   if (oldJobStatus != newJobStatus) {
       Dmsg2(800, "leave setJobStatus old=%c new=%c\n", (oldJobStatus==0)?'0':oldJobStatus, newJobStatus);
 //    generate_plugin_event(this, bEventStatusChange, NULL);
    }
+
+   return ret_status;
+}
+
+static pthread_mutex_t status_lock = PTHREAD_MUTEX_INITIALIZER;
+
+void JCR::setJobStatus(int newJobStatus)
+{
+
+   P(status_lock);
+
+   Dmsg2(800, "set_jcr_job_status(%ld, %c)\n", JobId, newJobStatus);
+
+   /* Update wait_time depending on newJobStatus and oldJobStatus */
+   update_wait_time(this, newJobStatus);
+
+   JobStatus = compareJobStatus(JobStatus, newJobStatus);
+
    V(status_lock);
 }
 
index d84306339bbec408068207d336a913ce754e5bac..b3bd62c51f8382e488b39272eb4b5a7e788b29ee 100644 (file)
@@ -264,6 +264,7 @@ void     set_jcr_in_tsd(JCR *jcr);
 void     remove_jcr_from_tsd(JCR *jcr);
 uint32_t get_jobid_from_tsd();
 uint32_t get_jobid_from_tid(pthread_t tid);
+int compareJobStatus(int oldJobStatus, int newJobStatus);
 
 
 /* lex.c */
index 967fa2e7d2215b696d0530577889cf8ca0bc5b7f..c43b312a33ffc7c31806283a508ba2944d2d5075 100644 (file)
@@ -100,7 +100,7 @@ int run_scripts(JCR *jcr, alist *runscripts, const char *label)
 
    RUNSCRIPT *script;
    bool runit;
-
+   bool ret = true; /* Changed to false in case any of the scripts fails */
    int when;
 
    if (strstr(label, NT_("Before"))) {
@@ -166,10 +166,13 @@ int run_scripts(JCR *jcr, alist *runscripts, const char *label)
 
       /* we execute it */
       if (runit) {
-         script->run(jcr, label);
+         if (!script->run(jcr, label)) {
+            ret = false; /* return err in case any of the script commands failed */
+         }
       }
    }
-   return 1;
+
+   return ret;
 }
 
 bool RUNSCRIPT::is_local()