OpenDNSSEC-enforcer  2.0.3
worker.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 NLNet Labs. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
32 #include "daemon/engine.h"
33 #include "daemon/worker.h"
34 #include "scheduler/schedule.h"
35 #include "scheduler/task.h"
36 #include "log.h"
37 #include "status.h"
38 #include "util.h"
39 #include "duration.h"
40 
46 worker_create(int num)
47 {
48  worker_type* worker;
49 
50  worker = (worker_type*) malloc( sizeof(worker_type) );
51  if (!worker) {
52  return NULL;
53  }
54 
55  ods_log_debug("create worker[%i]", num +1);
56  worker->thread_num = num +1;
57  worker->engine = NULL;
58  worker->task = NULL;
59  worker->need_to_exit = 0;
60  worker->jobs_appointed = 0;
61  worker->jobs_completed = 0;
62  worker->jobs_failed = 0;
63  worker->sleeping = 0;
64  worker->waiting = 0;
65  worker->dbconn = NULL;
66  return worker;
67 }
68 
73 static void
74 worker_perform_task(worker_type* worker)
75 {
76  task_type* task;
77 
78  if (!worker || !worker->task || !worker->task->context || !worker->engine) {
79  return;
80  }
81 
82  task = (task_type*) worker->task;
83  ods_log_debug("[worker[%i]]: perform task [%s] for %s",
84  worker->thread_num, task_what2str(task->what),
85  task_who2str(task->who));
86 
87  /* We temporarily assign the database connection to the task so
88  * it is accessable from the task function */
89  task->dbconn = worker->dbconn;
90  worker->task = task_perform(task);
91  if (worker->task) task->dbconn = NULL;
92 }
93 
98 void
100 {
101  ods_log_assert(worker);
102 
103  while (worker->need_to_exit == 0) {
104  ods_log_debug("[worker[%i]]: report for duty", worker->thread_num);
105 
106  /* When no task available this call blocks and waits for event.
107  * Then it will return NULL; */
108  worker->task = schedule_pop_task(worker->engine->taskq);
109  if (worker->task) {
110  ods_log_debug("[worker[%i]] start working", worker->thread_num);
111  worker_perform_task(worker);
112  ods_log_debug("[worker[%i]] finished working", worker->thread_num);
113  if (worker->task) {
114  if (schedule_task(worker->engine->taskq, worker->task) !=
115  ODS_STATUS_OK)
116  {
117  ods_log_error("[worker[%i]] unable to schedule task",
118  worker->thread_num);
119  }
120  worker->task = NULL;
121  }
122  }
123  }
124 }
125 
130 void
132 {
133  if (!worker) return;
134  free(worker);
135 }
int waiting
Definition: worker.h:52
size_t jobs_completed
Definition: worker.h:49
void ods_log_debug(const char *format,...)
Definition: log.c:41
size_t jobs_appointed
Definition: worker.h:48
char * who
Definition: task.h:66
worker_type * worker_create(int num)
Definition: worker.c:46
void worker_start(worker_type *worker)
Definition: worker.c:99
int need_to_exit
Definition: worker.h:53
void * context
Definition: task.h:68
ods_status schedule_task(schedule_type *schedule, task_type *task)
Definition: schedule.c:401
void ods_log_error(const char *format,...)
Definition: log.c:69
void worker_cleanup(worker_type *worker)
Definition: worker.c:131
struct engine_struct * engine
Definition: worker.h:46
size_t jobs_failed
Definition: worker.h:50
task_type * task
Definition: worker.h:47
task_type * task_perform(task_type *task)
Definition: task.c:292
task_type * schedule_pop_task(schedule_type *schedule)
Definition: schedule.c:370
db_connection_t * dbconn
Definition: task.h:71
const char * task_what2str(int what)
Definition: task.c:212
db_connection_t * dbconn
Definition: worker.h:54
const char * task_who2str(const char *who)
Definition: task.c:240
task_id what
Definition: task.h:60
int thread_num
Definition: worker.h:44
schedule_type * taskq
Definition: engine.h:55
int sleeping
Definition: worker.h:51