]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/job.c
- Convert more atoi to str_to_int64() for DB.
[bacula/bacula] / bacula / src / dird / job.c
index 1107ef425b1049a790144075acc146873fe7ac4a..8175a89258ed14634fcf97cbe1076add0cfb25ee 100644 (file)
@@ -36,16 +36,9 @@ static void job_monitor_destructor(watchdog_t *self);
 static bool job_check_maxwaittime(JCR *control_jcr, JCR *jcr);
 static bool job_check_maxruntime(JCR *control_jcr, JCR *jcr);
 
-/* Exported subroutines */
-
 /* Imported subroutines */
 extern void term_scheduler();
 extern void term_ua_server();
-extern int do_backup(JCR *jcr);
-extern bool do_mac(JCR *jcr);
-extern int do_admin(JCR *jcr);
-extern int do_restore(JCR *jcr);
-extern bool do_verify(JCR *jcr);
 
 /* Imported variables */
 extern time_t watchdog_time;
@@ -61,9 +54,7 @@ void init_job_server(int max_workers)
       berrno be;
       Emsg1(M_ABORT, 0, _("Could not init job queue: ERR=%s\n"), be.strerror(stat));
    }
-   if ((wd = new_watchdog()) == NULL) {
-      Emsg0(M_ABORT, 0, _("Could not init job monitor watchdogs\n"));
-   }
+   wd = new_watchdog();
    wd->callback = job_monitor_watchdog;
    wd->destructor = job_monitor_destructor;
    wd->one_shot = false;
@@ -143,6 +134,44 @@ JobId_t run_job(JCR *jcr)
       jcr->fname = get_pool_memory(PM_FNAME);
    }
 
+   /* Now, do pre-run stuff, like setting job level (Inc/diff, ...) */
+   switch (jcr->JobType) {
+   case JT_BACKUP:
+      if (!do_backup_init(jcr)) {
+        backup_cleanup(jcr, JS_ErrorTerminated);
+      }
+      break;
+   case JT_VERIFY:
+      if (!do_verify_init(jcr)) {
+        verify_cleanup(jcr, JS_ErrorTerminated);
+      }
+      break;
+   case JT_RESTORE:
+      if (!do_restore_init(jcr)) {
+        restore_cleanup(jcr, JS_ErrorTerminated);
+      }
+      break;
+   case JT_ADMIN:
+      if (!do_admin_init(jcr)) {
+        admin_cleanup(jcr, JS_ErrorTerminated);
+      }
+      break;
+   case JT_MIGRATION:
+   case JT_COPY:
+   case JT_ARCHIVE:
+      if (!do_mac_init(jcr)) {            /* migration, archive, copy */
+        mac_cleanup(jcr, JS_ErrorTerminated);
+      }
+      break;
+   default:
+      Pmsg1(0, "Unimplemented job type: %d\n", jcr->JobType);
+      set_jcr_job_status(jcr, JS_ErrorTerminated);
+      break;
+   }
+   if (job_canceled(jcr)) {
+      goto bail_out;
+   }
+
    Dmsg0(200, "Add jrc to work queue\n");
 
    /* Queue the job to be run */
@@ -158,7 +187,10 @@ JobId_t run_job(JCR *jcr)
    return JobId;
 
 bail_out:
-   set_jcr_job_status(jcr, JS_ErrorTerminated);
+   if (jcr->fname) {
+      free_memory(jcr->fname);
+      jcr->fname = NULL;
+   }
    V(jcr->mutex);
    return JobId;
 
