Example #1
0
	/*
	 *   Do clean-up and notifications in the main thread
	 */
	static bool worker_post(gpointer _wj)
	{
		WorkerJob* wj = _wj;
		QueueItem* job = wj->job;
		WfWorker* w = wj->worker;

		if(!job->cancelled){
			Waveform* waveform = g_weak_ref_get(&job->ref);
			call(job->done, waveform, NULL, job->user_data);
			if(waveform) g_object_unref(waveform);
		}
		if(job->free && job->user_data){
			job->free(job->user_data);
			job->user_data = NULL;
		}

		w->jobs = g_list_remove(w->jobs, job);

		g_weak_ref_set(&job->ref, NULL);
		g_free(job);
		g_free(wj);

		return G_SOURCE_REMOVE;
	}
Example #2
0
/*
 * recv_writer_queue - return the number of queued items
 */
static int
recv_writer_queue(void)
{
	PGconn	   *conn;
	List	   *queue;
	int			ret;
	char	   *instid = NULL;
	bool		connection_used = false;

	pthread_mutex_lock(&writer_queue_lock);
	queue = writer_queue;
	writer_queue = NIL;
	pthread_mutex_unlock(&writer_queue_lock);

	if (list_length(queue) == 0)
		return 0;

	/* install writer schema */
	if ((conn = writer_connect(superuser_connect)) == NULL)
	{
		elog(ERROR, "could not connect to repository");

		/* discard current queue if can't connect to repository */
		if (list_length(queue) > 0)
		{
			elog(WARNING, "writer discards %d items", list_length(queue));
			list_destroy(queue, destroy_writer_queue);
		}
		return 0;
	}

	/* do the writer queue process */
	if ((instid = get_instid(conn)) != NULL)
	{
		connection_used = true;

		while (list_length(queue) > 0)
		{
			QueueItem  *item = (QueueItem *) linitial(queue);

			if (!item->exec(item, conn, instid))
			{
				if (++item->retry < DB_MAX_RETRY)
					break;	/* retry the item */

				/*
				 * discard if the retry count is exceeded to avoid infinite
				 * loops at one bad item.
				 */
				elog(WARNING, "writer discard an item");
			}

			item->free(item);
			queue = list_delete_first(queue);
		}
	}
	free(instid);

	/* delay on error */
	if (list_length(queue) > 0)
		delay();

	/*
	 * When we have failed items, we concatenate to the head of writer queue
	 * and retry them in the next cycle.
	 */
	pthread_mutex_lock(&writer_queue_lock);
	writer_queue = list_concat(queue, writer_queue);
	ret = list_length(writer_queue);
	pthread_mutex_unlock(&writer_queue_lock);

	/* update last used time of the connection. */
	if (connection_used)
		writer_conn_last_used = time(NULL);

	return ret;
}