]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/job.c
9200ccaddde5860e3568f76d13b97c1ae1b5aa27
[bacula/bacula] / bacula / src / dird / job.c
1 /*
2  *
3  *   Bacula Director Job processing routines
4  *
5  *     Kern Sibbald, October MM
6  */
7 /*
8    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of
13    the License, or (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public
21    License along with this program; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23    MA 02111-1307, USA.
24
25  */
26
27 #include "bacula.h"
28 #include "dird.h"
29
30 /* Forward referenced subroutines */
31 static void job_thread(void *arg);
32
33 /* Exported subroutines */
34 void run_job(JCR *jcr);
35 void init_job_server(int max_workers);
36
37
38 /* Imported subroutines */
39 extern void term_scheduler();
40 extern void term_ua_server();
41 extern int do_backup(JCR *jcr);
42 extern int do_restore(JCR *jcr);
43 extern int do_verify(JCR *jcr);
44 extern void backup_cleanup(void);
45 extern void start_UA_server(int port);
46
47 /* Queue of jobs to be run */
48 static workq_t job_wq;                /* our job work queue */
49
50
51 void init_job_server(int max_workers)
52 {
53    int stat;
54
55    if ((stat = workq_init(&job_wq, max_workers, job_thread)) != 0) {
56       Emsg1(M_ABORT, 0, _("Could not init job work queue: ERR=%s\n"), strerror(stat));
57    }
58    return;
59 }
60
61 /*
62  * Run a job
63  *
64  */
65 void run_job(JCR *jcr)
66 {
67    int stat, errstat;
68
69    create_unique_job_name(jcr, jcr->job->hdr.name);
70    jcr->jr.SchedTime = jcr->sched_time;
71    jcr->jr.StartTime = jcr->start_time;
72    jcr->jr.Type = jcr->JobType;
73    jcr->jr.Level = jcr->level;
74    strcpy(jcr->jr.Name, jcr->job->hdr.name);
75    strcpy(jcr->jr.Job, jcr->Job);
76
77    /* Initialize termination condition variable */
78    if ((errstat = pthread_cond_init(&jcr->term_wait, NULL)) != 0) {
79       Emsg1(M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
80       jcr->JobStatus = JS_ErrorTerminated;
81       free_jcr(jcr);
82       return;
83    }
84
85    /*
86     * Open database
87     */
88    Dmsg0(50, "Open database\n");
89    jcr->db=db_init_database(jcr->catalog->db_name, jcr->catalog->db_user,
90                             jcr->catalog->db_password);
91    if (!db_open_database(jcr->db)) {
92       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
93       db_close_database(jcr->db);
94       jcr->JobStatus = JS_ErrorTerminated;
95       free_jcr(jcr);
96       return;
97    }
98    Dmsg0(50, "DB opened\n");
99
100
101    /*
102     * Create Job record  
103     */
104    jcr->jr.JobStatus = jcr->JobStatus;
105    if (!db_create_job_record(jcr->db, &jcr->jr)) {
106       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
107       db_close_database(jcr->db);
108       jcr->JobStatus = JS_ErrorTerminated;
109       free_jcr(jcr);
110       return;
111    }
112    jcr->JobId = jcr->jr.JobId;
113    ASSERT(jcr->jr.JobId > 0);
114
115    Dmsg4(30, "Created job record JobId=%d Name=%s Type=%c Level=%c\n", 
116        jcr->JobId, jcr->Job, jcr->jr.Type, jcr->jr.Level);
117    Dmsg0(200, "Add jrc to work queue\n");
118
119
120    /* Queue the job to be run */
121    if ((stat = workq_add(&job_wq, (void *)jcr)) != 0) {
122       Emsg1(M_ABORT, 0, _("Could not add job to work queue: ERR=%s\n"), strerror(stat));
123    }
124    Dmsg0(200, "Done run_job()\n");
125 }
126
127 /* This is the engine called by workq_add() */
128 static void job_thread(void *arg)
129 {
130    time_t now;
131    JCR *jcr = (JCR *)arg;
132
133    time(&now);
134
135    Dmsg0(100, "=====Start Job=========\n");
136    jcr->start_time = now;             /* set the real start time */
137    if (jcr->job->MaxStartDelay != 0 && jcr->job->MaxStartDelay <
138        (jcr->start_time - jcr->sched_time)) {
139       Jmsg(jcr, M_FATAL, 0, _("Job cancelled because max delay time exceeded.\n"));
140       free_jcr(jcr);
141    }
142    jcr->JobStatus = JS_Running;
143
144    switch (jcr->JobType) {
145       case JT_BACKUP:
146          do_backup(jcr);
147          break;
148       case JT_VERIFY:
149          do_verify(jcr);
150          break;
151       case JT_RESTORE:
152          do_restore(jcr);
153          break;
154       default:
155          Dmsg1(0, "Unimplemented job type: %d\n", jcr->JobType);
156          break;
157    }
158    Dmsg0(50, "Before free jcr\n");
159    free_jcr(jcr);
160    Dmsg0(50, "======== End Job ==========\n");
161 }
162
163 /*
164  * Takes base_name and appends (unique) current
165  *   date and time to form unique job name.
166  *
167  *  Returns: unique job name in jcr->Job
168  *    date/time in jcr->start_time
169  */
170 void create_unique_job_name(JCR *jcr, char *base_name)
171 {
172    /* Job start mutex */
173    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
174    static time_t last_start_time = 0;
175    time_t now;
176    struct tm tm;
177    char dt[MAX_TIME_LENGTH];
178    char name[MAX_NAME_LENGTH];
179    char *p;
180
181    /* Guarantee unique start time -- maximum one per second, and
182     * thus unique Job Name 
183     */
184    P(mutex);                          /* lock creation of jobs */
185    time(&now);
186    while (now == last_start_time) {
187       sleep(1);
188       time(&now);
189    }
190    last_start_time = now;
191    V(mutex);                          /* allow creation of jobs */
192    jcr->start_time = now;
193    /* Form Unique JobName */
194    localtime_r(&now, &tm);
195    strftime(dt, sizeof(dt), "%Y-%m-%d.%H:%M:%S", &tm); 
196    strncpy(name, base_name, sizeof(name));
197    name[sizeof(name)-22] = 0;          /* truncate if too long */
198    sprintf(jcr->Job, "%s.%s", name, dt); /* add date & time */
199    /* Convert spaces into underscores */
200    for (p=jcr->Job; *p; p++) {
201       if (*p == ' ') {
202          *p = '_';
203       }
204    }
205 }
206
207 /*
208  * Free the Job Control Record if no one is still using it.
209  *  Called from main free_jcr() routine in src/lib/jcr.c so
210  *  that we can do our Director specific cleanup of the jcr.
211  */
212 void dird_free_jcr(JCR *jcr)
213 {
214    Dmsg0(200, "Start dird free_jcr\n");
215
216    if (jcr->file_bsock) {
217       Dmsg0(200, "Close File bsock\n");
218       bnet_close(jcr->file_bsock);
219    }
220    if (jcr->store_bsock) {
221       Dmsg0(200, "Close Store bsock\n");
222       bnet_close(jcr->store_bsock);
223    }
224    if (jcr->fname) {  
225       Dmsg0(200, "Free JCR fname\n");
226       free_pool_memory(jcr->fname);
227    }
228    if (jcr->stime) {
229       Dmsg0(200, "Free JCR stime\n");
230       free_pool_memory(jcr->stime);
231    }
232    if (jcr->db) {
233       Dmsg0(200, "Close DB\n");
234       db_close_database(jcr->db);
235    }
236    if (jcr->RestoreWhere) {
237       free(jcr->RestoreWhere);
238    }
239    Dmsg0(200, "End dird free_jcr\n");
240 }
241
242 /*
243  * Set some defaults in the JCR necessary to
244  * run. These items are pulled from the job
245  * definition as defaults, but can be overridden
246  * later.
247  */
248 void set_jcr_defaults(JCR *jcr, JOB *job)
249 {
250    jcr->job = job;
251    jcr->JobType = job->JobType;
252    jcr->level = job->level;
253    jcr->store = job->storage;
254    jcr->client = job->client;
255    if (jcr->client_name) {
256       free(jcr->client_name);
257    }
258    jcr->client_name = bstrdup(job->client->hdr.name);
259    jcr->pool = job->pool;
260    jcr->catalog = job->client->catalog;
261    jcr->fileset = job->fs;
262    /* If no default level given, set one */
263    if (jcr->level == 0) {
264       switch (jcr->JobType) {
265       case JT_VERIFY:
266          jcr->level = L_VERIFY_CATALOG;
267          break;
268       case JT_BACKUP:
269          jcr->level = L_INCREMENTAL;
270          break;
271       default:
272          break;
273       }
274    }
275 }