]> git.sur5r.net Git - bacula/bacula/blob - bacula/patches/testing/cleanup_CR_catalog_at_start.patch
ebl Add the -B option that extract Catalog configuration from
[bacula/bacula] / bacula / patches / testing / cleanup_CR_catalog_at_start.patch
1 This patch fixes all orphan jobs from catalog (Running/Created) during
2 startup.
3
4 This avoid phantom jobs with bweb or bat.
5
6   cd <bacula-source>
7   patch -p0 < cleanup_CR_catalog_at_start.patch
8   ./configure <your options>
9   make
10   ...
11   make install
12
13
14 Index: src/dird/dird.c
15 ===================================================================
16 --- src/dird/dird.c     (revision 7176)
17 +++ src/dird/dird.c     (working copy)
18 @@ -40,6 +40,7 @@
19  /* Forward referenced subroutines */
20  void terminate_dird(int sig);
21  static bool check_resources();
22 +static void cleanup_catalog();
23  static bool check_catalog();
24  static void dir_sql_query(JCR *jcr, const char *cmd);
25    
26 @@ -286,6 +287,8 @@
27     set_thread_concurrency(director->MaxConcurrentJobs * 2 +
28        4 /* UA */ + 4 /* sched+watchdog+jobsvr+misc */);
29  
30 +   cleanup_catalog();                 /* cleanup old running jobs */
31 +
32     Dmsg0(200, "Start UA server\n");
33     start_UA_server(director->DIRaddrs);
34  
35 @@ -881,6 +884,38 @@
36     return OK;
37  }
38  
39 +/* Cleanup old catalog records during starting */
40 +static void cleanup_catalog()
41 +{
42 +   CAT *catalog;
43 +   foreach_res(catalog, R_CATALOG) {
44 +      B_DB *db;
45 +      /*
46 +       * Make sure we can open catalog, otherwise print a warning
47 +       * message because the server is probably not running.
48 +       */
49 +      db = db_init(NULL, catalog->db_driver, catalog->db_name, catalog->db_user,
50 +                         catalog->db_password, catalog->db_address,
51 +                         catalog->db_port, catalog->db_socket,
52 +                         catalog->mult_db_connections);
53 +      if (!db || !db_open_database(NULL, db)) {
54 +         Pmsg2(000, _("Could not open Catalog \"%s\", database \"%s\".\n"),
55 +              catalog->name(), catalog->db_name);
56 +         Jmsg(NULL, M_FATAL, 0, _("Could not open Catalog \"%s\", database \"%s\".\n"),
57 +              catalog->name(), catalog->db_name);
58 +         if (db) {
59 +            Jmsg(NULL, M_FATAL, 0, _("%s"), db_strerror(db));
60 +            Pmsg1(000, "%s", db_strerror(db));
61 +            db_close_database(NULL, db);
62 +         }
63 +
64 +      } else {                            /* connection is OK */
65 +         db_cleanup_job_record(NULL, db); /* check if jobs are running ?? */
66 +        db_close_database(NULL, db);
67 +      }
68 +   }
69 +}
70 +
71  static bool check_catalog()
72  {
73     bool OK = true;
74 Index: src/cats/sql_update.c
75 ===================================================================
76 --- src/cats/sql_update.c       (revision 7176)
77 +++ src/cats/sql_update.c       (working copy)
78 @@ -288,6 +288,18 @@
79  }
80  
81  bool
82 +db_cleanup_job_record(JCR *jcr, B_DB *mdb)
83 +{
84 +   int stat;
85 +   char query[] = "UPDATE Job SET JobStatus = 'f', EndTime = NOW() WHERE JobStatus IN ('R', 'C')";
86 +
87 +   db_lock(mdb);
88 +   stat = UPDATE_DB(jcr, mdb, query);
89 +   db_unlock(mdb);
90 +   return stat;
91 +}
92 +
93 +bool
94  db_update_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *sr)
95  {
96     int stat;
97 Index: src/cats/protos.h
98 ===================================================================
99 --- src/cats/protos.h   (revision 7176)
100 +++ src/cats/protos.h   (working copy)
101 @@ -123,6 +123,7 @@
102  void db_list_client_records(JCR *jcr, B_DB *mdb, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type);
103  
104  /* sql_update.c */
105 +bool db_cleanup_job_record(JCR *jcr, B_DB *mdb);
106  bool db_update_job_start_record(JCR *jcr, B_DB *db, JOB_DBR *jr);
107  int  db_update_job_end_record(JCR *jcr, B_DB *db, JOB_DBR *jr, bool stats_enabled);
108  int  db_update_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr);