[PATCH] Workers: Add job of type callback
Max Sikstrom
max.sikstrom at op5.com
Wed Mar 6 23:55:56 CET 2013
From: Max Sikström <msikstrom at op5.com>
To be able to run jobs from broker modules, and in a generic way handle the
result from the job, without blocking, without polling, and without specific
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 process,
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 the
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öm <msikstrom at op5.com>
---
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, time_t timeout, const char *c
job->arg = arg;
job->timeout = timeout;
job->command = strdup(command);
+ job->callback = NULL;
return job;
}
@@ -195,6 +196,12 @@ static void destroy_job(worker_process *wp, worker_job *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", job->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 = 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, nagios_macros *mac)
job = 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 = timeout + time(NULL);
+
+ job = create_job(WPJOB_CALLBACK, arg, real_timeout, cmd);
+ job->callback = 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
#define WPROC_FORCE (1 << 0)
@@ -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, char *cmd, nagios_macros *mac);
extern int wproc_run_host_job(int jtype, int timeout, host *hst, char *cmd, 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 result is done, takes a wproc_result and the arg pointer */
} worker_job;
/** A worker process as seen from its controller */
--
1.7.1
------------------------------------------------------------------------------
Symantec Endpoint Protection 12 positioned as A LEADER in The Forrester
Wave(TM): Endpoint Security, Q1 2013 and "remains a good choice" in the
endpoint security space. For insight on selecting the right partner to
tackle endpoint security challenges, access the full report.
http://p.sf.net/sfu/symantec-dev2dev
_______________________________________________
Nagios-devel mailing list
Nagios-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-devel
More information about the Developers
mailing list