@@ -222,35 +254,40 @@ static void *job_thread(void *arg)
         }
         switch (jcr->JobType) {
         case JT_BACKUP:
-           do_backup(jcr);
-           if (jcr->JobStatus == JS_Terminated) {
+           if (do_backup(jcr)) {
               do_autoprune(jcr);
+           } else {
+              backup_cleanup(jcr, JS_ErrorTerminated);
            }
            break;
         case JT_VERIFY:
-           do_verify(jcr);
-           if (jcr->JobStatus == JS_Terminated) {
+           if (do_verify(jcr)) {
               do_autoprune(jcr);
+           } else {
+              verify_cleanup(jcr, JS_ErrorTerminated);
            }
            break;
         case JT_RESTORE:
-           do_restore(jcr);
-           if (jcr->JobStatus == JS_Terminated) {
+           if (do_restore(jcr)) {
               do_autoprune(jcr);
+           } else {
+              restore_cleanup(jcr, JS_ErrorTerminated);
            }
            break;
         case JT_ADMIN:
-           do_admin(jcr);
-           if (jcr->JobStatus == JS_Terminated) {
+           if (do_admin(jcr)) {
               do_autoprune(jcr);
+           } else {
+              admin_cleanup(jcr, JS_ErrorTerminated);
            }
            break;
         case JT_MIGRATION:
         case JT_COPY:
         case JT_ARCHIVE:
-           do_mac(jcr);              /* migration, archive, copy */
-           if (jcr->JobStatus == JS_Terminated) {
+           if (do_mac(jcr)) {              /* migration, archive, copy */
               do_autoprune(jcr);
+           } else {
+              mac_cleanup(jcr, JS_ErrorTerminated);
            }
            break;
         default:
@@ -436,18 +473,32 @@ static void job_monitor_watchdog(watchdog_t *self)
 static bool job_check_maxwaittime(JCR *control_jcr, JCR *jcr)
 {
    bool cancel = false;
+   bool ok_to_cancel = false;
+   JOB *job = jcr->job;
 
-   if (jcr->job->MaxWaitTime == 0) {
+   if (job->MaxWaitTime == 0 && job->FullMaxWaitTime == 0 &&
+       job->IncMaxWaitTime == 0 && job->DiffMaxWaitTime == 0) {
       return false;
-   }
-   if ((watchdog_time - jcr->start_time) < jcr->job->MaxWaitTime) {
-      Dmsg3(800, "Job %p (%s) with MaxWaitTime %d not expired\n",
-           jcr, jcr->Job, jcr->job->MaxWaitTime);
+   } 
+   if (jcr->JobLevel == L_FULL && job->FullMaxWaitTime != 0 &&
+        (watchdog_time - jcr->start_time) >= job->FullMaxWaitTime) {
+      ok_to_cancel = true;
+   } else if (jcr->JobLevel == L_DIFFERENTIAL && job->DiffMaxWaitTime != 0 &&
+        (watchdog_time - jcr->start_time) >= job->DiffMaxWaitTime) {
+      ok_to_cancel = true;
+   } else if (jcr->JobLevel == L_INCREMENTAL && job->IncMaxWaitTime != 0 &&
+        (watchdog_time - jcr->start_time) >= job->IncMaxWaitTime) {
+      ok_to_cancel = true;
+   } else if (job->MaxWaitTime != 0 &&
+        (watchdog_time - jcr->start_time) >= job->MaxWaitTime) {
+      ok_to_cancel = true;
+   }
+   if (!ok_to_cancel) {
       return false;
    }
    Dmsg3(800, "Job %d (%s): MaxWaitTime of %d seconds exceeded, "
          "checking status\n",
-        jcr->JobId, jcr->Job, jcr->job->MaxWaitTime);
+        jcr->JobId, jcr->Job, job->MaxWaitTime);
    switch (jcr->JobStatus) {
    case JS_Created:
    case JS_Blocked:
@@ -687,15 +738,9 @@ void create_unique_job_name(JCR *jcr, const char *base_name)
    }
 }
 
-/*
- * Free the Job Control Record if no one is still using it.
- *  Called from main free_jcr() routine in src/lib/jcr.c so
- *  that we can do our Director specific cleanup of the jcr.
- */
-void dird_free_jcr(JCR *jcr)
+/* Called directly from job rescheduling */
+void dird_free_jcr_pointers(JCR *jcr)
 {
-   Dmsg0(200, "Start dird free_jcr\n");
-
    if (jcr->sd_auth_key) {
       free(jcr->sd_auth_key);
       jcr->sd_auth_key = NULL;
@@ -734,7 +779,21 @@ void dird_free_jcr(JCR *jcr)
    }
    if (jcr->term_wait_inited) {
       pthread_cond_destroy(&jcr->term_wait);
+      jcr->term_wait_inited = false;
    }
+}
+
+/*
+ * Free the Job Control Record if no one is still using it.
+ *  Called from main free_jcr() routine in src/lib/jcr.c so
+ *  that we can do our Director specific cleanup of the jcr.
+ */
+void dird_free_jcr(JCR *jcr)
+{
+   Dmsg0(200, "Start dird free_jcr\n");
+
+   dird_free_jcr_pointers(jcr);
+
    /* Delete lists setup to hold storage pointers */
    if (jcr->storage) {
       delete jcr->storage;