From: Max Sikstr=C3=B6m
To be able to run jobs from broker modules, and in a generic way handle t=
he
result from the job, without blocking, without polling, and without speci=
fic
code within nagios, a callback infrastructure of jobs in the worker is
nessecary.
This makes a method wproc_job_callback, which sends a job to a worker pro=
cess,
and calls the callback, either on successful result, or when the job is
destroyed.
The code enforces that the callback is only runned once, independent of t=
he
result the job, so the callback needs to clean up the context.
Changes in the ABI: adds an extra callback variable at the end of the
worker_job structure, and presents a kvvec object with the result to the
callback method
Signed-off-by: Max Sikstr=C3=B6m
---
base/workers.c | 24 +++++++++++++++++++++++-
include/workers.h | 2 ++
lib/worker.h | 1 +
3 files changed, 26 insertions(+), 1 deletions(-)
diff --git a/base/workers.c b/base/workers.c
index 4af4c8c..6205bbb 100644
--- a/base/workers.c
+++ b/base/workers.c
@@ -133,6 +133,7 @@ static worker_job *create_job(int type, void *arg, ti=
me_t timeout, const char *c
job->arg =3D arg;
job->timeout =3D timeout;
job->command =3D strdup(command);
+ job->callback =3D NULL;
=20
return job;
}
@@ -195,6 +196,12 @@ static void destroy_job(worker_process *wp, worker_j=
ob *job)
case WPJOB_HOST_EVTHANDLER:
/* these require nothing special */
break;
+
+ case WPJOB_CALLBACK:
+ if( job->callback ) {
+ (*job->callback)(NULL, job->arg, 0);
+ }
+ break;
default:
logit(NSLOG_RUNTIME_WARNING, TRUE, "wproc: Unknown job type: %d\n", jo=
b->type);
break;
@@ -616,7 +623,12 @@ static int handle_worker_result(int sd, int events, =
void *arg)
job->command, tv2float(&wpres.runtime));
}
break;
-
+ case WPJOB_CALLBACK:
+ if( job->callback ) {
+ (*job->callback)(&kvv, job->arg, 0);
+ }
+ job->callback =3D NULL; /* Don't run again... just once per job */
+ break;
default:
logit(NSLOG_RUNTIME_WARNING, TRUE, "Worker %d: Unknown jobtype: %d\n"=
, wp->pid, job->type);
break;
@@ -1008,3 +1020,13 @@ int wproc_run(int jtype, char *cmd, int timeout, n=
agios_macros *mac)
job =3D create_job(jtype, NULL, real_timeout, cmd);
return wproc_run_job(job, mac);
}
+
+int wproc_run_callback(char *cmd, int timeout, nagios_macros *mac, void =
(*callback)(struct kvvec *, void *, int), void *arg)
+{
+ worker_job *job;
+ time_t real_timeout =3D timeout + time(NULL);
+
+ job =3D create_job(WPJOB_CALLBACK, arg, real_timeout, cmd);
+ job->callback =3D callback;
+ return wproc_run_job(job, mac);
+}
diff --git a/include/workers.h b/include/workers.h
index 56b728d..e9b422a 100644
--- a/include/workers.h
+++ b/include/workers.h
@@ -13,6 +13,7 @@
#define WPJOB_SVC_EVTHANDLER 5
#define WPJOB_GLOBAL_HOST_EVTHANDLER 6
#define WPJOB_HOST_EVTHANDLER 7
+#define WPJOB_CALLBACK 8
=20
#define WPROC_FORCE (1 << 0)
=20
@@ -30,5 +31,6 @@ extern int wproc_notify(char *cname, char *hname, char =
*sdesc, char *cmd, nagios
extern int wproc_run(int job_type, char *cmd, int timeout, nagios_macros=
*mac);
extern int wproc_run_service_job(int jtype, int timeout, service *svc, c=
har *cmd, nagios_macros *mac);
extern int wproc_run_host_job(int jtype, int timeout, host *hst, char *c=
md, nagios_macros *mac);
+extern int wproc_run_callback(char *cmd, int timeout, nagios_macros *mac=
, void (*callback)(struct kvvec *, void *, int), void *arg);
extern int wproc_destroy(worker_process *wp, int flags);
#endif
diff --git a/lib/worker.h b/lib/worker.h
index 8298218..c457da4 100644
--- a/lib/worker.h
+++ b/lib/worker.h
@@ -44,6 +44,7 @@ typedef struct worker_job {
char *command; /**< command string for this job */
struct worker_process *wp; /**< worker process running this job */
void *arg; /**< any random argument */
+ void (*callback)(struct kvvec *, void *, int); /**< callback when resul=
t is done, takes a wproc_result and the arg pointer */
} worker_job;
=20
/** A worker process as seen from its controller */
--=20
1.7.1
This post was automatically imported from historical nagios-devel mailing list archives
Original poster: [email protected]