3 * Bacula Director Job processing routines
5 * Kern Sibbald, October MM
8 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
30 /* Forward referenced subroutines */
31 static void job_thread(void *arg);
33 /* Exported subroutines */
34 void run_job(JCR *jcr);
35 void init_job_server(int max_workers);
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);
47 /* Queue of jobs to be run */
48 static workq_t job_wq; /* our job work queue */
51 void init_job_server(int max_workers)
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));
65 void run_job(JCR *jcr)
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);
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;
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;
98 Dmsg0(50, "DB opened\n");
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;
112 jcr->JobId = jcr->jr.JobId;
113 ASSERT(jcr->jr.JobId > 0);
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");
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));
124 Dmsg0(200, "Done run_job()\n");
127 /* This is the engine called by workq_add() */
128 static void job_thread(void *arg)
131 JCR *jcr = (JCR *)arg;
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"));
142 jcr->JobStatus = JS_Running;
144 switch (jcr->JobType) {
155 Dmsg1(0, "Unimplemented job type: %d\n", jcr->JobType);
158 Dmsg0(50, "Before free jcr\n");
160 Dmsg0(50, "======== End Job ==========\n");
164 * Takes base_name and appends (unique) current
165 * date and time to form unique job name.
167 * Returns: unique job name in jcr->Job
168 * date/time in jcr->start_time
170 void create_unique_job_name(JCR *jcr, char *base_name)
172 /* Job start mutex */
173 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
174 static time_t last_start_time = 0;
177 char dt[MAX_TIME_LENGTH];
178 char name[MAX_NAME_LENGTH];
181 /* Guarantee unique start time -- maximum one per second, and
182 * thus unique Job Name
184 P(mutex); /* lock creation of jobs */
186 while (now == last_start_time) {
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++) {
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.
212 void dird_free_jcr(JCR *jcr)
214 Dmsg0(200, "Start dird free_jcr\n");
216 if (jcr->file_bsock) {
217 Dmsg0(200, "Close File bsock\n");
218 bnet_close(jcr->file_bsock);
220 if (jcr->store_bsock) {
221 Dmsg0(200, "Close Store bsock\n");
222 bnet_close(jcr->store_bsock);
225 Dmsg0(200, "Free JCR fname\n");
226 free_pool_memory(jcr->fname);
229 Dmsg0(200, "Free JCR stime\n");
230 free_pool_memory(jcr->stime);
233 Dmsg0(200, "Close DB\n");
234 db_close_database(jcr->db);
236 if (jcr->RestoreWhere) {
237 free(jcr->RestoreWhere);
239 Dmsg0(200, "End dird free_jcr\n");
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
248 void set_jcr_defaults(JCR *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);
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) {
266 jcr->level = L_VERIFY_CATALOG;
269 jcr->level = L_INCREMENTAL;