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