]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/job.c
Fix line I accidently truncated
[bacula/bacula] / bacula / src / dird / job.c
index 0301534d9e605e6b64822a1baf0b14f2f7ebc162..b158ce2d1fd7eaeb74b64947bc03f465381d898f 100644 (file)
@@ -9,7 +9,7 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
+   Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
@@ -156,6 +156,10 @@ bool setup_job(JCR *jcr)
     * Create Job record
     */
    init_jcr_job_record(jcr);
+   if (!get_or_create_client_record(jcr)) {
+      goto bail_out;
+   }
+
    if (!db_create_job_record(jcr, jcr->db, &jcr->jr)) {
       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
       goto bail_out;
@@ -164,17 +168,12 @@ bool setup_job(JCR *jcr)
    Dmsg4(100, "Created job record JobId=%d Name=%s Type=%c Level=%c\n",
        jcr->JobId, jcr->Job, jcr->jr.JobType, jcr->jr.JobLevel);
 
-   if (!get_or_create_client_record(jcr)) {
-      goto bail_out;
-   }
-
    generate_daemon_event(jcr, "JobStart");
 
    if (job_canceled(jcr)) {
       goto bail_out;
    }
 
-
    /*
     * Now, do pre-run stuff, like setting job level (Inc/diff, ...)
     *  this allows us to setup a proper job start record for restarting
@@ -219,6 +218,13 @@ bail_out:
    return false;
 }
 
+void update_job_end(JCR *jcr, int TermCode)
+{
+   dequeue_messages(jcr);             /* display any queued messages */
+   set_jcr_job_status(jcr, TermCode);
+   run_scripts(jcr, jcr->job->RunScripts, "AfterJob");
+   update_job_end_record(jcr);
+}
 
 /*
  * This is the engine called by jobq.c:jobq_add() when we were pulled
@@ -241,8 +247,8 @@ static void *job_thread(void *arg)
 
    if (jcr->job->MaxStartDelay != 0 && jcr->job->MaxStartDelay <
        (utime_t)(jcr->start_time - jcr->sched_time)) {
-      Jmsg(jcr, M_FATAL, 0, _("Job canceled because max start delay time exceeded.\n"));
       set_jcr_job_status(jcr, JS_Canceled);
+      Jmsg(jcr, M_FATAL, 0, _("Job canceled because max start delay time exceeded.\n"));
    }
 
    /* TODO : check if it is used somewhere */
@@ -259,7 +265,7 @@ static void *job_thread(void *arg)
    run_scripts(jcr, jcr->job->RunScripts, "BeforeJob");
 
    if (job_canceled(jcr)) {
-      update_job_end_record(jcr);
+      update_job_end(jcr, jcr->JobStatus);
 
    } else {
       /*
@@ -322,8 +328,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);
@@ -347,6 +351,7 @@ static void *job_thread(void *arg)
 bool cancel_job(UAContext *ua, JCR *jcr)
 {
    BSOCK *sd, *fd;
+   char ed1[50];
 
    set_jcr_job_status(jcr, JS_Canceled);
 
@@ -358,8 +363,8 @@ bool cancel_job(UAContext *ua, JCR *jcr)
    case JS_WaitPriority:
    case JS_WaitMaxJobs:
    case JS_WaitStartTime:
-      bsendmsg(ua, _("JobId %d, Job %s marked to be canceled.\n"),
-              jcr->JobId, jcr->Job);
+      bsendmsg(ua, _("JobId %s, Job %s marked to be canceled.\n"),
+              edit_uint64(jcr->JobId, ed1), jcr->Job);
       jobq_remove(&job_queue, jcr); /* attempt to remove it from queue */
       return true;
 
@@ -394,10 +399,8 @@ bool cancel_job(UAContext *ua, JCR *jcr)
             USTORE store;
             if (jcr->rstorage) {
                store.store = jcr->rstore;
-               pm_strcpy(store.store_source, jcr->rstore_source);
             } else {
                store.store = jcr->wstore;
-               pm_strcpy(store.store_source, jcr->wstore_source);
             }
             set_wstorage(ua->jcr, &store);
          }
