2003-07-01 20:37:09 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2003-01-07 08:11:16 +00:00
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
2003-07-01 20:37:09 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-07-08 21:30:47 +00:00
|
|
|
//
|
2003-01-07 08:11:16 +00:00
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
2003-07-08 21:30:47 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2003-01-07 08:11:16 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
2003-07-01 20:37:09 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-07-08 21:30:47 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2003-01-07 08:11:16 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2004-09-09 23:45:45 +00:00
|
|
|
// This is a framework for an assimilator.
|
|
|
|
// You need to link with with an (application-specific) function
|
|
|
|
// assimilate_handler()
|
|
|
|
// in order to make a complete program.
|
2004-05-25 19:20:45 +00:00
|
|
|
//
|
|
|
|
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
2002-12-17 19:00:43 +00:00
|
|
|
#include <unistd.h>
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <ctime>
|
2003-01-23 08:07:48 +00:00
|
|
|
#include <vector>
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2003-04-07 19:06:00 +00:00
|
|
|
#include "boinc_db.h"
|
2002-12-17 19:00:43 +00:00
|
|
|
#include "parse.h"
|
2003-02-10 19:51:32 +00:00
|
|
|
#include "util.h"
|
2003-08-15 00:45:25 +00:00
|
|
|
#include "sched_config.h"
|
2003-03-08 00:09:40 +00:00
|
|
|
#include "sched_util.h"
|
2004-04-08 08:15:23 +00:00
|
|
|
#include "sched_msgs.h"
|
2003-01-23 08:07:48 +00:00
|
|
|
#include "assimilate_handler.h"
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2004-06-30 18:17:21 +00:00
|
|
|
using std::vector;
|
|
|
|
|
2003-06-20 01:31:03 +00:00
|
|
|
#define LOCKFILE "assimilator.out"
|
|
|
|
#define PIDFILE "assimilator.pid"
|
2003-02-10 19:51:32 +00:00
|
|
|
|
2003-09-02 21:16:55 +00:00
|
|
|
SCHED_CONFIG config;
|
2004-11-26 23:33:11 +00:00
|
|
|
bool update_db = true;
|
2004-11-30 00:28:19 +00:00
|
|
|
bool noinsert = false;
|
2004-11-26 23:33:11 +00:00
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2004-12-14 00:57:03 +00:00
|
|
|
#define SLEEP_INTERVAL 10
|
2003-12-31 23:09:21 +00:00
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
// assimilate all WUs that need it
|
2002-12-17 19:00:43 +00:00
|
|
|
// return nonzero if did anything
|
|
|
|
//
|
2003-01-23 08:07:48 +00:00
|
|
|
bool do_pass(APP& app) {
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_WORKUNIT wu;
|
|
|
|
DB_RESULT canonical_result, result;
|
2003-08-15 20:35:44 +00:00
|
|
|
bool did_something = false;
|
2004-05-13 18:18:22 +00:00
|
|
|
char buf[256];
|
2004-10-04 23:59:51 +00:00
|
|
|
int retval;
|
2004-11-30 00:08:11 +00:00
|
|
|
int num_assimilated=0;
|
2004-06-24 21:00:13 +00:00
|
|
|
|
2004-05-03 19:30:01 +00:00
|
|
|
check_stop_daemons();
|
2003-03-08 00:09:40 +00:00
|
|
|
|
2004-08-31 23:45:07 +00:00
|
|
|
sprintf(buf, "where appid=%d and assimilate_state=%d limit 1000", app.id, ASSIMILATE_READY);
|
2003-06-04 17:21:26 +00:00
|
|
|
while (!wu.enumerate(buf)) {
|
2003-08-15 20:35:44 +00:00
|
|
|
vector<RESULT> results; // must be inside while()!
|
2004-11-26 23:33:11 +00:00
|
|
|
|
2004-11-29 22:26:34 +00:00
|
|
|
// for testing purposes, pretend we did nothing
|
|
|
|
//
|
|
|
|
if (update_db) {
|
|
|
|
did_something = true;
|
|
|
|
}
|
2003-01-07 22:49:42 +00:00
|
|
|
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::DEBUG,
|
2003-08-12 20:58:24 +00:00
|
|
|
"[%s] assimilating; state=%d\n", wu.name, wu.assimilate_state
|
|
|
|
);
|
2003-01-07 22:49:42 +00:00
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
sprintf(buf, "where workunitid=%d", wu.id);
|
|
|
|
while (!result.enumerate(buf)) {
|
2003-01-23 08:07:48 +00:00
|
|
|
results.push_back(result);
|
|
|
|
if (result.id == wu.canonical_resultid) {
|
|
|
|
canonical_result = result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-20 20:47:25 +00:00
|
|
|
retval = assimilate_handler(wu, results, canonical_result);
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
|
|
|
"[%s] handler returned error %d; exiting\n", wu.name, retval
|
|
|
|
);
|
|
|
|
}
|
2003-01-23 08:07:48 +00:00
|
|
|
|
2004-11-29 22:26:34 +00:00
|
|
|
if (update_db) {
|
|
|
|
sprintf(
|
|
|
|
buf, "assimilate_state=%d, transition_time=%d",
|
|
|
|
ASSIMILATE_DONE, (int)time(0)
|
|
|
|
);
|
|
|
|
retval = wu.update_field(buf);
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL,
|
|
|
|
"[%s] update failed: %d\n", wu.name, retval
|
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2004-11-30 00:50:46 +00:00
|
|
|
|
|
|
|
num_assimilated++;
|
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
2004-11-30 00:50:46 +00:00
|
|
|
|
2004-11-15 23:46:50 +00:00
|
|
|
if (did_something) {
|
|
|
|
boinc_db.commit_transaction();
|
|
|
|
}
|
2004-11-30 00:08:11 +00:00
|
|
|
|
2004-12-14 00:57:03 +00:00
|
|
|
if (num_assimilated) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::NORMAL,
|
2004-11-30 00:08:11 +00:00
|
|
|
"Assimilated %d workunits.\n", num_assimilated
|
2004-12-14 00:57:03 +00:00
|
|
|
);
|
|
|
|
}
|
2004-11-30 00:08:11 +00:00
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
return did_something;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
int retval;
|
|
|
|
bool asynch = false, one_pass = false;
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_APP app;
|
2002-12-17 19:00:43 +00:00
|
|
|
int i;
|
2003-01-07 01:02:08 +00:00
|
|
|
char buf[256];
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2004-05-03 19:30:01 +00:00
|
|
|
check_stop_daemons();
|
2002-12-17 19:00:43 +00:00
|
|
|
for (i=1; i<argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-asynch")) {
|
|
|
|
asynch = true;
|
|
|
|
} else if (!strcmp(argv[i], "-one_pass")) {
|
|
|
|
one_pass = true;
|
2003-06-11 23:36:40 +00:00
|
|
|
} else if (!strcmp(argv[i], "-d")) {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.set_debug_level(atoi(argv[++i]));
|
2002-12-18 01:34:51 +00:00
|
|
|
} else if (!strcmp(argv[i], "-app")) {
|
|
|
|
strcpy(app.name, argv[++i]);
|
2004-11-29 22:26:34 +00:00
|
|
|
} else if (!strcmp(argv[i], "-dont_update_db")) {
|
2004-12-14 00:57:03 +00:00
|
|
|
// This option is for testing your assimilator. When set,
|
|
|
|
// it ensures that the assimilator does not actually modify
|
|
|
|
// the assimilate_state of the workunits, so you can run
|
|
|
|
// your assimilator over and over again without affecting
|
|
|
|
// your project.
|
2004-11-29 22:26:34 +00:00
|
|
|
update_db = false;
|
2004-11-30 00:28:19 +00:00
|
|
|
} else if (!strcmp(argv[i], "-noinsert")) {
|
2004-12-14 00:57:03 +00:00
|
|
|
// This option is also for testing and is used to
|
|
|
|
// prevent the inserting of results into the *backend*
|
|
|
|
// (as opposed to the boinc) DB.
|
2004-11-30 00:28:19 +00:00
|
|
|
noinsert = true;
|
2002-12-17 19:00:43 +00:00
|
|
|
} else {
|
2004-11-29 22:26:34 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "Unrecognized arg: %s\n", argv[i]);
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-02 21:16:55 +00:00
|
|
|
retval = config.parse_file("..");
|
2002-12-17 19:00:43 +00:00
|
|
|
if (retval) {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "Can't parse config file\n");
|
2002-12-17 19:00:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asynch) {
|
|
|
|
if (fork()) {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-08 21:30:47 +00:00
|
|
|
// // Call lock_file after fork(), because file locks are not always inherited
|
|
|
|
// if (lock_file(LOCKFILE)) {
|
2004-04-08 08:15:23 +00:00
|
|
|
// log_messages.printf(SCHED_MSG_LOG::NORMAL, "Another copy of assimilator is already running\n");
|
2003-07-08 21:30:47 +00:00
|
|
|
// exit(1);
|
|
|
|
// }
|
|
|
|
// write_pid_file(PIDFILE);
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::NORMAL, "Starting\n");
|
2003-07-01 00:39:54 +00:00
|
|
|
|
2004-01-15 23:53:13 +00:00
|
|
|
retval = boinc_db.open(config.db_name, config.db_host, config.db_user, config.db_passwd);
|
2002-12-18 01:34:51 +00:00
|
|
|
if (retval) {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "Can't open DB\n");
|
2002-12-18 01:34:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-06-04 17:21:26 +00:00
|
|
|
sprintf(buf, "where name='%s'", app.name);
|
|
|
|
retval = app.lookup(buf);
|
2002-12-18 01:34:51 +00:00
|
|
|
if (retval) {
|
2004-04-08 08:15:23 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::CRITICAL, "Can't find app\n");
|
2002-12-18 01:34:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-12-31 23:09:21 +00:00
|
|
|
install_stop_signal_handler();
|
2002-12-17 19:00:43 +00:00
|
|
|
if (one_pass) {
|
2002-12-18 01:34:51 +00:00
|
|
|
do_pass(app);
|
2002-12-17 19:00:43 +00:00
|
|
|
} else {
|
|
|
|
while (1) {
|
2003-12-31 23:09:21 +00:00
|
|
|
if (!do_pass(app)) sleep(SLEEP_INTERVAL);
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-12-06 22:41:19 +00:00
|
|
|
|
2004-12-08 00:40:19 +00:00
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
static volatile const char __attribute__((unused)) *BOINCrcsid="$Id$";
|
|
|
|
#else
|
2004-12-06 22:41:19 +00:00
|
|
|
static volatile const char *BOINCrcsid="$Id$";
|
2004-12-08 00:40:19 +00:00
|
|
|
#endif
|