1 /* **************************************************************
2 Copyright (C) 2010-2013 Hewlett-Packard Development Company, L.P.
3 Copyright (C) 2015 Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 ************************************************************** */
18 
19 #ifndef JOB_H_INCLUDE
20 #define JOB_H_INCLUDE
21 
22 /* local includes */
23 #include <logging.h>
24 
25 /* std library includes */
26 #include <stdio.h>
27 #include <event.h>
28 #include <libpq-fe.h>
29 
30 /* glib includes */
31 #include <glib.h>
32 
33 /* ************************************************************************** */
34 /* **** Data Types ********************************************************** */
35 /* ************************************************************************** */
36 
37 #define JOB_STATUS_TYPES(apply)                        \
38   apply(NOT_AVAILABLE)                                 \
39   /** Checkout out from the DB, but not started yet */ \
40   apply(CHECKEDOUT)                                    \
41   /** Agents have started analysis on the job */       \
42   apply(STARTED)                                       \
43   /** All the data for this job has been analyzed */   \
44   apply(COMPLETE)                                      \
45   /** @FIXME NOT USED */                               \
46   apply(RESTART)                                       \
47   /** The job has failed, likely an agent failure */   \
48   apply(FAILED)                                        \
49   /** Paused by some user interface */                 \
50   apply(PAUSED)
51 
52 #define SELECT_ENUM(passed) JB_##passed,
53 typedef enum { JOB_STATUS_TYPES(SELECT_ENUM) } job_status;
54 #undef SELECT_ENUM
55 
56 extern const char* job_status_strings[];
57 
58 /**
59  * @brief The job structure
60  */
61 typedef struct
62 {
63     /* associated agent information */
64     char*  agent_type;      ///< The type of agent used to analyze the data
65     char*  required_host;   ///< If not NULL, this job must run on a specific host machine
66     GList* running_agents;  ///< The list of agents assigned to this job that are still working
67     GList* finished_agents; ///< The list of agents that have completed their tasks
68     GList* failed_agents;   ///< The list of agents that failed while working
69     log_t*  log;            ///< The log to print any agent logging messages to
70 
71     /* information for data manipulation */
72     job_status status;    ///< The current status for the job
73     gchar*     data;      ///< The data associated with this job (jq_args)
74     gchar     *jq_cmd_args; ///< Command line arguments for this job
75     PGresult*  db_result; ///< Results from the sql query (if any)
76     GMutex*    lock;      ///< Lock to maintain data integrity
77     uint32_t   idx;       ///< The current index into the sql results
78 
79     /* information about job status */
80     gchar*   message;   ///< Message that will be sent with job notification email
81     int32_t  priority;  ///< Importance of the job, maps directory to unix priority
82     int32_t  verbose;   ///< The verbose level for all of the agents in this job
83     int32_t  parent_id; ///< The identifier for the parent of this job (its queue id)
84     int32_t  id;        ///< The identifier for this job
85     int32_t  user_id;   ///< The id of the user that created the job
86     int32_t  group_id;  ///< The id of the group that created the job
87 } job_t;
88 
89 /* ************************************************************************** */
90 /* **** Constructor Destructor ********************************************** */
91 /* ************************************************************************** */
92 
93 job_t* job_init(GTree* job_list, GSequence* job_queue, char* type, char* host,
94     int id, int parent_id, int user_id, int group_id, int priority, char *jq_cmd_args);
95 void   job_destroy(job_t* job);
96 
97 /* ************************************************************************** */
98 /* **** Functions and events ************************************************ */
99 /* ************************************************************************** */
100 
101 void job_verbose_event (scheduler_t* scheduler, job_t* j);
102 void job_status_event  (scheduler_t* scheduler, arg_int* params);
103 void job_pause_event   (scheduler_t* scheduler, arg_int* params);
104 void job_restart_event (scheduler_t* scheduler, arg_int* params);
105 void job_priority_event(scheduler_t* scheduler, arg_int* params);
106 void job_fail_event    (scheduler_t* scheduler, job_t* job);
107 
108 void job_add_agent(job_t* job, void* a);
109 void job_remove_agent(job_t* job, GTree* job_list, void* a);
110 void job_finish_agent(job_t* job, void* a);
111 void job_fail_agent(job_t* job, void* a);
112 void job_set_data(scheduler_t* scheduler, job_t* job, char* data, int sql);
113 void job_update(scheduler_t* scheduler, job_t* job);
114 
115 gboolean  job_is_open(scheduler_t* scheduler, job_t* job);
116 gchar*    job_next(job_t* job);
117 log_t*    job_log(job_t* job);
118 
119 /* ************************************************************************** */
120 /* **** Job list Functions ************************************************** */
121 /* ************************************************************************** */
122 
123 job_t*   next_job(GSequence* job_queue);
124 job_t*   peek_job(GSequence* job_queue);
125 uint32_t active_jobs(GTree* job_list);
126 
127 #endif /* JOB_H_INCLUDE */
128