]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/jobq.c
- Convert more atoi to str_to_int64() for DB.
[bacula/bacula] / bacula / src / dird / jobq.c
1 /*
2  * Bacula job queue routines.
3  *
4  *  This code consists of three queues, the waiting_jobs
5  *  queue, where jobs are initially queued, the ready_jobs
6  *  queue, where jobs are placed when all the resources are
7  *  allocated and they can immediately be run, and the
8  *  running queue where jobs are placed when they are
9  *  running.
10  *
11  *  Kern Sibbald, July MMIII
12  *
13  *   Version $Id$
14  *
15  *  This code was adapted from the Bacula workq, which was
16  *    adapted from "Programming with POSIX Threads", by
17  *    David R. Butenhof
18  *
19  */
20 /*
21    Copyright (C) 2003-2005 Kern Sibbald
22
23    This program is free software; you can redistribute it and/or
24    modify it under the terms of the GNU General Public License as
25    published by the Free Software Foundation; either version 2 of
26    the License, or (at your option) any later version.
27
28    This program is distributed in the hope that it will be useful,
29    but WITHOUT ANY WARRANTY; without even the implied warranty of
30    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31    General Public License for more details.
32
33    You should have received a copy of the GNU General Public
34    License along with this program; if not, write to the Free
35    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
36    MA 02111-1307, USA.
37
38  */
39
40 #include "bacula.h"
41 #include "dird.h"
42
43 extern JCR *jobs;
44
45 /* Forward referenced functions */
46 extern "C" void *jobq_server(void *arg);
47 extern "C" void *sched_wait(void *arg);
48
49 static int  start_server(jobq_t *jq);
50 static bool acquire_resources(JCR *jcr);
51
52
53
54 /*
55  * Initialize a job queue
56  *
57  *  Returns: 0 on success
58  *           errno on failure
59  */
60 int jobq_init(jobq_t *jq, int threads, void *(*engine)(void *arg))
61 {
62    int stat;
63    jobq_item_t *item = NULL;
64
65    if ((stat = pthread_attr_init(&jq->attr)) != 0) {
66       berrno be;
67       Jmsg1(NULL, M_ERROR, 0, "pthread_attr_init: ERR=%s\n", be.strerror(stat));
68       return stat;
69    }
70    if ((stat = pthread_attr_setdetachstate(&jq->attr, PTHREAD_CREATE_DETACHED)) != 0) {
71       pthread_attr_destroy(&jq->attr);
72       return stat;
73    }
74    if ((stat = pthread_mutex_init(&jq->mutex, NULL)) != 0) {
75       berrno be;
76       Jmsg1(NULL, M_ERROR, 0, "pthread_mutex_init: ERR=%s\n", be.strerror(stat));
77       pthread_attr_destroy(&jq->attr);
78       return stat;
79    }
80    if ((stat = pthread_cond_init(&jq->work, NULL)) != 0) {
81       berrno be;
82       Jmsg1(NULL, M_ERROR, 0, "pthread_cond_init: ERR=%s\n", be.strerror(stat));
83       pthread_mutex_destroy(&jq->mutex);
84       pthread_attr_destroy(&jq->attr);
85       return stat;
86    }
87    jq->quit = false;
88    jq->max_workers = threads;         /* max threads to create */
89    jq->num_workers = 0;               /* no threads yet */
90    jq->idle_workers = 0;              /* no idle threads */
91    jq->engine = engine;               /* routine to run */
92    jq->valid = JOBQ_VALID;
93    /* Initialize the job queues */
94    jq->waiting_jobs = New(dlist(item, &item->link));
95    jq->running_jobs = New(dlist(item, &item->link));
96    jq->ready_jobs = New(dlist(item, &item->link));
97    return 0;
98 }
99
100 /*
101  * Destroy the job queue
102  *
103  * Returns: 0 on success
104  *          errno on failure
105  */
106 int jobq_destroy(jobq_t *jq)
107 {
108    int stat, stat1, stat2;
109
110    if (jq->valid != JOBQ_VALID) {
111       return EINVAL;
112    }
113    if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
114       berrno be;
115       Jmsg1(NULL, M_ERROR, 0, "pthread_mutex_lock: ERR=%s\n", be.strerror(stat));
116       return stat;
117    }
118    jq->valid = 0;                      /* prevent any more operations */
119
120    /* 
121     * If any threads are active, wake them 
122     */
123    if (jq->num_workers > 0) {
124       jq->quit = true;
125       if (jq->idle_workers) {
126          if ((stat = pthread_cond_broadcast(&jq->work)) != 0) {
127             berrno be;
128             Jmsg1(NULL, M_ERROR, 0, "pthread_cond_broadcast: ERR=%s\n", be.strerror(stat));
129             pthread_mutex_unlock(&jq->mutex);
130             return stat;
131          }
132       }
133       while (jq->num_workers > 0) {
134          if ((stat = pthread_cond_wait(&jq->work, &jq->mutex)) != 0) {
135             berrno be;
136             Jmsg1(NULL, M_ERROR, 0, "pthread_cond_wait: ERR=%s\n", be.strerror(stat));
137             pthread_mutex_unlock(&jq->mutex);
138             return stat;
139          }
140       }
141    }
142    if ((stat = pthread_mutex_unlock(&jq->mutex)) != 0) {
143       berrno be;
144       Jmsg1(NULL, M_ERROR, 0, "pthread_mutex_unlock: ERR=%s\n", be.strerror(stat));
145       return stat;
146    }
147    stat  = pthread_mutex_destroy(&jq->mutex);
148    stat1 = pthread_cond_destroy(&jq->work);
149    stat2 = pthread_attr_destroy(&jq->attr);
150    delete jq->waiting_jobs;
151    delete jq->running_jobs;
152    delete jq->ready_jobs;
153    return (stat != 0 ? stat : (stat1 != 0 ? stat1 : stat2));
154 }
155
156 struct wait_pkt {
157    JCR *jcr;
158    jobq_t *jq;
159 };
160
161 /*
162  * Wait until schedule time arrives before starting. Normally
163  *  this routine is only used for jobs started from the console
164  *  for which the user explicitly specified a start time. Otherwise
165  *  most jobs are put into the job queue only when their
166  *  scheduled time arives.
167  */
168 extern "C"
169 void *sched_wait(void *arg)
170 {
171    JCR *jcr = ((wait_pkt *)arg)->jcr;
172    jobq_t *jq = ((wait_pkt *)arg)->jq;
173
174    Dmsg0(2300, "Enter sched_wait.\n");
175    free(arg);
176    time_t wtime = jcr->sched_time - time(NULL);
177    set_jcr_job_status(jcr, JS_WaitStartTime);
178    /* Wait until scheduled time arrives */
179    if (wtime > 0) {
180       Jmsg(jcr, M_INFO, 0, _("Job %s waiting %d seconds for scheduled start time.\n"),
181          jcr->Job, wtime);
182    }
183    /* Check every 30 seconds if canceled */
184    while (wtime > 0) {
185       Dmsg2(2300, "Waiting on sched time, jobid=%d secs=%d\n", jcr->JobId, wtime);
186       if (wtime > 30) {
187          wtime = 30;
188       }
189       bmicrosleep(wtime, 0);
190       if (job_canceled(jcr)) {
191          break;
192       }
193       wtime = jcr->sched_time - time(NULL);
194    }
195    P(jcr->mutex);                     /* lock jcr */
196    jobq_add(jq, jcr);
197    V(jcr->mutex);
198    free_jcr(jcr);                     /* we are done with jcr */
199    Dmsg0(2300, "Exit sched_wait\n");
200    return NULL;
201 }
202
203 /*
204  *  Add a job to the queue
205  *    jq is a queue that was created with jobq_init
206  *
207  *  On entry jcr->mutex must be locked.
208  *
209  */
210 int jobq_add(jobq_t *jq, JCR *jcr)
211 {
212    int stat;
213    jobq_item_t *item, *li;
214    bool inserted = false;
215    time_t wtime = jcr->sched_time - time(NULL);
216    pthread_t id;
217    wait_pkt *sched_pkt;
218
219    Dmsg3(2300, "jobq_add jobid=%d jcr=0x%x use_count=%d\n", jcr->JobId, jcr, jcr->use_count);
220    if (jq->valid != JOBQ_VALID) {
221       Jmsg0(jcr, M_ERROR, 0, "Jobq_add queue not initialized.\n");
222       return EINVAL;
223    }
224
225    jcr->use_count++;                  /* mark jcr in use by us */
226    Dmsg3(2300, "jobq_add jobid=%d jcr=0x%x use_count=%d\n", jcr->JobId, jcr, jcr->use_count);
227    if (!job_canceled(jcr) && wtime > 0) {
228       set_thread_concurrency(jq->max_workers + 2);
229       sched_pkt = (wait_pkt *)malloc(sizeof(wait_pkt));
230       sched_pkt->jcr = jcr;
231       sched_pkt->jq = jq;
232 //    jcr->use_count--;            /* release our use of jcr */
233       stat = pthread_create(&id, &jq->attr, sched_wait, (void *)sched_pkt);        
234       if (stat != 0) {                /* thread not created */
235          berrno be;
236          Jmsg1(jcr, M_ERROR, 0, "pthread_thread_create: ERR=%s\n", be.strerror(stat));
237       }
238       return stat;
239    }
240
241    if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
242       berrno be;
243       Jmsg1(jcr, M_ERROR, 0, "pthread_mutex_lock: ERR=%s\n", be.strerror(stat));
244       jcr->use_count--;               /* release jcr */
245       return stat;
246    }
247
248    if ((item = (jobq_item_t *)malloc(sizeof(jobq_item_t))) == NULL) {
249       jcr->use_count--;               /* release jcr */
250       return ENOMEM;
251    }
252    item->jcr = jcr;
253
254    if (job_canceled(jcr)) {
255       /* Add job to ready queue so that it is canceled quickly */
256       jq->ready_jobs->prepend(item);
257       Dmsg1(2300, "Prepended job=%d to ready queue\n", jcr->JobId);
258    } else {
259       /* Add this job to the wait queue in priority sorted order */
260       foreach_dlist(li, jq->waiting_jobs) {
261          Dmsg2(2300, "waiting item jobid=%d priority=%d\n",
262             li->jcr->JobId, li->jcr->JobPriority);
263          if (li->jcr->JobPriority > jcr->JobPriority) {
264             jq->waiting_jobs->insert_before(item, li);
265             Dmsg2(2300, "insert_before jobid=%d before waiting job=%d\n",
266                li->jcr->JobId, jcr->JobId);
267             inserted = true;
268             break;
269          }
270       }
271       /* If not jobs in wait queue, append it */
272       if (!inserted) {
273          jq->waiting_jobs->append(item);
274          Dmsg1(2300, "Appended item jobid=%d to waiting queue\n", jcr->JobId);
275       }
276    }
277
278    /* Ensure that at least one server looks at the queue. */
279    stat = start_server(jq);
280
281    pthread_mutex_unlock(&jq->mutex);
282    Dmsg0(2300, "Return jobq_add\n");
283    return stat;
284 }
285
286 /*
287  *  Remove a job from the job queue. Used only by cancel_job().
288  *    jq is a queue that was created with jobq_init
289  *    work_item is an element of work
290  *
291  *   Note, it is "removed" from the job queue.
292  *    If you want to cancel it, you need to provide some external means
293  *    of doing so (e.g. pthread_kill()).
294  */
295 int jobq_remove(jobq_t *jq, JCR *jcr)
296 {
297    int stat;
298    bool found = false;
299    jobq_item_t *item;
300
301    Dmsg2(2300, "jobq_remove jobid=%d jcr=0x%x\n", jcr->JobId, jcr);
302    if (jq->valid != JOBQ_VALID) {
303       return EINVAL;
304    }
305
306    if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
307       berrno be;
308       Jmsg1(NULL, M_ERROR, 0, "pthread_mutex_lock: ERR=%s\n", be.strerror(stat));
309       return stat;
310    }
311
312    foreach_dlist(item, jq->waiting_jobs) {
313       if (jcr == item->jcr) {
314          found = true;
315          break;
316       }
317    }
318    if (!found) {
319       pthread_mutex_unlock(&jq->mutex);
320       Dmsg2(2300, "jobq_remove jobid=%d jcr=0x%x not in wait queue\n", jcr->JobId, jcr);
321       return EINVAL;
322    }
323
324    /* Move item to be the first on the list */
325    jq->waiting_jobs->remove(item);
326    jq->ready_jobs->prepend(item);
327    Dmsg2(2300, "jobq_remove jobid=%d jcr=0x%x moved to ready queue\n", jcr->JobId, jcr);
328
329    stat = start_server(jq);
330
331    pthread_mutex_unlock(&jq->mutex);
332    Dmsg0(2300, "Return jobq_remove\n");
333    return stat;
334 }
335
336
337 /*
338  * Start the server thread if it isn't already running
339  */
340 static int start_server(jobq_t *jq)
341 {
342    int stat = 0;
343    pthread_t id;
344
345    /*
346     * if any threads are idle, wake one --                
347     *   actually we do a broadcast because on /lib/tls 
348     *   these signals seem to get lost from time to time.
349     */
350    if (jq->idle_workers > 0) {
351       Dmsg0(2300, "Signal worker to wake up\n");
352       if ((stat = pthread_cond_broadcast(&jq->work)) != 0) {
353          berrno be;
354          Jmsg1(NULL, M_ERROR, 0, "pthread_cond_signal: ERR=%s\n", be.strerror(stat));
355          return stat;
356       }
357    } else if (jq->num_workers < jq->max_workers) {
358       Dmsg0(2300, "Create worker thread\n");
359       /* No idle threads so create a new one */
360       set_thread_concurrency(jq->max_workers + 1);
361       if ((stat = pthread_create(&id, &jq->attr, jobq_server, (void *)jq)) != 0) {
362          berrno be;
363          Jmsg1(NULL, M_ERROR, 0, "pthread_create: ERR=%s\n", be.strerror(stat));
364          return stat;
365       }
366    }
367    return stat;
368 }
369
370
371 /*
372  * This is the worker thread that serves the job queue.
373  * When all the resources are acquired for the job,
374  *  it will call the user's engine.
375  */
376 extern "C"
377 void *jobq_server(void *arg)
378 {
379    struct timespec timeout;
380    jobq_t *jq = (jobq_t *)arg;
381    jobq_item_t *je;                   /* job entry in queue */
382    int stat;
383    bool timedout = false;
384    bool work = true;
385
386    Dmsg0(2300, "Start jobq_server\n");
387    if ((stat = pthread_mutex_lock(&jq->mutex)) != 0) {
388       berrno be;
389       Jmsg1(NULL, M_ERROR, 0, "pthread_mutex_lock: ERR=%s\n", be.strerror(stat));
390       return NULL;
391    }
392    jq->num_workers++;
393
394    for (;;) {
395       struct timeval tv;
396       struct timezone tz;
397
398       Dmsg0(2300, "Top of for loop\n");
399       if (!work && !jq->quit) {
400          gettimeofday(&tv, &tz);
401          timeout.tv_nsec = 0;
402          timeout.tv_sec = tv.tv_sec + 4;
403
404          while (!jq->quit) {
405             /*
406              * Wait 4 seconds, then if no more work, exit
407              */
408             Dmsg0(2300, "pthread_cond_timedwait()\n");
409             stat = pthread_cond_timedwait(&jq->work, &jq->mutex, &timeout);
410             if (stat == ETIMEDOUT) {
411                Dmsg0(2300, "timedwait timedout.\n");
412                timedout = true;
413                break;
414             } else if (stat != 0) {
415                /* This shouldn't happen */
416                Dmsg0(2300, "This shouldn't happen\n");
417                jq->num_workers--;
418                pthread_mutex_unlock(&jq->mutex);
419                return NULL;
420             }
421             break;
422          }
423       }
424       /*
425        * If anything is in the ready queue, run it
426        */
427       Dmsg0(2300, "Checking ready queue.\n");
428       while (!jq->ready_jobs->empty() && !jq->quit) {
429          JCR *jcr;
430          je = (jobq_item_t *)jq->ready_jobs->first();
431          jcr = je->jcr;
432          jq->ready_jobs->remove(je);
433          if (!jq->ready_jobs->empty()) {
434             Dmsg0(2300, "ready queue not empty start server\n");
435             if (start_server(jq) != 0) {
436                jq->num_workers--;
437                pthread_mutex_unlock(&jq->mutex);
438                return NULL;
439             }
440          }
441          jq->running_jobs->append(je);
442          Dmsg1(2300, "Took jobid=%d from ready and appended to run\n", jcr->JobId);
443
444          /* Release job queue lock */
445          V(jq->mutex);
446
447          /* Call user's routine here */
448          Dmsg1(2300, "Calling user engine for jobid=%d\n", jcr->JobId);
449          jq->engine(je->jcr);
450
451          Dmsg1(2300, "Back from user engine jobid=%d.\n", jcr->JobId);
452
453          /* Reacquire job queue lock */
454          P(jq->mutex);
455          Dmsg0(200, "Done lock mutex after running job. Release locks.\n");
456          jq->running_jobs->remove(je);
457          /*
458           * Release locks if acquired. Note, they will not have
459           *  been acquired for jobs canceled before they were
460           *  put into the ready queue.
461           */
462          if (jcr->acquired_resource_locks) {
463             jcr->store->NumConcurrentJobs--;
464             jcr->client->NumConcurrentJobs--;
465             jcr->job->NumConcurrentJobs--;
466          }
467
468          /*
469           * Reschedule the job if necessary and requested
470           */
471          if (jcr->job->RescheduleOnError &&
472              jcr->JobStatus != JS_Terminated &&
473              jcr->JobStatus != JS_Canceled &&
474              jcr->job->RescheduleTimes > 0 &&
475              jcr->reschedule_count < jcr->job->RescheduleTimes) {
476              char dt[50];
477
478              /*
479               * Reschedule this job by cleaning it up, but
480               *  reuse the same JobId if possible.
481               */
482             jcr->reschedule_count++;
483             jcr->sched_time = time(NULL) + jcr->job->RescheduleInterval;
484             Dmsg2(2300, "Rescheduled Job %s to re-run in %d seconds.\n", jcr->Job,
485                (int)jcr->job->RescheduleInterval);
486             bstrftime(dt, sizeof(dt), time(NULL));
487             Jmsg(jcr, M_INFO, 0, _("Rescheduled Job %s at %s to re-run in %d seconds.\n"),
488                jcr->Job, dt, (int)jcr->job->RescheduleInterval);
489             dird_free_jcr_pointers(jcr);     /* partial cleanup old stuff */
490             jcr->JobStatus = JS_WaitStartTime;
491             jcr->SDJobStatus = 0;
492             if (jcr->JobBytes == 0) {
493                Dmsg1(2300, "Requeue job=%d\n", jcr->JobId);
494                jcr->JobStatus = JS_WaitStartTime;
495                V(jq->mutex);
496                jobq_add(jq, jcr);     /* queue the job to run again */
497                P(jq->mutex);
498                free(je);              /* free the job entry */
499                continue;              /* look for another job to run */
500             }
501             /*
502              * Something was actually backed up, so we cannot reuse
503              *   the old JobId or there will be database record
504              *   conflicts.  We now create a new job, copying the
505              *   appropriate fields.
506              */
507             JCR *njcr = new_jcr(sizeof(JCR), dird_free_jcr);
508             set_jcr_defaults(njcr, jcr->job);
509             njcr->reschedule_count = jcr->reschedule_count;
510             njcr->JobLevel = jcr->JobLevel;
511             njcr->JobStatus = jcr->JobStatus;
512             copy_storage(njcr, jcr);
513             njcr->messages = jcr->messages;
514             Dmsg0(2300, "Call to run new job\n");
515             V(jq->mutex);
516             run_job(njcr);            /* This creates a "new" job */
517             free_jcr(njcr);           /* release "new" jcr */
518             P(jq->mutex);
519             Dmsg0(2300, "Back from running new job.\n");
520          }
521          /* Clean up and release old jcr */
522          if (jcr->db) {
523             db_close_database(jcr, jcr->db);
524             jcr->db = NULL;
525          }
526          Dmsg2(2300, "====== Termination job=%d use_cnt=%d\n", jcr->JobId, jcr->use_count);
527          jcr->SDJobStatus = 0;
528          V(jq->mutex);                /* release internal lock */
529          free_jcr(jcr);
530          free(je);                    /* release job entry */
531          P(jq->mutex);                /* reacquire job queue lock */
532       }
533       /*
534        * If any job in the wait queue can be run,
535        *  move it to the ready queue
536        */
537       Dmsg0(2300, "Done check ready, now check wait queue.\n");
538       if (!jq->waiting_jobs->empty() && !jq->quit) {
539          int Priority;
540          je = (jobq_item_t *)jq->waiting_jobs->first();
541          jobq_item_t *re = (jobq_item_t *)jq->running_jobs->first();
542          if (re) {
543             Priority = re->jcr->JobPriority;
544             Dmsg2(2300, "JobId %d is running. Look for pri=%d\n", re->jcr->JobId, Priority);
545          } else {
546             Priority = je->jcr->JobPriority;
547             Dmsg1(2300, "No job running. Look for Job pri=%d\n", Priority);
548          }
549          /*
550           * Walk down the list of waiting jobs and attempt
551           *   to acquire the resources it needs.
552           */
553          for ( ; je;  ) {
554             /* je is current job item on the queue, jn is the next one */
555             JCR *jcr = je->jcr;
556             jobq_item_t *jn = (jobq_item_t *)jq->waiting_jobs->next(je);
557
558             Dmsg3(2300, "Examining Job=%d JobPri=%d want Pri=%d\n",
559                jcr->JobId, jcr->JobPriority, Priority);
560
561             /* Take only jobs of correct Priority */
562             if (jcr->JobPriority != Priority) {
563                set_jcr_job_status(jcr, JS_WaitPriority);
564                break;
565             }
566
567             if (!acquire_resources(jcr)) {
568                je = jn;            /* point to next waiting job */
569                continue;
570             }
571
572             /* Got all locks, now remove it from wait queue and append it
573              *   to the ready queue
574              */
575             jcr->acquired_resource_locks = true;
576             jq->waiting_jobs->remove(je);
577             jq->ready_jobs->append(je);
578             Dmsg1(2300, "moved JobId=%d from wait to ready queue\n", je->jcr->JobId);
579             je = jn;                  /* Point to next waiting job */
580          } /* end for loop */
581
582       } /* end if */
583
584       Dmsg0(2300, "Done checking wait queue.\n");
585       /*
586        * If no more ready work and we are asked to quit, then do it
587        */
588       if (jq->ready_jobs->empty() && jq->quit) {
589          jq->num_workers--;
590          if (jq->num_workers == 0) {
591             Dmsg0(2300, "Wake up destroy routine\n");
592             /* Wake up destroy routine if he is waiting */
593             pthread_cond_broadcast(&jq->work);
594          }
595          break;
596       }
597       Dmsg0(2300, "Check for work request\n");
598       /*
599        * If no more work requests, and we waited long enough, quit
600        */
601       Dmsg2(2300, "timedout=%d read empty=%d\n", timedout,
602          jq->ready_jobs->empty());
603       if (jq->ready_jobs->empty() && timedout) {
604          Dmsg0(2300, "break big loop\n");
605          jq->num_workers--;
606          break;
607       }
608
609       work = !jq->ready_jobs->empty() || !jq->waiting_jobs->empty();
610       if (work) {
611          /*
612           * If a job is waiting on a Resource, don't consume all
613           *   the CPU time looping looking for work, and even more
614           *   important, release the lock so that a job that has
615           *   terminated can give us the resource.
616           */
617          V(jq->mutex);
618          bmicrosleep(2, 0);              /* pause for 2 seconds */
619          P(jq->mutex);
620          /* Recompute work as something may have changed in last 2 secs */
621          work = !jq->ready_jobs->empty() || !jq->waiting_jobs->empty();
622       }
623       Dmsg1(2300, "Loop again. work=%d\n", work);
624    } /* end of big for loop */
625
626    Dmsg0(200, "unlock mutex\n");
627    V(jq->mutex);
628    Dmsg0(2300, "End jobq_server\n");
629    return NULL;
630 }
631
632 /*
633  * See if we can acquire all the necessary resources for the job (JCR)
634  *
635  *  Returns: true  if successful
636  *           false if resource failure
637  */
638 static bool acquire_resources(JCR *jcr)
639 {
640    bool skip_this_jcr = false;
641
642    if (jcr->JobType == JT_RESTORE || jcr->JobType == JT_VERIFY) {
643       /* 
644        * Let only one Restore/verify job run at a time regardless
645        *   of MaxConcurrentJobs.
646        */ 
647       if (jcr->store->NumConcurrentJobs == 0) {
648          jcr->store->NumConcurrentJobs = 1;
649       } else {
650          set_jcr_job_status(jcr, JS_WaitStoreRes);
651          return false;
652       }
653    /* We are not doing a Restore or Verify */
654    } else if (jcr->store->NumConcurrentJobs == 0 &&
655               jcr->store->NumConcurrentJobs < jcr->store->MaxConcurrentJobs) {
656        /* Simple case, first job */
657        jcr->store->NumConcurrentJobs = 1;
658    } else if (jcr->store->NumConcurrentJobs < jcr->store->MaxConcurrentJobs) {
659        jcr->store->NumConcurrentJobs++;
660    } else {
661       skip_this_jcr = true;
662    }
663    if (skip_this_jcr) {
664       set_jcr_job_status(jcr, JS_WaitStoreRes);
665       return false;
666    }
667
668    if (jcr->client->NumConcurrentJobs < jcr->client->MaxConcurrentJobs) {
669       jcr->client->NumConcurrentJobs++;
670    } else {
671       /* Back out previous locks */
672       jcr->store->NumConcurrentJobs--;
673       set_jcr_job_status(jcr, JS_WaitClientRes);
674       return false;
675    }
676    if (jcr->job->NumConcurrentJobs < jcr->job->MaxConcurrentJobs) {
677       jcr->job->NumConcurrentJobs++;
678    } else {
679       /* Back out previous locks */
680       jcr->store->NumConcurrentJobs--;
681       jcr->client->NumConcurrentJobs--;
682       set_jcr_job_status(jcr, JS_WaitJobRes);
683       return false;
684    }
685    /* Check actual device availability */
686    /* ***FIXME****/
687    return true;
688 }