int main() { struct job Ajob(5), Bjob(6); struct queue *Queue; //Ajob.j_id = 5; //Ajob->j_prev = Ajob->j_next =NULL; //Bjob.j_id = 6; //Bjob->j_prev = Bjob->j_next =NULL; queue_init(Queue); job_insert(Queue, &Ajob); job_insert(Queue, &Bjob); job_find(Queue, 6); }
void cron_remove(unsigned int id) { List *job, **list; if((job = job_find(id, &list)) != NULL) { Job *j = list_data(job); *list = list_unlink(*list, job); printf("removing id %u, data='%s'\n", j->id, (const char *) j->data); cron_info.id_reuse = list_prepend(cron_info.id_reuse, (void *) PERIODIC_CLR(j->id)); memchunk_free(cron_info.chunk_job, list_data(job)); list_destroy(job); } }
void cron_set(unsigned int id, double seconds, int (*handler)(void *), void *data) { List *j; if(id == 0 || seconds <= 0.0 || handler == NULL) return; if((j = job_find(id, NULL)) != NULL) { Job *job = list_data(j); if(IS_PERIODIC(id)) job->when.periodic.period = seconds; else fprintf(stderr, "cron: Don't know how to set() non-periodic job\n"); } }
/* process xml response */ static int __process_node_xml_response(xmlDoc * doc, xmlNode * node, int ent_id) { logdebug(_("node response for entity %d\n"), ent_id); char * str = (char *) xmlGetProp(node, (const xmlChar *) "id"); if (str) logdebug("node response id(job id) = %s\n", str); else { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } int id = atoi(str); free(str); str = (char *) xmlGetProp(node, (const xmlChar *) "status"); if (str) logdebug("node response status = %s\n", str); else { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } int status = atoi(str); free(str); int data_type = -1; node = node->children; for (; node; node = node->next) { if (node->type == XML_ELEMENT_NODE) { if (strcmp((char *)node->name, "result") == 0) { xmlNode * n = node->children; if (n && n->type == XML_TEXT_NODE && n->content) loginfo(_("response result: %s\n"), n->content); else loginfo(_("response no result message\n")); } else if (strcmp((char *)node->name, "data") == 0) { str = (char *) xmlGetProp(node, (const xmlChar *) "type"); if (str) { logdebug("node response data type = %s\n", str); data_type = atoi(str); free(str); } else logwarn(_("response data no type\n")); } } } if (id != 0) { LYJobInfo * job = job_find(id); if (job == NULL) { logwarn(_("job(%d) not found waiting for node reply\n"), id); return 0; } if (job_update_status(job, status)) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } /* hack to release entity during instance shutting down */ if (((job->j_action == LY_A_NODE_STOP_INSTANCE) || (job->j_action == LY_A_NODE_FULLREBOOT_INSTANCE) || (job->j_action == LY_A_NODE_ACPIREBOOT_INSTANCE)) && status == LY_S_RUNNING_STOPPED) { int ins_id = ly_entity_find_by_db(LY_ENTITY_OSM, job->j_target_id); if (ins_id > 0) ly_entity_release(ins_id); } /* continue processing for data */ } int ent_type = ly_entity_type(ent_id); if (ent_type == LY_ENTITY_NODE && data_type == DATA_INSTANCE_INFO) { if ( __instance_info_update(doc, node)) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } } if (ent_type == LY_ENTITY_NODE && data_type == DATA_NODE_INFO) { if ( __node_info_update(doc, node, ent_id, &status)) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } } return 0; }
static void client_old_parse_messages(GebrCommProtocolSocket * socket, struct client *client) { GList *link; struct gebr_comm_message *message; while ((link = g_list_last(client->socket->protocol->messages)) != NULL) { message = (struct gebr_comm_message *)link->data; /* check login */ if (message->hash == gebr_comm_protocol_defs.ini_def.code_hash) { GList *arguments; GString *accounts_list = g_string_new(""); GString *queue_list = g_string_new(""); GString *display_port = g_string_new(""); /* organize message data */ if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 2)) == NULL) goto err; GString *version = g_list_nth_data(arguments, 0); GString *hostname = g_list_nth_data(arguments, 1); g_debug("Current protocol version is: %s", gebr_comm_protocol_get_version()); g_debug("Received protocol version: %s", version->str); if (strcmp(version->str, gebr_comm_protocol_get_version())) { gebr_comm_protocol_socket_oldmsg_send(client->socket, TRUE, gebr_comm_protocol_defs.err_def, 2, "protocol", gebr_comm_protocol_get_version()); goto err; } /* set client info */ client->socket->protocol->logged = TRUE; g_string_assign(client->socket->protocol->hostname, hostname->str); client->server_location = GEBR_COMM_SERVER_LOCATION_REMOTE; const gchar *server_type; if (gebrd_get_server_type() == GEBR_COMM_SERVER_TYPE_MOAB) { /* Get info from the MOAB cluster */ server_moab_read_credentials(accounts_list, queue_list); server_type = "moab"; } else server_type = "regular"; GString *mpi_flavors = g_string_new(""); g_list_foreach(gebrd->mpi_flavors, (GFunc) get_mpi_flavors, mpi_flavors); if (mpi_flavors->len > 0) mpi_flavors = g_string_erase(mpi_flavors, mpi_flavors->len-1, 1); g_debug("------------on daemon, Sending %s", mpi_flavors->str) ; const gchar *model_name; const gchar *cpu_clock; const gchar *total_memory; GebrdCpuInfo *cpuinfo = gebrd_cpu_info_new(); GebrdMemInfo *meminfo = gebrd_mem_info_new(); model_name = gebrd_cpu_info_get (cpuinfo, 0, "model name"); cpu_clock = gebrd_cpu_info_get (cpuinfo, 0, "cpu MHz"); total_memory = gebrd_mem_info_get (meminfo, "MemTotal"); gchar *ncores = g_strdup_printf("%d", gebrd_cpu_info_n_procs(cpuinfo)); gebr_comm_protocol_socket_oldmsg_send(client->socket, FALSE, gebr_comm_protocol_defs.ret_def, 11, gebrd->hostname, server_type, accounts_list->str, model_name, total_memory, gebrd->fs_lock->str, ncores, cpu_clock, gebrd_user_get_daemon_id(gebrd->user), g_get_home_dir(), mpi_flavors->str); gebrd_cpu_info_free(cpuinfo); gebrd_mem_info_free(meminfo); gebr_comm_protocol_socket_oldmsg_split_free(arguments); g_string_free(accounts_list, TRUE); g_string_free(queue_list, TRUE); g_string_free(display_port, TRUE); g_free(ncores); } else if (message->hash == gebr_comm_protocol_defs.gid_def.code_hash) { GList *arguments; /* organize message data */ if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 3)) == NULL) goto err; GString *gid = g_list_nth_data(arguments, 0); GString *cookie = g_list_nth_data(arguments, 1); GString *disp_str = g_list_nth_data(arguments, 2); guint display; if (g_strcmp0(disp_str->str, "0") == 0) display = gebr_comm_get_available_port(6010); else display = atoi(disp_str->str); display = MAX(display-6000, 0); g_hash_table_insert(gebrd->display_ports, g_strdup(gid->str), GUINT_TO_POINTER(display)); g_debug("Received gid %s with cookie %s", gid->str, cookie->str); if (cookie->len && gebrd_get_server_type() != GEBR_COMM_SERVER_TYPE_MOAB) { gebrd_message(GEBR_LOG_DEBUG, "Authorizing with system(xauth)"); gchar *tmp = g_strdup_printf(":%d", display); if (!run_xauth_command("add", tmp, cookie->str)) display = 0; g_free(tmp); } gchar *display_str = g_strdup_printf("%d", display ? display + 6000 : 0); gebrd_message(GEBR_LOG_INFO, "Sending port %s to client %s!", display_str, gid->str); gebr_comm_protocol_socket_oldmsg_send(client->socket, FALSE, gebr_comm_protocol_defs.ret_def, 2, gid->str, display_str); g_free(display_str); gebr_comm_protocol_socket_oldmsg_split_free(arguments); } else if (client->socket->protocol->logged == FALSE) { /* not logged! */ goto err; } else if (message->hash == gebr_comm_protocol_defs.qut_def.code_hash) { client_free(client); gebr_comm_message_free(message); return; } else if (message->hash == gebr_comm_protocol_defs.lst_def.code_hash) { job_list(client); } else if (message->hash == gebr_comm_protocol_defs.run_def.code_hash) { GList *arguments; GebrdJob *job; /* organize message data */ if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 9)) == NULL) goto err; GString *gid = g_list_nth_data(arguments, 0); GString *id = g_list_nth_data(arguments, 1); GString *frac = g_list_nth_data(arguments, 2); GString *numproc = g_list_nth_data(arguments, 3); GString *nice = g_list_nth_data(arguments, 4); GString *flow_xml = g_list_nth_data(arguments, 5); GString *paths = g_list_nth_data(arguments, 6); /* Moab & MPI settings */ GString *account = g_list_nth_data(arguments, 7); GString *servers_mpi = g_list_nth_data(arguments, 8); g_debug("SERVERS MPI %s", servers_mpi->str); /* try to run and send return */ job_new(&job, client, gid, id, frac, numproc, nice, flow_xml, account, paths, servers_mpi); #ifdef DEBUG gchar *env_delay = getenv("GEBRD_RUN_DELAY_SEC"); if (env_delay != NULL) sleep(atoi(env_delay)); #endif if (gebrd_get_server_type() == GEBR_COMM_SERVER_TYPE_REGULAR) { /* send job message (job is created -promoted from waiting server response- at the client) */ g_debug("RUN_DEF: run task with rid %s", id->str); job_send_clients_job_notify(job); job_run_flow(job); } else { /* ask moab to run */ job_run_flow(job); /* send job message (job is created -promoted from waiting server response- at the client) * at moab we must run the process before sending the JOB message, because at * job_run_flow moab_jid is acquired. */ job_send_clients_job_notify(job); } /* frees */ gebr_comm_protocol_socket_oldmsg_split_free(arguments); } else if (message->hash == gebr_comm_protocol_defs.rnq_def.code_hash) { } else if (message->hash == gebr_comm_protocol_defs.flw_def.code_hash) { } else if (message->hash == gebr_comm_protocol_defs.clr_def.code_hash) { GList *arguments; GString *rid; GebrdJob *job; /* organize message data */ if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 1)) == NULL) goto err; rid = g_list_nth_data(arguments, 0); job = job_find(rid); if (job != NULL) job_clear(job); /* frees */ gebr_comm_protocol_socket_oldmsg_split_free(arguments); } else if (message->hash == gebr_comm_protocol_defs.end_def.code_hash) { GList *arguments; GString *rid; GebrdJob *job; /* organize message data */ if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 1)) == NULL) goto err; rid = g_list_nth_data(arguments, 0); /* try to run and send return */ job = job_find(rid); if (job != NULL) { job_end(job); } /* frees */ gebr_comm_protocol_socket_oldmsg_split_free(arguments); } else if (message->hash == gebr_comm_protocol_defs.kil_def.code_hash) { GList *arguments; GString *rid; GebrdJob *job; /* organize message data */ if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 1)) == NULL) goto err; rid = g_list_nth_data(arguments, 0); /* try to run and send return */ job = job_find(rid); if (job != NULL) { job_kill(job); } /* frees */ gebr_comm_protocol_socket_oldmsg_split_free(arguments); } else if (message->hash == gebr_comm_protocol_defs.path_def.code_hash) { GList *arguments; if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 3)) == NULL) goto err; GString *new_path = g_list_nth_data(arguments, 0); GString *old_path = g_list_nth_data(arguments, 1); GString *opt = g_list_nth_data(arguments, 2); g_debug("new_path:%s, old_path:%s, opt:%s", new_path->str, old_path->str, opt->str); GList *new_paths= parse_comma_separated_string(new_path->str); gint option = gebr_comm_protocol_path_str_to_enum(opt->str); gint status_id = -1; gboolean flag_exists = FALSE; gboolean flag_error = FALSE; switch (option) { case GEBR_COMM_PROTOCOL_PATH_CREATE: for (GList *j = new_paths; j; j = j->next) { GString *path = j->data; if (g_file_test(path->str, G_FILE_TEST_IS_DIR)){ flag_exists = TRUE; } else if (*(path->str) && g_mkdir_with_parents(path->str, 0700)) { flag_error = TRUE; break; } if (g_access(path->str, W_OK)==-1){ flag_error = TRUE; break; } } if (flag_error) status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_ERROR; else if (flag_exists) status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_EXISTS; else status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_OK; break; case GEBR_COMM_PROTOCOL_PATH_RENAME: g_debug("Renaming %s to %s", old_path->str, new_path->str); gboolean dir_exist = g_file_test(new_path->str, G_FILE_TEST_IS_DIR); gboolean create_dir = g_rename(old_path->str, new_path->str) == 0; if (!dir_exist && create_dir) status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_OK; else if (dir_exist && !create_dir) status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_EXISTS; else if (!dir_exist && !create_dir) status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_ERROR; else status_id = -1; break; case GEBR_COMM_PROTOCOL_PATH_DELETE: for (GList *j = new_paths; j; j = j->next) { GString *path = j->data; if (g_rmdir(path->str)) status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_ERROR; else status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_OK; } break; default: g_warn_if_reached(); break; } g_debug("on %s, new_path:'%s', old_path:'%s', status_id: '%d'", __func__, new_path->str, old_path->str, status_id); /* frees */ gebr_comm_protocol_socket_oldmsg_split_free(arguments); gebr_comm_protocol_socket_oldmsg_send(socket, FALSE, gebr_comm_protocol_defs.ret_def, 2, gebrd->hostname, g_strdup_printf("%d", status_id)); } else if (message->hash == gebr_comm_protocol_defs.harakiri_def.code_hash) { /* Maestro wants me killed */ g_debug("Harakiri"); gebrd_quit(); } else { /* unknown message! */ goto err; } gebr_comm_message_free(message); client->socket->protocol->messages = g_list_delete_link(client->socket->protocol->messages, link); } return; err: gebr_comm_message_free(message); client->socket->protocol->messages = g_list_delete_link(client->socket->protocol->messages, link); client_disconnected(socket, client); }
// Readrec5 is like readrec, but it reads a record in "version 5" // of the log format. static int readrec5(File *f, job l, int *err) { int r, sz = 0; size_t namelen; Jobrec5 jr; job j; tube t; char tubename[MAX_TUBE_NAME_LEN]; r = read(f->fd, &namelen, sizeof(namelen)); if (r == -1) { twarn("read"); warnpos(f, 0, "error"); *err = 1; return 0; } if (r != sizeof(namelen)) { return 0; } sz += r; if (namelen >= MAX_TUBE_NAME_LEN) { warnpos(f, -r, "namelen %zu exceeds maximum of %d", namelen, MAX_TUBE_NAME_LEN - 1); *err = 1; return 0; } if (namelen) { r = readfull(f, tubename, namelen, err, "v5 tube name"); if (!r) { return 0; } sz += r; } tubename[namelen] = '\0'; r = readfull(f, &jr, Jobrec5size, err, "v5 job struct"); if (!r) { return 0; } sz += r; // are we reading trailing zeroes? if (!jr.id) return 0; j = job_find(jr.id); if (!(j || namelen)) { // We read a short record without having seen a // full record for this job, so the full record // was in an eariler file that has been deleted. // Therefore the job itself has either been // deleted or migrated; either way, this record // should be ignored. return 1; } switch (jr.state) { case Reserved: jr.state = Ready; case Ready: case Buried: case Delayed: if (!j) { if (jr.body_size > job_data_size_limit) { warnpos(f, -r, "job %"PRIu64" is too big (%"PRId32" > %zu)", jr.id, jr.body_size, job_data_size_limit); goto Error; } t = tube_find_or_make(tubename); j = make_job_with_id(jr.pri, jr.delay, jr.ttr, jr.body_size, t, jr.id); j->next = j->prev = j; j->r.created_at = jr.created_at; } j->r.id = jr.id; j->r.pri = jr.pri; j->r.delay = jr.delay * 1000; // us => ns j->r.ttr = jr.ttr * 1000; // us => ns j->r.body_size = jr.body_size; j->r.created_at = jr.created_at * 1000; // us => ns j->r.deadline_at = jr.deadline_at * 1000; // us => ns j->r.reserve_ct = jr.reserve_ct; j->r.timeout_ct = jr.timeout_ct; j->r.release_ct = jr.release_ct; j->r.bury_ct = jr.bury_ct; j->r.kick_ct = jr.kick_ct; j->r.state = jr.state; job_insert(l, j); // full record; read the job body if (namelen) { if (jr.body_size != j->r.body_size) { warnpos(f, -r, "job %"PRIu64" size changed", j->r.id); warnpos(f, -r, "was %"PRId32", now %"PRId32, j->r.body_size, jr.body_size); goto Error; } r = readfull(f, j->body, j->r.body_size, err, "v5 job body"); if (!r) { goto Error; } sz += r; // since this is a full record, we can move // the file pointer and decref the old // file, if any filermjob(j->file, j); fileaddjob(f, j); } j->walused += sz; f->w->alive += sz; return 1; case Invalid: if (j) { job_remove(j); filermjob(j->file, j); job_free(j); } return 1; } Error: *err = 1; if (j) { job_remove(j); filermjob(j->file, j); job_free(j); } return 0; }
/* process xml response */ static int __process_node_xml_response(xmlDoc * doc, xmlNode * node, int ent_id) { loginfo(_("node response for entity %d\n"), ent_id); char * str = (char *) xmlGetProp(node, (const xmlChar *) "id"); if (str) logdebug("node response id(job id) = %s\n", str); else { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } int id = atoi(str); free(str); str = (char *) xmlGetProp(node, (const xmlChar *) "status"); if (str) logdebug("node response status = %s\n", str); else { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } int status = atoi(str); free(str); int data_type = -1; node = node->children; for (; node; node = node->next) { if (node->type == XML_ELEMENT_NODE) { if (strcmp((char *)node->name, "result") == 0) { xmlNode * n = node->children; if (n && n->type == XML_TEXT_NODE && n->content) loginfo(_("response result: %s\n"), n->content); else loginfo(_("response no result message\n")); } else if (strcmp((char *)node->name, "data") == 0) { str = (char *) xmlGetProp(node, (const xmlChar *) "type"); if (str) { logdebug("node response data type = %s\n", str); data_type = atoi(str); free(str); } else logwarn(_("response data no type\n")); } } } if (id != 0) { LYJobInfo * job = job_find(id); if (job == NULL) { logwarn(_("job(%d) not found waiting for node reply\n"), id); return 0; } if (job_update_status(job, status)) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } if (status != LY_S_FINISHED_SUCCESS || data_type < 0) return 0; } int ent_type = ly_entity_type(ent_id); if (ent_type == LY_ENTITY_NODE && data_type == DATA_INSTANCE_INFO) { if ( __instance_info_update(doc, node, ent_id, &status)) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } } if (ent_type == LY_ENTITY_NODE && data_type == DATA_NODE_INFO) { if ( __node_info_update(doc, node, ent_id, &status)) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } } return 0; }