+/*
+ * Returns true if cleanup done and we should look for more work
+ */
+static bool reschedule_job(JCR *jcr, jobq_t *jq, jobq_item_t *je)
+{
+ /*
+ * Reschedule the job if necessary and requested
+ */
+ if (jcr->job->RescheduleOnError &&
+ jcr->JobStatus != JS_Terminated &&
+ jcr->JobStatus != JS_Canceled &&
+ jcr->getJobType() == JT_BACKUP &&
+ (jcr->job->RescheduleTimes == 0 ||
+ jcr->reschedule_count < jcr->job->RescheduleTimes)) {
+ char dt[50], dt2[50];
+
+ /*
+ * Reschedule this job by cleaning it up, but
+ * reuse the same JobId if possible.
+ */
+ time_t now = time(NULL);
+ jcr->reschedule_count++;
+ jcr->sched_time = now + jcr->job->RescheduleInterval;
+ bstrftime(dt, sizeof(dt), now);
+ bstrftime(dt2, sizeof(dt2), jcr->sched_time);
+ Dmsg4(2300, "Rescheduled Job %s to re-run in %d seconds.(now=%u,then=%u)\n", jcr->Job,
+ (int)jcr->job->RescheduleInterval, now, jcr->sched_time);
+ Jmsg(jcr, M_INFO, 0, _("Rescheduled Job %s at %s to re-run in %d seconds (%s).\n"),
+ jcr->Job, dt, (int)jcr->job->RescheduleInterval, dt2);
+ dird_free_jcr_pointers(jcr); /* partial cleanup old stuff */
+ jcr->JobStatus = -1;
+ set_jcr_job_status(jcr, JS_WaitStartTime);
+ jcr->SDJobStatus = 0;
+ if (!allow_duplicate_job(jcr)) {
+ return false;
+ }
+ if (jcr->JobBytes == 0) {
+ Dmsg2(2300, "Requeue job=%d use=%d\n", jcr->JobId, jcr->use_count());
+ V(jq->mutex);
+ jobq_add(jq, jcr); /* queue the job to run again */
+ P(jq->mutex);
+ free_jcr(jcr); /* release jcr */
+ free(je); /* free the job entry */
+ return true; /* we already cleaned up */
+ }
+ /*
+ * Something was actually backed up, so we cannot reuse
+ * the old JobId or there will be database record
+ * conflicts. We now create a new job, copying the
+ * appropriate fields.
+ */
+ JCR *njcr = new_jcr(sizeof(JCR), dird_free_jcr);
+ set_jcr_defaults(njcr, jcr->job);
+ njcr->reschedule_count = jcr->reschedule_count;
+ njcr->sched_time = jcr->sched_time;
+ njcr->set_JobLevel(jcr->getJobLevel());
+ njcr->pool = jcr->pool;
+ njcr->run_pool_override = jcr->run_pool_override;
+ njcr->full_pool = jcr->full_pool;
+ njcr->run_full_pool_override = jcr->run_full_pool_override;
+ njcr->inc_pool = jcr->inc_pool;
+ njcr->run_inc_pool_override = jcr->run_inc_pool_override;
+ njcr->diff_pool = jcr->diff_pool;
+ njcr->JobStatus = -1;
+ set_jcr_job_status(njcr, jcr->JobStatus);
+ if (jcr->rstore) {
+ copy_rstorage(njcr, jcr->rstorage, _("previous Job"));
+ } else {
+ free_rstorage(njcr);
+ }
+ if (jcr->wstore) {
+ copy_wstorage(njcr, jcr->wstorage, _("previous Job"));
+ } else {
+ free_wstorage(njcr);
+ }
+ njcr->messages = jcr->messages;
+ njcr->spool_data = jcr->spool_data;
+ njcr->write_part_after_job = jcr->write_part_after_job;
+ Dmsg0(2300, "Call to run new job\n");
+ V(jq->mutex);
+ run_job(njcr); /* This creates a "new" job */
+ free_jcr(njcr); /* release "new" jcr */
+ P(jq->mutex);
+ Dmsg0(2300, "Back from running new job.\n");
+ }
+ return false;
+}
+
+/*
+ * See if we can acquire all the necessary resources for the job (JCR)
+ *
+ * Returns: true if successful
+ * false if resource failure
+ */
+static bool acquire_resources(JCR *jcr)
+{
+ bool skip_this_jcr = false;
+
+ jcr->acquired_resource_locks = false;
+/*
+ * Turning this code off is likely to cause some deadlocks,
+ * but we do not really have enough information here to
+ * know if this is really a deadlock (it may be a dual drive
+ * autochanger), and in principle, the SD reservation system
+ * should detect these deadlocks, so push the work off on it.
+ */
+#ifdef xxx
+ if (jcr->rstore && jcr->rstore == jcr->wstore) { /* possible deadlock */
+ Jmsg(jcr, M_FATAL, 0, _("Job canceled. Attempt to read and write same device.\n"
+ " Read storage \"%s\" (From %s) -- Write storage \"%s\" (From %s)\n"),
+ jcr->rstore->name(), jcr->rstore_source, jcr->wstore->name(), jcr->wstore_source);
+ set_jcr_job_status(jcr, JS_Canceled);
+ return false;
+ }
+#endif
+ if (jcr->rstore) {
+ Dmsg1(200, "Rstore=%s\n", jcr->rstore->name());
+ if (!inc_read_store(jcr)) {
+ Dmsg1(200, "Fail rncj=%d\n", jcr->rstore->NumConcurrentJobs);
+ set_jcr_job_status(jcr, JS_WaitStoreRes);
+ return false;
+ }
+ }
+
+ if (jcr->wstore) {
+ Dmsg1(200, "Wstore=%s\n", jcr->wstore->name());
+ if (jcr->wstore->NumConcurrentJobs < jcr->wstore->MaxConcurrentJobs) {
+ jcr->wstore->NumConcurrentJobs++;
+ Dmsg1(200, "Inc wncj=%d\n", jcr->wstore->NumConcurrentJobs);
+ } else if (jcr->rstore) {
+ dec_read_store(jcr);
+ skip_this_jcr = true;
+ } else {
+ Dmsg1(200, "Fail wncj=%d\n", jcr->wstore->NumConcurrentJobs);
+ skip_this_jcr = true;
+ }
+ }
+ if (skip_this_jcr) {
+ set_jcr_job_status(jcr, JS_WaitStoreRes);
+ return false;
+ }
+
+ if (jcr->client->NumConcurrentJobs < jcr->client->MaxConcurrentJobs) {
+ jcr->client->NumConcurrentJobs++;
+ } else {
+ /* Back out previous locks */
+ dec_write_store(jcr);
+ dec_read_store(jcr);
+ set_jcr_job_status(jcr, JS_WaitClientRes);
+ return false;
+ }
+ if (jcr->job->NumConcurrentJobs < jcr->job->MaxConcurrentJobs) {
+ jcr->job->NumConcurrentJobs++;
+ } else {
+ /* Back out previous locks */
+ dec_write_store(jcr);
+ dec_read_store(jcr);
+ jcr->client->NumConcurrentJobs--;
+ set_jcr_job_status(jcr, JS_WaitJobRes);
+ return false;
+ }
+
+ jcr->acquired_resource_locks = true;
+ return true;
+}
+
+static pthread_mutex_t rstore_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+/*
+ * Note: inc_read_store() and dec_read_store() are
+ * called from select_rstore() in src/dird/restore.c
+ */
+bool inc_read_store(JCR *jcr)
+{
+ P(rstore_mutex);
+ if (jcr->rstore->NumConcurrentJobs < jcr->rstore->MaxConcurrentJobs) {
+ jcr->rstore->NumConcurrentReadJobs++;
+ jcr->rstore->NumConcurrentJobs++;
+ Dmsg1(200, "Inc rncj=%d\n", jcr->rstore->NumConcurrentJobs);
+ V(rstore_mutex);
+ return true;
+ }
+ V(rstore_mutex);
+ return false;
+}
+
+void dec_read_store(JCR *jcr)
+{
+ if (jcr->rstore) {
+ P(rstore_mutex);
+ jcr->rstore->NumConcurrentReadJobs--; /* back out rstore */
+ jcr->rstore->NumConcurrentJobs--; /* back out rstore */
+ Dmsg1(200, "Dec rncj=%d\n", jcr->rstore->NumConcurrentJobs);
+ V(rstore_mutex);
+ ASSERT(jcr->rstore->NumConcurrentReadJobs >= 0);
+ ASSERT(jcr->rstore->NumConcurrentJobs >= 0);
+ }
+}
+
+static void dec_write_store(JCR *jcr)
+{
+ if (jcr->wstore) {
+ jcr->wstore->NumConcurrentJobs--;
+ Dmsg1(200, "Dec wncj=%d\n", jcr->wstore->NumConcurrentJobs);
+ ASSERT(jcr->wstore->NumConcurrentJobs >= 0);
+ }
+}