@@ -438,34 +441,36 @@ static void job_monitor_watchdog(watchdog_t *self)
    Dmsg1(800, "job_monitor_watchdog %p called\n", self);
 
    foreach_jcr(jcr) {
-      bool cancel;
+      bool cancel = false;
 
-      if (jcr->JobId == 0) {
-         Dmsg2(800, "Skipping JCR %p (%s) with JobId 0\n",
-               jcr, jcr->Job);
+      if (jcr->JobId == 0 || job_canceled(jcr)) {
+         Dmsg2(800, "Skipping JCR=%p Job=%s\n", jcr, jcr->Job);
          continue;
       }
 
       /* check MaxWaitTime */
-      cancel = job_check_maxwaittime(control_jcr, jcr);
-
+      if (job_check_maxwaittime(control_jcr, jcr)) {
+         set_jcr_job_status(jcr, JS_Canceled);
+         Jmsg(jcr, M_FATAL, 0, _("Max wait time exceeded. Job canceled.\n"));
+         cancel = true;
       /* check MaxRunTime */
-      cancel |= job_check_maxruntime(control_jcr, jcr);
+      } else if (job_check_maxruntime(control_jcr, jcr)) {
+         set_jcr_job_status(jcr, JS_Canceled);
+         Jmsg(jcr, M_FATAL, 0, _("Max run time exceeded. Job canceled.\n"));
+         cancel = true;
+      }
 
       if (cancel) {
-         Dmsg3(800, "Cancelling JCR %p jobid %d (%s)\n",
-               jcr, jcr->JobId, jcr->Job);
-
+         Dmsg3(800, "Cancelling JCR %p jobid %d (%s)\n", jcr, jcr->JobId, jcr->Job);
          UAContext *ua = new_ua_context(jcr);
          ua->jcr = control_jcr;
          cancel_job(ua, jcr);
          free_ua_context(ua);
-
          Dmsg2(800, "Have cancelled JCR %p Job=%d\n", jcr, jcr->JobId);
       }
 
-      /* Keep reference counts correct */
    }
+   /* Keep reference counts correct */
    endeach_jcr(jcr);
 }
 
@@ -479,6 +484,9 @@ static bool job_check_maxwaittime(JCR *control_jcr, JCR *jcr)
    bool ok_to_cancel = false;
    JOB *job = jcr->job;
 
+   if (job_canceled(jcr)) {
+      return false;                /* already canceled */
+   }
    if (job->MaxWaitTime == 0 && job->FullMaxWaitTime == 0 &&
        job->IncMaxWaitTime == 0 && job->DiffMaxWaitTime == 0) {
       return false;
@@ -499,6 +507,11 @@ static bool job_check_maxwaittime(JCR *control_jcr, JCR *jcr)
    if (!ok_to_cancel) {
       return false;
    }
+
+/*
+ * I don't see the need for all this -- kes 17Dec06
+ */
+#ifdef xxx
    Dmsg3(800, "Job %d (%s): MaxWaitTime of %d seconds exceeded, "
          "checking status\n",
          jcr->JobId, jcr->Job, job->MaxWaitTime);
@@ -541,8 +554,8 @@ static bool job_check_maxwaittime(JCR *control_jcr, JCR *jcr)
             jcr->JobStatus);
    }
    Dmsg3(800, "MaxWaitTime result: %scancel JCR %p (%s)\n",
-         cancel ? "" : "do not ", jcr, jcr->job);
-
+         cancel ? "" : "do not ", jcr, jcr->Job);
+#endif
    return cancel;
 }
 
