2003-01-07 08:11:16 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// 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-06-20 01:31:03 +00:00
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
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-06-20 01:31:03 +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-06-20 01:31:03 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002, 2003
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2003-01-07 08:11:16 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
#include <string.h>
|
2002-12-17 19:35:38 +00:00
|
|
|
#include <stdlib.h>
|
2002-12-17 19:00:43 +00:00
|
|
|
#include <unistd.h>
|
2003-01-07 08:11:16 +00:00
|
|
|
#include <time.h>
|
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"
|
2002-12-17 19:00:43 +00:00
|
|
|
#include "config.h"
|
2003-03-08 00:09:40 +00:00
|
|
|
#include "sched_util.h"
|
2003-01-23 08:07:48 +00:00
|
|
|
#include "assimilate_handler.h"
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2003-06-20 01:31:03 +00:00
|
|
|
#define LOCKFILE "assimilator.out"
|
|
|
|
#define PIDFILE "assimilator.pid"
|
2003-02-10 19:51:32 +00:00
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
CONFIG config;
|
|
|
|
|
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-01-23 08:07:48 +00:00
|
|
|
vector<RESULT> results;
|
|
|
|
bool did_something = false, delete_inputs, delete_outputs;
|
2003-01-07 22:49:42 +00:00
|
|
|
char buf[MAX_BLOB_SIZE];
|
2003-01-23 08:07:48 +00:00
|
|
|
unsigned int i;
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2003-03-08 00:09:40 +00:00
|
|
|
check_stop_trigger();
|
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
sprintf(buf, "where appid=%d and assimilate_state=%d", app.id, ASSIMILATE_READY);
|
|
|
|
while (!wu.enumerate(buf)) {
|
2002-12-17 19:00:43 +00:00
|
|
|
did_something = true;
|
2003-01-07 22:49:42 +00:00
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
sprintf(buf,
|
|
|
|
"Assimilating WU %s, assim state %d\n",
|
|
|
|
wu.name, wu.assimilate_state
|
|
|
|
);
|
2003-06-11 23:36:40 +00:00
|
|
|
write_log(buf, MSG_DEBUG);
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assimilate_handler(wu, results, canonical_result);
|
|
|
|
|
|
|
|
delete_outputs = true;
|
|
|
|
delete_inputs = true;
|
|
|
|
for (i=0; i<results.size(); i++) {
|
|
|
|
result = results[i];
|
|
|
|
if (result.server_state != RESULT_SERVER_STATE_OVER
|
|
|
|
|| (result.outcome != RESULT_OUTCOME_SUCCESS && result.outcome != RESULT_OUTCOME_CLIENT_ERROR)
|
|
|
|
) {
|
|
|
|
delete_outputs = false;
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
2003-01-23 08:07:48 +00:00
|
|
|
if (result.server_state != RESULT_SERVER_STATE_OVER) {
|
|
|
|
delete_inputs = false;
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
2003-01-23 08:07:48 +00:00
|
|
|
}
|
2003-01-07 22:49:42 +00:00
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
if (delete_outputs) {
|
|
|
|
for (i=0; i<results.size(); i++) {
|
|
|
|
result = results[i];
|
|
|
|
result.file_delete_state = FILE_DELETE_READY;
|
2003-06-04 17:21:26 +00:00
|
|
|
result.update();
|
2003-01-23 08:07:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i=0; i<results.size(); i++) {
|
|
|
|
result = results[i];
|
|
|
|
if (result.server_state == RESULT_SERVER_STATE_OVER
|
|
|
|
&& result.id != wu.canonical_resultid
|
|
|
|
&& (result.outcome == RESULT_OUTCOME_SUCCESS || result.outcome == RESULT_OUTCOME_CLIENT_ERROR)
|
|
|
|
) {
|
|
|
|
result.file_delete_state = FILE_DELETE_READY;
|
2003-06-04 17:21:26 +00:00
|
|
|
result.update();
|
2003-01-23 08:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
2003-01-23 08:07:48 +00:00
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
wu.assimilate_state = ASSIMILATE_DONE;
|
2003-01-23 08:07:48 +00:00
|
|
|
if (delete_inputs) {
|
|
|
|
wu.file_delete_state = FILE_DELETE_READY;
|
|
|
|
}
|
2003-06-04 17:21:26 +00:00
|
|
|
wu.update();
|
2003-04-04 19:38:57 +00:00
|
|
|
|
|
|
|
// Clear out result vector so we don't reuse them in the next WU
|
|
|
|
results.erase(results.begin(),results.end());
|
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
|
|
|
|
2003-03-08 00:09:40 +00:00
|
|
|
check_stop_trigger();
|
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")) {
|
|
|
|
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]);
|
2002-12-17 19:00:43 +00:00
|
|
|
} else {
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf, "Unrecognized arg: %s\n", argv[i]);
|
2003-06-11 23:36:40 +00:00
|
|
|
write_log(buf, MSG_CRITICAL);
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = config.parse_file();
|
|
|
|
if (retval) {
|
2003-06-11 23:36:40 +00:00
|
|
|
write_log("Can't parse config file\n", MSG_CRITICAL);
|
2002-12-17 19:00:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asynch) {
|
|
|
|
if (fork()) {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-01 07:13:43 +00:00
|
|
|
// Call lock_file after fork(), because file locks are not always inherited
|
2003-06-20 01:31:03 +00:00
|
|
|
if (lock_file(LOCKFILE)) {
|
2003-06-17 21:23:43 +00:00
|
|
|
write_log("Another copy of assimilator is already running\n", MSG_NORMAL);
|
2003-02-27 19:29:48 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-06-20 01:31:03 +00:00
|
|
|
write_pid_file(PIDFILE);
|
2003-02-27 19:29:48 +00:00
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
retval = boinc_db_open(config.db_name, config.db_passwd);
|
2002-12-18 01:34:51 +00:00
|
|
|
if (retval) {
|
2003-06-11 23:36:40 +00:00
|
|
|
write_log("Can't open DB\n", MSG_CRITICAL);
|
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) {
|
2003-06-11 23:36:40 +00:00
|
|
|
write_log("Can't find app\n", MSG_CRITICAL);
|
2002-12-18 01:34:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-06-20 01:31:03 +00:00
|
|
|
install_sigint_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) {
|
2002-12-18 01:34:51 +00:00
|
|
|
if (!do_pass(app)) sleep(10);
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|