]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/job.c
SQLite performance + misc -- see kes20Jul02
[bacula/bacula] / bacula / src / dird / job.c
1 /*
2  *
3  *   Bacula Director Job processing routines
4  *
5  *     Kern Sibbald, October MM
6  *
7  *    Version $Id$
8  */
9 /*
10    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "dird.h"
31
32 /* Forward referenced subroutines */
33 static void job_thread(void *arg);
34 static char *edit_run_codes(JCR *jcr, char *omsg, char *imsg);
35
36 /* Exported subroutines */
37 void run_job(JCR *jcr);
38 void init_job_server(int max_workers);
39
40
41 /* Imported subroutines */
42 extern void term_scheduler();
43 extern void term_ua_server();
44 extern int do_backup(JCR *jcr);
45 extern int do_restore(JCR *jcr);
46 extern int do_verify(JCR *jcr);
47 extern void backup_cleanup(void);
48 extern void start_UA_server(int port);
49
50 /* Queue of jobs to be run */
51 static workq_t job_wq;                /* our job work queue */
52
53
54 void init_job_server(int max_workers)
55 {
56    int stat;
57
58    if ((stat = workq_init(&job_wq, max_workers, job_thread)) != 0) {
59       Emsg1(M_ABORT, 0, _("Could not init job work queue: ERR=%s\n"), strerror(stat));
60    }
61    return;
62 }
63
64 /*
65  * Run a job -- typically called by the scheduler, but may also
66  *              be called by the UA (Console program).
67  *
68  */
69 void run_job(JCR *jcr)
70 {
71    int stat, errstat;
72
73    init_msg(jcr, jcr->msgs);
74    create_unique_job_name(jcr, jcr->job->hdr.name);
75    jcr->jr.SchedTime = jcr->sched_time;
76    jcr->jr.StartTime = jcr->start_time;
77    jcr->jr.Type = jcr->JobType;
78    jcr->jr.Level = jcr->JobLevel;
79    jcr->jr.JobStatus = jcr->JobStatus;
80    strcpy(jcr->jr.Name, jcr->job->hdr.name);
81    strcpy(jcr->jr.Job, jcr->Job);
82
83    /* Initialize termination condition variable */
84    if ((errstat = pthread_cond_init(&jcr->term_wait, NULL)) != 0) {
85       Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
86       jcr->JobStatus = JS_ErrorTerminated;
87       free_jcr(jcr);
88       return;
89    }
90
91    /*
92     * Open database
93     */
94    Dmsg0(50, "Open database\n");
95    jcr->db=db_init_database(jcr->catalog->db_name, jcr->catalog->db_user,
96                             jcr->catalog->db_password);
97    if (!db_open_database(jcr->db)) {
98       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
99       db_close_database(jcr->db);
100       jcr->JobStatus = JS_ErrorTerminated;
101       free_jcr(jcr);
102       return;
103    }
104    Dmsg0(50, "DB opened\n");
105
106    /*
107     * Create Job record  
108     */
109    jcr->jr.JobStatus = jcr->JobStatus;
110    if (!db_create_job_record(jcr->db, &jcr->jr)) {
111       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
112       db_close_database(jcr->db);
113       jcr->JobStatus = JS_ErrorTerminated;
114       free_jcr(jcr);
115       return;
116    }
117    jcr->JobId = jcr->jr.JobId;
118    ASSERT(jcr->jr.JobId > 0);
119
120    Dmsg4(30, "Created job record JobId=%d Name=%s Type=%c Level=%c\n", 
121        jcr->JobId, jcr->Job, jcr->jr.Type, jcr->jr.Level);
122    Dmsg0(200, "Add jrc to work queue\n");
123
124
125    /* Queue the job to be run */
126    if ((stat = workq_add(&job_wq, (void *)jcr)) != 0) {
127       Emsg1(M_ABORT, 0, _("Could not add job to work queue: ERR=%s\n"), strerror(stat));
128    }
129    Dmsg0(200, "Done run_job()\n");
130 }
131
132 /* 
133  * This is the engine called by workq_add() when we were pulled                
134  *  from the work queue.
135  *  At this point, we are running in our own thread 
136  */
137 static void job_thread(void *arg)
138 {
139    time_t now;
140    JCR *jcr = (JCR *)arg;
141
142    time(&now);
143
144    Dmsg0(100, "=====Start Job=========\n");
145    jcr->start_time = now;             /* set the real start time */
146    if (jcr->job->MaxStartDelay != 0 && jcr->job->MaxStartDelay <
147        (btime_t)(jcr->start_time - jcr->sched_time)) {
148       Jmsg(jcr, M_FATAL, 0, _("Job cancelled because max delay time exceeded.\n"));
149       jcr->JobStatus = JS_ErrorTerminated;
150       update_job_end_record(jcr);
151    } else {
152
153       /* Run Job */
154       jcr->JobStatus = JS_Running;
155
156       if (jcr->job->RunBeforeJob) {
157          POOLMEM *before = get_pool_memory(PM_FNAME);
158          int status;
159          
160          before = edit_run_codes(jcr, before, jcr->job->RunBeforeJob);
161          status = run_program(before, 0, NULL);
162          free_pool_memory(before);
163       }
164       switch (jcr->JobType) {
165          case JT_BACKUP:
166             do_backup(jcr);
167             if (jcr->JobStatus == JS_Terminated) {
168                do_autoprune(jcr);
169             }
170             break;
171          case JT_VERIFY:
172             do_verify(jcr);
173             if (jcr->JobStatus == JS_Terminated) {
174                do_autoprune(jcr);
175             }
176             break;
177          case JT_RESTORE:
178             do_restore(jcr);
179             if (jcr->JobStatus == JS_Terminated) {
180                do_autoprune(jcr);
181             }
182             break;
183          case JT_ADMIN:
184             /* No actual job */
185             do_autoprune(jcr);
186             break;
187          default:
188             Dmsg1(0, "Unimplemented job type: %d\n", jcr->JobType);
189             break;
190          }
191    }
192    if (jcr->job->RunAfterJob) {
193       POOLMEM *after = get_pool_memory(PM_FNAME);
194       int status;
195       
196       after = edit_run_codes(jcr, after, jcr->job->RunAfterJob);
197       status = run_program(after, 0, NULL);
198       free_pool_memory(after);
199    }
200    Dmsg0(50, "Before free jcr\n");
201    free_jcr(jcr);
202    Dmsg0(50, "======== End Job ==========\n");
203 }
204
205 /*
206  * Get or create a Client record for this Job
207  */
208 int get_or_create_client_record(JCR *jcr)
209 {
210    CLIENT_DBR cr;
211
212    memset(&cr, 0, sizeof(cr));
213    strcpy(cr.Name, jcr->client->hdr.name);
214    cr.AutoPrune = jcr->client->AutoPrune;
215    cr.FileRetention = jcr->client->FileRetention;
216    cr.JobRetention = jcr->client->JobRetention;
217    if (jcr->client_name) {
218       free_pool_memory(jcr->client_name);
219    }
220    jcr->client_name = get_memory(strlen(jcr->client->hdr.name) + 1);
221    strcpy(jcr->client_name, jcr->client->hdr.name);
222    if (!db_create_client_record(jcr->db, &cr)) {
223       Jmsg(jcr, M_FATAL, 0, _("Could not create Client record. %s"), 
224          db_strerror(jcr->db));
225       return 0;
226    }
227    jcr->jr.ClientId = cr.ClientId;
228    Dmsg2(9, "Created Client %s record %d\n", jcr->client->hdr.name, 
229       jcr->jr.ClientId);
230    return 1;
231 }
232
233
234 /*
235  * Write status and such in DB
236  */
237 void update_job_end_record(JCR *jcr)
238 {
239    if (jcr->jr.EndTime == 0) {
240       jcr->jr.EndTime = time(NULL);
241    }
242    jcr->end_time = jcr->jr.EndTime;
243    jcr->jr.JobId = jcr->JobId;
244    jcr->jr.JobStatus = jcr->JobStatus;
245    jcr->jr.JobFiles = jcr->JobFiles;
246    jcr->jr.JobBytes = jcr->JobBytes;
247    jcr->jr.VolSessionId = jcr->VolSessionId;
248    jcr->jr.VolSessionTime = jcr->VolSessionTime;
249    if (!db_update_job_end_record(jcr->db, &jcr->jr)) {
250       Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"), 
251          db_strerror(jcr->db));
252    }
253 }
254
255 /*
256  * Takes base_name and appends (unique) current
257  *   date and time to form unique job name.
258  *
259  *  Returns: unique job name in jcr->Job
260  *    date/time in jcr->start_time
261  */
262 void create_unique_job_name(JCR *jcr, char *base_name)
263 {
264    /* Job start mutex */
265    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
266    static time_t last_start_time = 0;
267    time_t now;
268    struct tm tm;
269    char dt[MAX_TIME_LENGTH];
270    char name[MAX_NAME_LENGTH];
271    char *p;
272
273    /* Guarantee unique start time -- maximum one per second, and
274     * thus unique Job Name 
275     */
276    P(mutex);                          /* lock creation of jobs */
277    time(&now);
278    while (now == last_start_time) {
279       sleep(1);
280       time(&now);
281    }
282    last_start_time = now;
283    V(mutex);                          /* allow creation of jobs */
284    jcr->start_time = now;
285    /* Form Unique JobName */
286    localtime_r(&now, &tm);
287    strftime(dt, sizeof(dt), "%Y-%m-%d.%H:%M:%S", &tm); 
288    strncpy(name, base_name, sizeof(name));
289    name[sizeof(name)-22] = 0;          /* truncate if too long */
290    sprintf(jcr->Job, "%s.%s", name, dt); /* add date & time */
291    /* Convert spaces into underscores */
292    for (p=jcr->Job; *p; p++) {
293       if (*p == ' ') {
294          *p = '_';
295       }
296    }
297 }
298
299 /*
300  * Free the Job Control Record if no one is still using it.
301  *  Called from main free_jcr() routine in src/lib/jcr.c so
302  *  that we can do our Director specific cleanup of the jcr.
303  */
304 void dird_free_jcr(JCR *jcr)
305 {
306    Dmsg0(200, "Start dird free_jcr\n");
307
308    if (jcr->file_bsock) {
309       Dmsg0(200, "Close File bsock\n");
310       bnet_close(jcr->file_bsock);
311    }
312    if (jcr->store_bsock) {
313       Dmsg0(200, "Close Store bsock\n");
314       bnet_close(jcr->store_bsock);
315    }
316    if (jcr->fname) {  
317       Dmsg0(200, "Free JCR fname\n");
318       free_pool_memory(jcr->fname);
319    }
320    if (jcr->stime) {
321       Dmsg0(200, "Free JCR stime\n");
322       free_pool_memory(jcr->stime);
323    }
324    if (jcr->db) {
325       Dmsg0(200, "Close DB\n");
326       db_close_database(jcr->db);
327    }
328    if (jcr->RestoreWhere) {
329       free(jcr->RestoreWhere);
330    }
331    if (jcr->RestoreBootstrap) {
332       free(jcr->RestoreBootstrap);
333    }
334    Dmsg0(200, "End dird free_jcr\n");
335 }
336
337 /*
338  * Set some defaults in the JCR necessary to
339  * run. These items are pulled from the job
340  * definition as defaults, but can be overridden
341  * later either by the Run record in the Schedule resource,
342  * or by the Console program.
343  */
344 void set_jcr_defaults(JCR *jcr, JOB *job)
345 {
346    jcr->job = job;
347    jcr->JobType = job->JobType;
348    jcr->JobLevel = job->level;
349    jcr->store = job->storage;
350    jcr->client = job->client;
351    if (jcr->client_name) {
352       free_pool_memory(jcr->client_name);
353    }
354    jcr->client_name = get_memory(strlen(jcr->client->hdr.name) + 1);
355    strcpy(jcr->client_name, jcr->client->hdr.name);
356    jcr->pool = job->pool;
357    jcr->catalog = job->client->catalog;
358    jcr->fileset = job->fs;
359    jcr->msgs = job->messages; 
360    if (jcr->RestoreBootstrap) {
361       free(jcr->RestoreBootstrap);
362    }
363    /* This can be overridden by Console program */
364    if (job->RestoreBootstrap) {
365       jcr->RestoreBootstrap = bstrdup(job->RestoreBootstrap);
366    }
367    /* If no default level given, set one */
368    if (jcr->JobLevel == 0) {
369       switch (jcr->JobType) {
370       case JT_VERIFY:
371          jcr->JobLevel = L_VERIFY_CATALOG;
372          break;
373       case JT_BACKUP:
374          jcr->JobLevel = L_INCREMENTAL;
375          break;
376       default:
377          break;
378       }
379    }
380 }
381
382 /*
383  * Edit codes into Run command
384  *  %% = %
385  *  %c = Client's name
386  *  %d = Director's name
387  *  %i = JobId
388  *  %e = Job Exit
389  *  %j = Job
390  *  %l = Job Level
391  *  %n = Job name
392  *  %t = Job type
393  *
394  *  omsg = edited output message
395  *  imsg = input string containing edit codes (%x)
396  *
397  */
398 static char *edit_run_codes(JCR *jcr, char *omsg, char *imsg) 
399 {
400    char *p, *o;
401    const char *str;
402    char add[20];
403
404    Dmsg1(200, "edit_run_codes: %s\n", imsg);
405    add[2] = 0;
406    o = omsg;
407    for (p=imsg; *p; p++) {
408       if (*p == '%') {
409          switch (*++p) {
410          case '%':
411             add[0] = '%';
412             add[1] = 0;
413             str = add;
414             break;
415          case 'c':
416             str = jcr->client_name;
417             if (!str) {
418                str = "";
419             }
420             break;
421          case 'd':
422             str = my_name;
423             break;
424          case 'e':
425             str = job_status_to_str(jcr->JobStatus);
426             break;
427          case 'i':
428             sprintf(add, "%d", jcr->JobId);
429             str = add;
430             break;
431          case 'j':                    /* Job */
432             str = jcr->Job;
433             break;
434          case 'l':
435             str = job_level_to_str(jcr->JobLevel);
436             break;
437          case 'n':
438             str = jcr->job->hdr.name;
439             break;
440          case 't':
441             str = job_type_to_str(jcr->JobType);
442             break;
443          default:
444             add[0] = '%';
445             add[1] = *p;
446             str = add;
447             break;
448          }
449       } else {
450          add[0] = *p;
451          add[1] = 0;
452          str = add;
453       }
454       Dmsg1(200, "add_str %s\n", str);
455       add_str_to_pool_mem(&omsg, &o, (char *)str);
456       *o = 0;
457       Dmsg1(200, "omsg=%s\n", omsg);
458    }
459    *o = 0;
460    return omsg;
461 }