@@ -552,9 +565,7 @@ static bool job_check_maxwaittime(JCR *control_jcr, JCR *jcr)
  */
 static bool job_check_maxruntime(JCR *control_jcr, JCR *jcr)
 {
-   bool cancel = false;
-
-   if (jcr->job->MaxRunTime == 0) {
+   if (jcr->job->MaxRunTime == 0 || job_canceled(jcr)) {
       return false;
    }
    if ((watchdog_time - jcr->start_time) < jcr->job->MaxRunTime) {
@@ -563,6 +574,7 @@ static bool job_check_maxruntime(JCR *control_jcr, JCR *jcr)
       return false;
    }
 
+#ifdef xxx
    switch (jcr->JobStatus) {
    case JS_Created:
    case JS_Running:
@@ -590,9 +602,9 @@ static bool job_check_maxruntime(JCR *control_jcr, JCR *jcr)
    }
 
    Dmsg3(200, "MaxRunTime result: %scancel JCR %p (%s)\n",
-         cancel ? "" : "do not ", jcr, jcr->job);
-
-   return cancel;
+         cancel ? "" : "do not ", jcr, jcr->Job);
+#endif
+   return true;
 }
 
 /*
@@ -838,27 +850,6 @@ void dird_free_jcr_pointers(JCR *jcr)
       free_pool_memory(jcr->fname);
       jcr->fname = NULL;
    }
-   if (jcr->pool_source) {
-      free_pool_memory(jcr->pool_source);
-      jcr->pool_source = NULL;
-   }
-   if (jcr->rpool_source) {
-      free_pool_memory(jcr->rpool_source);
-      jcr->rpool_source = NULL;
-   }
-   if (jcr->wstore_source) {
-      free_pool_memory(jcr->wstore_source);
-      jcr->wstore_source = NULL;
-   }
-   if (jcr->rstore_source) {
-      free_pool_memory(jcr->rstore_source);
-      jcr->rstore_source = NULL;
-   }
-   if (jcr->stime) {
-      Dmsg0(200, "Free JCR stime\n");
-      free_pool_memory(jcr->stime);
-      jcr->stime = NULL;
-   }
    if (jcr->RestoreBootstrap) {
       free(jcr->RestoreBootstrap);
       jcr->RestoreBootstrap = NULL;
@@ -891,6 +882,40 @@ void dird_free_jcr(JCR *jcr)
       pthread_cond_destroy(&jcr->term_wait);
       jcr->term_wait_inited = false;
    }
+   if (jcr->db) {
+      db_close_database(jcr, jcr->db);
+      jcr->db = NULL;
+   }
+   if (jcr->db_batch) {
+      db_close_database(jcr, jcr->db_batch);
+      jcr->db_batch = NULL;
+   }
+   if (jcr->stime) {
+      Dmsg0(200, "Free JCR stime\n");
+      free_pool_memory(jcr->stime);
+      jcr->stime = NULL;
+   }
+   if (jcr->fname) {
+      Dmsg0(200, "Free JCR fname\n");
+      free_pool_memory(jcr->fname);
+      jcr->fname = NULL;
+   }
+   if (jcr->pool_source) {
+      free_pool_memory(jcr->pool_source);
+      jcr->pool_source = NULL;
+   }
+   if (jcr->rpool_source) {
+      free_pool_memory(jcr->rpool_source);
+      jcr->rpool_source = NULL;
+   }
+   if (jcr->wstore_source) {
+      free_pool_memory(jcr->wstore_source);
+      jcr->wstore_source = NULL;
+   }
+   if (jcr->rstore_source) {
+      free_pool_memory(jcr->rstore_source);
+      jcr->rstore_source = NULL;
+   }
 
    /* Delete lists setup to hold storage pointers */
    free_rwstorage(jcr);
@@ -941,6 +966,12 @@ void set_jcr_defaults(JCR *jcr, JOB *job)
    case JT_RESTORE:
       jcr->JobLevel = L_NONE;
       break;
+   case JT_MIGRATE:
+      if (!jcr->rpool_source) {
+         jcr->rpool_source = get_pool_memory(PM_MESSAGE);
+         pm_strcpy(jcr->rpool_source, _("unknown source"));
+      }
+      /* Fall-through wanted */
    default:
       jcr->JobLevel = job->JobLevel;
       break;
@@ -952,6 +983,7 @@ void set_jcr_defaults(JCR *jcr, JOB *job)
       jcr->pool_source = get_pool_memory(PM_MESSAGE);
       pm_strcpy(jcr->pool_source, _("unknown source"));
    }
+
    jcr->JobPriority = job->Priority;
    /* Copy storage definitions -- deleted in dir_free_jcr above */
    if (job->storage) {