3 * Bacula Director Job processing routines
5 * Kern Sibbald, October MM
10 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
32 /* Forward referenced subroutines */
33 static void job_thread(void *arg);
34 static char *edit_run_codes(JCR *jcr, char *omsg, char *imsg);
36 /* Exported subroutines */
37 void run_job(JCR *jcr);
38 void init_job_server(int max_workers);
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);
50 /* Queue of jobs to be run */
51 static workq_t job_wq; /* our job work queue */
54 void init_job_server(int max_workers)
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));
65 * Run a job -- typically called by the scheduler, but may also
66 * be called by the UA (Console program).
69 void run_job(JCR *jcr)
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);
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;
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;
104 Dmsg0(50, "DB opened\n");
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;
117 jcr->JobId = jcr->jr.JobId;
118 ASSERT(jcr->jr.JobId > 0);
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");
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));
129 Dmsg0(200, "Done run_job()\n");
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
137 static void job_thread(void *arg)
140 JCR *jcr = (JCR *)arg;
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);
154 jcr->JobStatus = JS_Running;
156 if (jcr->job->RunBeforeJob) {
157 POOLMEM *before = get_pool_memory(PM_FNAME);
160 before = edit_run_codes(jcr, before, jcr->job->RunBeforeJob);
161 status = run_program(before, 0, NULL);
162 free_pool_memory(before);
164 switch (jcr->JobType) {
167 if (jcr->JobStatus == JS_Terminated) {
173 if (jcr->JobStatus == JS_Terminated) {
179 if (jcr->JobStatus == JS_Terminated) {
186 jcr->JobStatus = JS_Terminated;
189 Dmsg1(0, "Unimplemented job type: %d\n", jcr->JobType);
193 if (jcr->job->RunAfterJob) {
194 POOLMEM *after = get_pool_memory(PM_FNAME);
197 after = edit_run_codes(jcr, after, jcr->job->RunAfterJob);
198 status = run_program(after, 0, NULL);
199 free_pool_memory(after);
201 Dmsg0(50, "Before free jcr\n");
203 Dmsg0(50, "======== End Job ==========\n");
207 * Get or create a Client record for this Job
209 int get_or_create_client_record(JCR *jcr)
213 memset(&cr, 0, sizeof(cr));
214 strcpy(cr.Name, jcr->client->hdr.name);
215 cr.AutoPrune = jcr->client->AutoPrune;
216 cr.FileRetention = jcr->client->FileRetention;
217 cr.JobRetention = jcr->client->JobRetention;
218 if (jcr->client_name) {
219 free_pool_memory(jcr->client_name);
221 jcr->client_name = get_memory(strlen(jcr->client->hdr.name) + 1);
222 strcpy(jcr->client_name, jcr->client->hdr.name);
223 if (!db_create_client_record(jcr->db, &cr)) {
224 Jmsg(jcr, M_FATAL, 0, _("Could not create Client record. %s"),
225 db_strerror(jcr->db));
228 jcr->jr.ClientId = cr.ClientId;
229 Dmsg2(9, "Created Client %s record %d\n", jcr->client->hdr.name,
236 * Write status and such in DB
238 void update_job_end_record(JCR *jcr)
240 if (jcr->jr.EndTime == 0) {
241 jcr->jr.EndTime = time(NULL);
243 jcr->end_time = jcr->jr.EndTime;
244 jcr->jr.JobId = jcr->JobId;
245 jcr->jr.JobStatus = jcr->JobStatus;
246 jcr->jr.JobFiles = jcr->JobFiles;
247 jcr->jr.JobBytes = jcr->JobBytes;
248 jcr->jr.VolSessionId = jcr->VolSessionId;
249 jcr->jr.VolSessionTime = jcr->VolSessionTime;
250 if (!db_update_job_end_record(jcr->db, &jcr->jr)) {
251 Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"),
252 db_strerror(jcr->db));
257 * Takes base_name and appends (unique) current
258 * date and time to form unique job name.
260 * Returns: unique job name in jcr->Job
261 * date/time in jcr->start_time
263 void create_unique_job_name(JCR *jcr, char *base_name)
265 /* Job start mutex */
266 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
267 static time_t last_start_time = 0;
270 char dt[MAX_TIME_LENGTH];
271 char name[MAX_NAME_LENGTH];
274 /* Guarantee unique start time -- maximum one per second, and
275 * thus unique Job Name
277 P(mutex); /* lock creation of jobs */
279 while (now == last_start_time) {
283 last_start_time = now;
284 V(mutex); /* allow creation of jobs */
285 jcr->start_time = now;
286 /* Form Unique JobName */
287 localtime_r(&now, &tm);
288 strftime(dt, sizeof(dt), "%Y-%m-%d.%H:%M:%S", &tm);
289 strncpy(name, base_name, sizeof(name));
290 name[sizeof(name)-22] = 0; /* truncate if too long */
291 sprintf(jcr->Job, "%s.%s", name, dt); /* add date & time */
292 /* Convert spaces into underscores */
293 for (p=jcr->Job; *p; p++) {
301 * Free the Job Control Record if no one is still using it.
302 * Called from main free_jcr() routine in src/lib/jcr.c so
303 * that we can do our Director specific cleanup of the jcr.
305 void dird_free_jcr(JCR *jcr)
307 Dmsg0(200, "Start dird free_jcr\n");
309 if (jcr->file_bsock) {
310 Dmsg0(200, "Close File bsock\n");
311 bnet_close(jcr->file_bsock);
313 if (jcr->store_bsock) {
314 Dmsg0(200, "Close Store bsock\n");
315 bnet_close(jcr->store_bsock);
318 Dmsg0(200, "Free JCR fname\n");
319 free_pool_memory(jcr->fname);
322 Dmsg0(200, "Free JCR stime\n");
323 free_pool_memory(jcr->stime);
326 Dmsg0(200, "Close DB\n");
327 db_close_database(jcr->db);
329 if (jcr->RestoreWhere) {
330 free(jcr->RestoreWhere);
332 if (jcr->RestoreBootstrap) {
333 free(jcr->RestoreBootstrap);
335 Dmsg0(200, "End dird free_jcr\n");
339 * Set some defaults in the JCR necessary to
340 * run. These items are pulled from the job
341 * definition as defaults, but can be overridden
342 * later either by the Run record in the Schedule resource,
343 * or by the Console program.
345 void set_jcr_defaults(JCR *jcr, JOB *job)
348 jcr->JobType = job->JobType;
349 jcr->JobLevel = job->level;
350 jcr->store = job->storage;
351 jcr->client = job->client;
352 if (jcr->client_name) {
353 free_pool_memory(jcr->client_name);
355 jcr->client_name = get_memory(strlen(jcr->client->hdr.name) + 1);
356 strcpy(jcr->client_name, jcr->client->hdr.name);
357 jcr->pool = job->pool;
358 jcr->catalog = job->client->catalog;
359 jcr->fileset = job->fileset;
360 jcr->msgs = job->messages;
361 if (jcr->RestoreBootstrap) {
362 free(jcr->RestoreBootstrap);
364 /* This can be overridden by Console program */
365 if (job->RestoreBootstrap) {
366 jcr->RestoreBootstrap = bstrdup(job->RestoreBootstrap);
368 /* If no default level given, set one */
369 if (jcr->JobLevel == 0) {
370 switch (jcr->JobType) {
372 jcr->JobLevel = L_VERIFY_CATALOG;
375 jcr->JobLevel = L_INCREMENTAL;
384 * Edit codes into Run command
387 * %d = Director's name
395 * omsg = edited output message
396 * imsg = input string containing edit codes (%x)
399 static char *edit_run_codes(JCR *jcr, char *omsg, char *imsg)
405 Dmsg1(200, "edit_run_codes: %s\n", imsg);
408 for (p=imsg; *p; p++) {
417 str = jcr->client_name;
426 str = job_status_to_str(jcr->JobStatus);
429 sprintf(add, "%d", jcr->JobId);
436 str = job_level_to_str(jcr->JobLevel);
439 str = jcr->job->hdr.name;
442 str = job_type_to_str(jcr->JobType);
455 Dmsg1(200, "add_str %s\n", str);
456 add_str_to_pool_mem(&omsg, &o, (char *)str);
458 Dmsg1(200, "omsg=%s\n", omsg);