mirror of
https://github.com/WCBROW01/zblock.git
synced 2025-12-11 20:18:07 -05:00
[main.c] Throw feed retrieval into a thread so it won't hold up the event loop
This commit is contained in:
29
main.c
29
main.c
@ -8,6 +8,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
|
|
||||||
@ -63,23 +64,29 @@ typedef struct {
|
|||||||
static PGconn *database_conn;
|
static PGconn *database_conn;
|
||||||
|
|
||||||
// this does not account for large-scale usage yet.
|
// this does not account for large-scale usage yet.
|
||||||
static void timer_retrieve_feeds(struct discord *client, struct discord_timer *timer) {
|
static void *thread_retrieve_feeds(void *arg) {
|
||||||
// not doing anything with the timer yet
|
struct discord *client = arg;
|
||||||
(void) timer;
|
|
||||||
|
|
||||||
// all of this is as asynchronous as I can reasonably make it
|
// all of this is as asynchronous as I can reasonably make it
|
||||||
CURLM *multi = curl_multi_init();
|
CURLM *multi = curl_multi_init();
|
||||||
if (!multi) {
|
if (!multi) {
|
||||||
// oh no
|
// oh no
|
||||||
log_error("Unable to retrieve feed list: NULL pointer from curl_multi_init()");
|
log_error("Unable to retrieve feed list: NULL pointer from curl_multi_init()");
|
||||||
return;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PGconn *database_conn = PQconnectdb(zblock_config.conninfo); // yes i know this name is reused
|
||||||
|
if (!database_conn) {
|
||||||
|
log_error("Failed to connect to database.");
|
||||||
|
curl_multi_cleanup(multi);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// Begin retrieval of feed list objects.
|
// Begin retrieval of feed list objects.
|
||||||
if (zblock_feed_info_retrieve_list_begin(database_conn)) {
|
if (zblock_feed_info_retrieve_list_begin(database_conn)) {
|
||||||
log_error("Unable to retrieve feed list: %s", PQerrorMessage(database_conn));
|
log_error("Unable to retrieve feed list: %s", PQerrorMessage(database_conn));
|
||||||
curl_multi_cleanup(multi);
|
curl_multi_cleanup(multi);
|
||||||
return;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// put running handles up here so we can start transfers now instead of later
|
// put running handles up here so we can start transfers now instead of later
|
||||||
@ -199,7 +206,17 @@ static void timer_retrieve_feeds(struct discord *client, struct discord_timer *t
|
|||||||
|
|
||||||
// processing is done
|
// processing is done
|
||||||
curl_multi_cleanup(multi);
|
curl_multi_cleanup(multi);
|
||||||
|
PQfinish(database_conn);
|
||||||
log_info("Retrieved %d of %d feeds!", successful_feeds, total_feeds);
|
log_info("Retrieved %d of %d feeds!", successful_feeds, total_feeds);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void timer_retrieve_feeds(struct discord *client, struct discord_timer *timer) {
|
||||||
|
// not doing anything with the timer
|
||||||
|
(void) timer;
|
||||||
|
|
||||||
|
pthread_t retrieve_thread;
|
||||||
|
pthread_create(&retrieve_thread, NULL, &thread_retrieve_feeds, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void bot_command_add(struct discord *client, const struct discord_interaction *event) {
|
static void bot_command_add(struct discord *client, const struct discord_interaction *event) {
|
||||||
|
|||||||
Reference in New Issue
Block a user