/**
 * Check the service still exists and start a worker that will
 * just perform a TCP-connect test.
 */
static int
_check_tcp_service_task(gpointer udata, GError **error)
{
	struct service_info_s *si;
	struct namespace_data_s *ns_data;
	struct taskdata_checksrv_s *task_data;
	
	task_data = udata;

	ns_data = g_hash_table_lookup(namespaces, task_data->ns_name);
	if (!ns_data) {
		task_done(task_data->task_name);
		GSETERROR(error, "Namespace unavailable");
		return 0;
	}

	/* if the service does not exists, the task itself is de-scheduled */
	if (!(si=g_hash_table_lookup(ns_data->local_services, task_data->srv_key))
	    && !(si=g_hash_table_lookup(ns_data->down_services, task_data->srv_key))) {
		task_done(task_data->task_name);
		task_stop(task_data->task_name);
		INFO("Service [%s] does not exist, stopping task [%s]", task_data->srv_key, task_data->task_name);
		return 1;
	}

	/* Now start a worker for this service. The worker has its own session_data,
	 * without hard reference to the task_t or the namespace_data_t */
	do {
		int fd = addrinfo_connect_nopoll(&(si->addr), 1000, error);
		if (0 > fd) {
			GSETERROR(error, "Connection to gridd server failed : (%d) %s",
					errno, strerror(errno));
			return 0;
		}

		sock_set_linger(fd, 1, 0);

		struct workerdata_checksrv_s *wdata = g_malloc0(sizeof(*wdata));
		g_strlcpy(wdata->task_name, task_data->task_name, sizeof(wdata->task_name)-1);
		g_strlcpy(wdata->ns_name, task_data->ns_name, sizeof(wdata->ns_name)-1);
		g_strlcpy(wdata->srv_key, task_data->srv_key, sizeof(wdata->srv_key)-1);

		worker_t *worker = g_malloc0(sizeof(worker_t));
		worker->func = _check_tcp_service_worker_func;
		worker->clean = _check_tcp_service_worker_cleaner;
		worker->timeout.startup = 1000;
		worker->timeout.activity = 1000;
		worker->data.sock_timeout = 1000;
		worker->data.fd = fd;
		worker->data.session = wdata;

		if (!add_fd_to_io_scheduler(worker, EPOLLOUT, error)) {
			_mark_service_state(task_data->ns_name, wdata->srv_key, FALSE);
			task_done(task_data->task_name);
			g_free(worker);
			g_free(wdata);
			GSETERROR(error, "Failed to add socket fd=%d to io_scheduler : %s", fd, strerror(errno));
			return 0;
		}
		
		TRACE("TCP-connect tried to [%s] for [%s] (fd=%d)", task_data->srv_key, task_data->task_name, fd);
	} while (0);

        return 1;
}
Exemple #2
0
gboolean
sock_set_linger_default(int fd)
{
	return sock_set_linger(fd, SOCK_DEFAULT_LINGER_ONOFF,
			SOCK_DEFAULT_LINGER_DELAY);
}