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);
35 /* Exported subroutines */
36 void run_job(JCR *jcr);
37 void init_job_server(int max_workers);
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);
49 /* Queue of jobs to be run */
50 static workq_t job_wq; /* our job work queue */
53 void init_job_server(int max_workers)
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));
64 * Run a job -- typically called by the scheduler, but may also
65 * be called by the UA (Console program).
68 void run_job(JCR *jcr)
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);
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;
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;
102 Dmsg0(50, "DB opened\n");
108 jcr->jr.JobStatus = jcr->JobStatus;
109 if (!db_create_job_record(jcr->db, &jcr->jr)) {
110 Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
111 db_close_database(jcr->db);
112 jcr->JobStatus = JS_ErrorTerminated;
116 jcr->JobId = jcr->jr.JobId;
117 ASSERT(jcr->jr.JobId > 0);
119 Dmsg4(30, "Created job record JobId=%d Name=%s Type=%c Level=%c\n",
120 jcr->JobId, jcr->Job, jcr->jr.Type, jcr->jr.Level);
121 Dmsg0(200, "Add jrc to work queue\n");
124 /* Queue the job to be run */
125 if ((stat = workq_add(&job_wq, (void *)jcr)) != 0) {
126 Emsg1(M_ABORT, 0, _("Could not add job to work queue: ERR=%s\n"), strerror(stat));
128 Dmsg0(200, "Done run_job()\n");
132 * This is the engine called by workq_add() when we were pulled
133 * from the work queue.
134 * At this point, we are running in our own thread
136 static void job_thread(void *arg)
139 JCR *jcr = (JCR *)arg;
143 Dmsg0(100, "=====Start Job=========\n");
144 jcr->start_time = now; /* set the real start time */
145 if (jcr->job->MaxStartDelay != 0 && jcr->job->MaxStartDelay <
146 (btime_t)(jcr->start_time - jcr->sched_time)) {
147 Jmsg(jcr, M_FATAL, 0, _("Job cancelled because max delay time exceeded.\n"));
148 jcr->JobStatus = JS_ErrorTerminated;
149 update_job_end_record(jcr);
153 jcr->JobStatus = JS_Running;
155 switch (jcr->JobType) {
158 if (jcr->JobStatus == JS_Terminated) {
164 // do_autoprune(jcr);
168 // do_autoprune(jcr);
175 Dmsg1(0, "Unimplemented job type: %d\n", jcr->JobType);
179 Dmsg0(50, "Before free jcr\n");
181 Dmsg0(50, "======== End Job ==========\n");
185 * Get or create a Client record for this Job
187 int get_or_create_client_record(JCR *jcr)
191 memset(&cr, 0, sizeof(cr));
192 strcpy(cr.Name, jcr->client->hdr.name);
193 cr.AutoPrune = jcr->client->AutoPrune;
194 cr.FileRetention = jcr->client->FileRetention;
195 cr.JobRetention = jcr->client->JobRetention;
196 if (jcr->client_name) {
197 free_pool_memory(jcr->client_name);
199 jcr->client_name = get_memory(strlen(jcr->client->hdr.name) + 1);
200 strcpy(jcr->client_name, jcr->client->hdr.name);
201 if (!db_create_client_record(jcr->db, &cr)) {
202 Jmsg(jcr, M_ERROR, 0, _("Could not create Client record. %s"),
203 db_strerror(jcr->db));
206 jcr->jr.ClientId = cr.ClientId;
207 Dmsg2(9, "Created Client %s record %d\n", jcr->client->hdr.name,
214 * Write status and such in DB
216 void update_job_end_record(JCR *jcr)
218 if (jcr->jr.EndTime == 0) {
219 jcr->jr.EndTime = time(NULL);
221 jcr->end_time = jcr->jr.EndTime;
222 jcr->jr.JobId = jcr->JobId;
223 jcr->jr.JobStatus = jcr->JobStatus;
224 jcr->jr.JobFiles = jcr->JobFiles;
225 jcr->jr.JobBytes = jcr->JobBytes;
226 jcr->jr.VolSessionId = jcr->VolSessionId;
227 jcr->jr.VolSessionTime = jcr->VolSessionTime;
228 if (!db_update_job_end_record(jcr->db, &jcr->jr)) {
229 Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"),
230 db_strerror(jcr->db));
235 * Takes base_name and appends (unique) current
236 * date and time to form unique job name.
238 * Returns: unique job name in jcr->Job
239 * date/time in jcr->start_time
241 void create_unique_job_name(JCR *jcr, char *base_name)
243 /* Job start mutex */
244 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
245 static time_t last_start_time = 0;
248 char dt[MAX_TIME_LENGTH];
249 char name[MAX_NAME_LENGTH];
252 /* Guarantee unique start time -- maximum one per second, and
253 * thus unique Job Name
255 P(mutex); /* lock creation of jobs */
257 while (now == last_start_time) {
261 last_start_time = now;
262 V(mutex); /* allow creation of jobs */
263 jcr->start_time = now;
264 /* Form Unique JobName */
265 localtime_r(&now, &tm);
266 strftime(dt, sizeof(dt), "%Y-%m-%d.%H:%M:%S", &tm);
267 strncpy(name, base_name, sizeof(name));
268 name[sizeof(name)-22] = 0; /* truncate if too long */
269 sprintf(jcr->Job, "%s.%s", name, dt); /* add date & time */
270 /* Convert spaces into underscores */
271 for (p=jcr->Job; *p; p++) {
279 * Free the Job Control Record if no one is still using it.
280 * Called from main free_jcr() routine in src/lib/jcr.c so
281 * that we can do our Director specific cleanup of the jcr.
283 void dird_free_jcr(JCR *jcr)
285 Dmsg0(200, "Start dird free_jcr\n");
287 if (jcr->file_bsock) {
288 Dmsg0(200, "Close File bsock\n");
289 bnet_close(jcr->file_bsock);
291 if (jcr->store_bsock) {
292 Dmsg0(200, "Close Store bsock\n");
293 bnet_close(jcr->store_bsock);
296 Dmsg0(200, "Free JCR fname\n");
297 free_pool_memory(jcr->fname);
300 Dmsg0(200, "Free JCR stime\n");
301 free_pool_memory(jcr->stime);
304 Dmsg0(200, "Close DB\n");
305 db_close_database(jcr->db);
307 if (jcr->RestoreWhere) {
308 free(jcr->RestoreWhere);
310 Dmsg0(200, "End dird free_jcr\n");
314 * Set some defaults in the JCR necessary to
315 * run. These items are pulled from the job
316 * definition as defaults, but can be overridden
317 * later either by the Run record in the Schedule resource,
318 * or by the Console program.
320 void set_jcr_defaults(JCR *jcr, JOB *job)
323 jcr->JobType = job->JobType;
324 jcr->JobLevel = job->level;
325 jcr->store = job->storage;
326 jcr->client = job->client;
327 if (jcr->client_name) {
328 free_pool_memory(jcr->client_name);
330 jcr->client_name = get_memory(strlen(jcr->client->hdr.name) + 1);
331 strcpy(jcr->client_name, jcr->client->hdr.name);
332 jcr->pool = job->pool;
333 jcr->catalog = job->client->catalog;
334 jcr->fileset = job->fs;
335 jcr->msgs = job->messages;
336 /* If no default level given, set one */
337 if (jcr->JobLevel == 0) {
338 switch (jcr->JobType) {
340 jcr->JobLevel = L_VERIFY_CATALOG;
343 jcr->JobLevel = L_INCREMENTAL;