1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC. If not, see <http://www.gnu.org/licenses/>.
18 // This is a framework for an assimilator.
19 // You need to link this with an (application-specific) function
20 // assimilate_handler()
21 // in order to make a complete program.
34 #include "error_numbers.h"
37 #include "sched_config.h"
38 #include "sched_util.h"
39 #include "sched_msgs.h"
40 #include "assimilate_handler.h"
44 #define LOCKFILE "assimilator.out"
45 #define PIDFILE "assimilator.pid"
46 #define SLEEP_INTERVAL 10
48 bool update_db = true;
49 bool noinsert = false;
50 int wu_id_modulus=0, wu_id_remainder=0;
51 int sleep_interval = SLEEP_INTERVAL;
56 void usage(char** argv) {
58 "This program is an 'assimilator'; it handles completed jobs.\n"
59 "Normally it is run as a daemon from config.xml.\n"
60 "See: http://boinc.berkeley.edu/trac/wiki/BackendPrograms\n\n"
63 fprintf(stderr, "usage: %s [options]\n"
65 " -app name Process jobs for the given application\n"
66 " [-sleep_interval X] Sleep X seconds if no jobs to process (default 10)\n"
67 " [-mod N R] Process jobs with mod(ID, N) == R\n"
68 " [-one_pass] Do one DB enumeration, then exit\n"
69 " [-one_pass_N_WU N] Process at most N jobs\n"
70 " [-d N] Set verbosity level (1, 2, 3=most verbose)\n"
71 " [-dont_update_db] Don't update DB (for testing)\n"
72 " [-noinsert] Don't insert records in app-specific DB\n",
78 // assimilate all WUs that need it
79 // return nonzero if did anything
81 bool do_pass(APP& app) {
83 DB_RESULT canonical_result, result;
84 bool did_something = false;
88 int num_assimilated=0;
93 sprintf(mod_clause, " and workunit.id %% %d = %d ",
94 wu_id_modulus, wu_id_remainder
97 strcpy(mod_clause, "");
101 "where appid=%d and assimilate_state=%d %s limit %d",
102 app.id, ASSIMILATE_READY, mod_clause,
103 one_pass_N_WU ? one_pass_N_WU : 1000
106 retval = wu.enumerate(buf);
108 if (retval != ERR_DB_NOT_FOUND) {
109 log_messages.printf(MSG_DEBUG,
110 "DB connection lost, exiting\n"
116 vector<RESULT> results; // must be inside while()!
118 // for testing purposes, pretend we did nothing
121 did_something = true;
124 log_messages.printf(MSG_DEBUG,
125 "[%s] assimilating WU %d; state=%d\n", wu.name, wu.id, wu.assimilate_state
128 sprintf(buf, "where workunitid=%d", wu.id);
129 canonical_result.clear();
131 while (!result.enumerate(buf)) {
132 results.push_back(result);
133 if (result.id == wu.canonical_resultid) {
134 canonical_result = result;
139 // If no canonical result found and WU had no other errors,
140 // something is wrong, e.g. result records got deleted prematurely.
141 // This is probably unrecoverable, so mark the WU as having
142 // an assimilation error and keep going.
144 if (!found && !wu.error_mask) {
145 log_messages.printf(MSG_CRITICAL,
146 "[%s] no canonical result\n", wu.name
148 wu.error_mask = WU_ERROR_NO_CANONICAL_RESULT;
149 sprintf(buf, "error_mask=%d", wu.error_mask);
150 wu.update_field(buf);
153 retval = assimilate_handler(wu, results, canonical_result);
154 if (retval && retval != DEFER_ASSIMILATION) {
155 log_messages.printf(MSG_CRITICAL,
156 "[%s] handler returned error %d; exiting\n", wu.name, retval
162 // Defer assimilation until next result is returned
163 int assimilate_state = ASSIMILATE_DONE;
164 if (retval == DEFER_ASSIMILATION) {
165 assimilate_state = ASSIMILATE_INIT;
168 buf, "assimilate_state=%d, transition_time=%d",
169 assimilate_state, (int)time(0)
171 retval = wu.update_field(buf);
173 log_messages.printf(MSG_CRITICAL,
174 "[%s] update failed: %d\n", wu.name, retval
185 boinc_db.commit_transaction();
188 if (num_assimilated) {
189 log_messages.printf(MSG_NORMAL,
190 "Assimilated %d workunits.\n", num_assimilated
194 return did_something;
197 int main(int argc, char** argv) {
199 bool one_pass = false;
204 strcpy(app.name, "");
205 check_stop_daemons();
208 for (i=1; i<argc; i++) {
209 if (!strcmp(argv[i], "-one_pass_N_WU")) {
210 one_pass_N_WU = atoi(argv[++i]);
212 } else if (!strcmp(argv[i], "-sleep_interval")) {
213 sleep_interval = atoi(argv[++i]);
214 } else if (!strcmp(argv[i], "-one_pass")) {
216 } else if (!strcmp(argv[i], "-d")) {
217 log_messages.set_debug_level(atoi(argv[++i]));
218 } else if (!strcmp(argv[i], "-app")) {
219 strcpy(app.name, argv[++i]);
220 } else if (!strcmp(argv[i], "-dont_update_db")) {
221 // This option is for testing your assimilator. When set,
222 // it ensures that the assimilator does not actually modify
223 // the assimilate_state of the workunits, so you can run
224 // your assimilator over and over again without affecting
227 } else if (!strcmp(argv[i], "-noinsert")) {
228 // This option is also for testing and is used to
229 // prevent the inserting of results into the *backend*
230 // (as opposed to the boinc) DB.
232 } else if (!strcmp(argv[i], "-mod")) {
233 wu_id_modulus = atoi(argv[++i]);
234 wu_id_remainder = atoi(argv[++i]);
235 } else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
238 log_messages.printf(MSG_CRITICAL, "Unrecognized arg: %s\n", argv[i]);
243 if (!strlen(app.name)) {
248 log_messages.printf(MSG_DEBUG,
249 "Using mod'ed WU enumeration. modulus = %d remainder = %d\n",
250 wu_id_modulus, wu_id_remainder
254 retval = config.parse_file("..");
256 log_messages.printf(MSG_CRITICAL,
257 "Can't parse ../config.xml: %s\n", boincerror(retval)
262 log_messages.printf(MSG_NORMAL, "Starting\n");
264 retval = boinc_db.open(config.db_name, config.db_host, config.db_user, config.db_passwd);
266 log_messages.printf(MSG_CRITICAL, "Can't open DB\n");
269 sprintf(buf, "where name='%s'", app.name);
270 retval = app.lookup(buf);
272 log_messages.printf(MSG_CRITICAL, "Can't find app\n");
275 install_stop_signal_handler();
279 sleep(sleep_interval);
285 const char *BOINC_RCSID_7841370789 = "$Id: assimilator.cpp 16121 2008-10-03 19:31:56Z davea $";