]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/job.c
0900b33d9613e949522cfc878425908fdeb4025d
[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
35 /* Exported subroutines */
36 void run_job(JCR *jcr);
37 void init_job_server(int max_workers);
38
39
40 /* Imported subroutines */
41 extern void term_scheduler();
42 extern void term_ua_server();
43 extern int do_backup(JCR *jcr);
44 extern int do_restore(JCR *jcr);
45 extern int do_verify(JCR *jcr);
46 extern void backup_cleanup(void);
47 extern void start_UA_server(int port);
48
49 /* Queue of jobs to be run */
50 static workq_t job_wq;                /* our job work queue */
51
52
53 void init_job_server(int max_workers)
54 {
55    int stat;
56
57    if ((stat = workq_init(&job_wq, max_workers, job_thread)) != 0) {
58       Emsg1(M_ABORT, 0, _("Could not init job work queue: ERR=%s\n"), strerror(stat));
59    }
60    return;
61 }
62
63 /*
64  * Run a job -- typically called by the scheduler, but may also
65  *              be called by the UA (Console program).
66  *
67  */
68 void run_job(JCR *jcr)
69 {
70    int stat, errstat;
71
72    init_msg(jcr, jcr->msgs);
73    create_unique_job_name(jcr, jcr->job->hdr.name);
74    jcr->jr.SchedTime = jcr->sched_time;
75    jcr->jr.StartTime = jcr->start_time;
76    jcr->jr.Type = jcr->JobType;
77    jcr->jr.Level = jcr->JobLevel;
78    strcpy(jcr->jr.Name, jcr->job->hdr.name);
79    strcpy(jcr->jr.Job, jcr->Job);
80
81    /* Initialize termination condition variable */
82    if ((errstat = pthread_cond_init(&jcr->term_wait, NULL)) != 0) {
83       Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
84       jcr->JobStatus = JS_ErrorTerminated;
85       free_jcr(jcr);
86       return;
87    }
88
89    /*
90     * Open database
91     */
92    Dmsg0(50, "Open database\n");
93    jcr->db=db_init_database(jcr->catalog->db_name, jcr->catalog->db_user,
94                             jcr->catalog->db_password);
95    if (!db_open_database(jcr->db)) {
96       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
97       db_close_database(jcr->db);
98       jcr->JobStatus = JS_ErrorTerminated;
99       free_jcr(jcr);
100       return;
101    }
102    Dmsg0(50, "DB opened\n");
103
104    /*
105     * Create Job record  
106     */
107    jcr->jr.JobStatus = jcr->JobStatus;
108    if (!db_create_job_record(jcr->db, &jcr->jr)) {
109       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
110       db_close_database(jcr->db);
111       jcr->JobStatus = JS_ErrorTerminated;
112       free_jcr(jcr);
113       return;
114    }
115    jcr->JobId = jcr->jr.JobId;
116    ASSERT(jcr->jr.JobId > 0);
117
118    Dmsg4(30, "Created job record JobId=%d Name=%s Type=%c Level=%c\n", 
119        jcr->JobId, jcr->Job, jcr->jr.Type, jcr->jr.Level);
120    Dmsg0(200, "Add jrc to work queue\n");
121
122
123    /* Queue the job to be run */
124    if ((stat = workq_add(&job_wq, (void *)jcr)) != 0) {
125       Emsg1(M_ABORT, 0, _("Could not add job to work queue: ERR=%s\n"), strerror(stat));
126    }
127    Dmsg0(200, "Done run_job()\n");
128 }
129
130 /* 
131  * This is the engine called by workq_add() when we were pulled                
132  *  from the work queue.
133  *  At this point, we are running in our own thread 
134  */
135 static void job_thread(void *arg)
136 {
137    time_t now;
138    JCR *jcr = (JCR *)arg;
139
140    time(&now);
141
142    Dmsg0(100, "=====Start Job=========\n");
143    jcr->start_time = now;             /* set the real start time */
144    if (jcr->job->MaxStartDelay != 0 && jcr->job->MaxStartDelay <
145        (btime_t)(jcr->start_time - jcr->sched_time)) {
146       Jmsg(jcr, M_FATAL, 0, _("Job cancelled because max delay time exceeded.\n"));
147       jcr->JobStatus = JS_ErrorTerminated;
148       update_job_end_record(jcr);
149    } else {
150
151       /* Run Job */
152       jcr->JobStatus = JS_Running;
153
154       switch (jcr->JobType) {
155          case JT_BACKUP:
156             do_backup(jcr);
157             if (jcr->JobStatus == JS_Terminated) {
158                do_autoprune(jcr);
159             }
160             break;
161          case JT_VERIFY:
162             do_verify(jcr);
163             if (jcr->JobStatus == JS_Terminated) {
164                do_autoprune(jcr);
165             }
166             break;
167          case JT_RESTORE:
168             do_restore(jcr);
169             if (jcr->JobStatus == JS_Terminated) {
170                do_autoprune(jcr);
171             }
172             break;
173          case JT_ADMIN:
174             /* No actual job */
175             do_autoprune(jcr);
176             break;
177          default:
178             Dmsg1(0, "Unimplemented job type: %d\n", jcr->JobType);
179             break;
180          }
181    }
182    Dmsg0(50, "Before free jcr\n");
183    free_jcr(jcr);
184    Dmsg0(50, "======== End Job ==========\n");
185 }
186
187 /*
188  * Get or create a Client record for this Job
189  */
190 int get_or_create_client_record(JCR *jcr)
191 {
192    CLIENT_DBR cr;
193
194    memset(&cr, 0, sizeof(cr));
195    strcpy(cr.Name, jcr->client->hdr.name);
196    cr.AutoPrune = jcr->client->AutoPrune;
197    cr.FileRetention = jcr->client->FileRetention;
198    cr.JobRetention = jcr->client->JobRetention;
199    if (jcr->client_name) {
200       free_pool_memory(jcr->client_name);
201    }
202    jcr->client_name = get_memory(strlen(jcr->client->hdr.name) + 1);
203    strcpy(jcr->client_name, jcr->client->hdr.name);
204    if (!db_create_client_record(jcr->db, &cr)) {
205       Jmsg(jcr, M_FATAL, 0, _("Could not create Client record. %s"), 
206          db_strerror(jcr->db));
207       return 0;
208    }
209    jcr->jr.ClientId = cr.ClientId;
210    Dmsg2(9, "Created Client %s record %d\n", jcr->client->hdr.name, 
211       jcr->jr.ClientId);
212    return 1;
213 }
214
215
216 /*
217  * Write status and such in DB
218  */
219 void update_job_end_record(JCR *jcr)
220 {
221    if (jcr->jr.EndTime == 0) {
222       jcr->jr.EndTime = time(NULL);
223    }
224    jcr->end_time = jcr->jr.EndTime;
225    jcr->jr.JobId = jcr->JobId;
226    jcr->jr.JobStatus = jcr->JobStatus;
227    jcr->jr.JobFiles = jcr->JobFiles;
228    jcr->jr.JobBytes = jcr->JobBytes;
229    jcr->jr.VolSessionId = jcr->VolSessionId;
230    jcr->jr.VolSessionTime = jcr->VolSessionTime;
231    if (!db_update_job_end_record(jcr->db, &jcr->jr)) {
232       Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"), 
233          db_strerror(jcr->db));
234    }
235 }
236
237 /*
238  * Takes base_name and appends (unique) current
239  *   date and time to form unique job name.
240  *
241  *  Returns: unique job name in jcr->Job
242  *    date/time in jcr->start_time
243  */
244 void create_unique_job_name(JCR *jcr, char *base_name)
245 {
246    /* Job start mutex */
247    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
248    static time_t last_start_time = 0;
249    time_t now;
250    struct tm tm;
251    char dt[MAX_TIME_LENGTH];
252    char name[MAX_NAME_LENGTH];
253    char *p;
254
255    /* Guarantee unique start time -- maximum one per second, and
256     * thus unique Job Name 
257     */
258    P(mutex);                          /* lock creation of jobs */
259    time(&now);
260    while (now == last_start_time) {
261       sleep(1);
262       time(&now);
263    }
264    last_start_time = now;
265    V(mutex);                          /* allow creation of jobs */
266    jcr->start_time = now;
267    /* Form Unique JobName */
268    localtime_r(&now, &tm);
269    strftime(dt, sizeof(dt), "%Y-%m-%d.%H:%M:%S", &tm); 
270    strncpy(name, base_name, sizeof(name));
271    name[sizeof(name)-22] = 0;          /* truncate if too long */
272    sprintf(jcr->Job, "%s.%s", name, dt); /* add date & time */
273    /* Convert spaces into underscores */
274    for (p=jcr->Job; *p; p++) {
275       if (*p == ' ') {
276          *p = '_';
277       }
278    }
279 }
280
281 /*
282  * Free the Job Control Record if no one is still using it.
283  *  Called from main free_jcr() routine in src/lib/jcr.c so
284  *  that we can do our Director specific cleanup of the jcr.
285  */
286 void dird_free_jcr(JCR *jcr)
287 {
288    Dmsg0(200, "Start dird free_jcr\n");
289
290    if (jcr->file_bsock) {
291       Dmsg0(200, "Close File bsock\n");
292       bnet_close(jcr->file_bsock);
293    }
294    if (jcr->store_bsock) {
295       Dmsg0(200, "Close Store bsock\n");
296       bnet_close(jcr->store_bsock);
297    }
298    if (jcr->fname) {  
299       Dmsg0(200, "Free JCR fname\n");
300       free_pool_memory(jcr->fname);
301    }
302    if (jcr->stime) {
303       Dmsg0(200, "Free JCR stime\n");
304       free_pool_memory(jcr->stime);
305    }
306    if (jcr->db) {
307       Dmsg0(200, "Close DB\n");
308       db_close_database(jcr->db);
309    }
310    if (jcr->RestoreWhere) {
311       free(jcr->RestoreWhere);
312    }
313    if (jcr->RestoreBootstrap) {
314       free(jcr->RestoreBootstrap);
315    }
316    Dmsg0(200, "End dird free_jcr\n");
317 }
318
319 /*
320  * Set some defaults in the JCR necessary to
321  * run. These items are pulled from the job
322  * definition as defaults, but can be overridden
323  * later either by the Run record in the Schedule resource,
324  * or by the Console program.
325  */
326 void set_jcr_defaults(JCR *jcr, JOB *job)
327 {
328    jcr->job = job;
329    jcr->JobType = job->JobType;
330    jcr->JobLevel = job->level;
331    jcr->store = job->storage;
332    jcr->client = job->client;
333    if (jcr->client_name) {
334       free_pool_memory(jcr->client_name);
335    }
336    jcr->client_name = get_memory(strlen(jcr->client->hdr.name) + 1);
337    strcpy(jcr->client_name, jcr->client->hdr.name);
338    jcr->pool = job->pool;
339    jcr->catalog = job->client->catalog;
340    jcr->fileset = job->fs;
341    jcr->msgs = job->messages; 
342    if (jcr->RestoreBootstrap) {
343       free(jcr->RestoreBootstrap);
344    }
345    /* This can be overridden by Console program */
346    if (job->RestoreBootstrap) {
347       jcr->RestoreBootstrap = bstrdup(job->RestoreBootstrap);
348    }
349    /* If no default level given, set one */
350    if (jcr->JobLevel == 0) {
351       switch (jcr->JobType) {
352       case JT_VERIFY:
353          jcr->JobLevel = L_VERIFY_CATALOG;
354          break;
355       case JT_BACKUP:
356          jcr->JobLevel = L_INCREMENTAL;
357          break;
358       default:
359          break;
360       }
361    }
362 }