[library] Move library update trigger to new library module

This commit is contained in:
ejurgensen 2017-01-27 23:23:25 +01:00
parent dc84294348
commit 97aa544945
5 changed files with 54 additions and 77 deletions

View File

@ -41,7 +41,7 @@
#include "logger.h"
#include "cache.h"
#include "listener.h"
#include "filescanner.h"
#include "library.h"
#include "misc.h"
#include "db.h"
#include "db_init.h"

View File

@ -21,10 +21,7 @@
# include <config.h>
#endif
#include "library.h"
#include <errno.h>
#include <event2/event.h>
#include <fcntl.h>
#include <limits.h>
#include <pthread.h>
@ -38,6 +35,9 @@
#include <uninorm.h>
#include <unistr.h>
#include <event2/event.h>
#include "library.h"
#include "cache.h"
#include "commands.h"
#include "conffile.h"
@ -45,20 +45,14 @@
#include "library/filescanner.h"
#include "logger.h"
#include "misc.h"
#include "listener.h"
#include "player.h"
static struct commands_base *cmdbase;
static pthread_t tid_library;
struct event_base *evbase_lib;
/* Flag for aborting scan on exit */
static bool scan_exit;
/* Flag for scan in progress */
static bool scanning;
extern struct library_source filescanner;
#ifdef HAVE_SPOTIFY_H
extern struct library_source spotifyscanner;
@ -72,6 +66,19 @@ static struct library_source *sources[] = {
NULL
};
/* Flag for aborting scan on exit */
static bool scan_exit;
/* Flag for scan in progress */
static bool scanning;
// After being told by db that the library was updated through update_trigger(),
// wait 60 seconds before notifying listeners of LISTENER_DATABASE. This is to
// avoid bombarding the listeners while there are many db updates, and to make
// sure they only get a single update (useful for the cache).
static struct timeval library_update_wait = { 60, 0 };
static struct event *updateev;
static void
sort_tag_create(char **sort_tag, char *src_tag)
@ -630,6 +637,24 @@ fullrescan(void *arg, int *ret)
return COMMAND_END;
}
static void
update_trigger_cb(int fd, short what, void *arg)
{
listener_notify(LISTENER_DATABASE);
}
static enum command_state
update_trigger(void *arg, int *retval)
{
evtimer_add(updateev, &library_update_wait);
*retval = 0;
return COMMAND_END;
}
/* --------------------------- LIBRARY INTERFACE -------------------------- */
void
library_rescan()
{
@ -692,6 +717,8 @@ initscan()
DPRINTF(E_LOG, L_LIB, "Library init scan completed in %.f sec\n", difftime(endtime, starttime));
scanning = false;
listener_notify(LISTENER_DATABASE);
}
/*
@ -721,6 +748,15 @@ library_is_exiting()
return scan_exit;
}
void
library_update_trigger(void)
{
if (scanning)
return;
commands_exec_async(cmdbase, update_trigger, NULL);
}
/*
* Execute the function 'func' with the given argument 'arg' in the library thread.
*
@ -786,13 +822,8 @@ library_init(void)
scan_exit = false;
scanning = false;
evbase_lib = event_base_new();
if (!evbase_lib)
{
DPRINTF(E_FATAL, L_LIB, "Could not create an event base\n");
return -1;
}
CHECK_NULL(L_LIB, evbase_lib = event_base_new());
CHECK_NULL(L_LIB, updateev = evtimer_new(evbase_lib, update_trigger_cb, NULL));
for (i = 0; sources[i]; i++)
{
@ -804,15 +835,9 @@ library_init(void)
sources[i]->disabled = 1;
}
cmdbase = commands_base_new(evbase_lib, NULL);
CHECK_NULL(L_LIB, cmdbase = commands_base_new(evbase_lib, NULL));
ret = pthread_create(&tid_library, NULL, library, NULL);
if (ret != 0)
{
DPRINTF(E_FATAL, L_LIB, "Could not spawn library thread: %s\n", strerror(errno));
goto thread_fail;
}
CHECK_ERR(L_LIB, pthread_create(&tid_library, NULL, library, NULL));
#if defined(HAVE_PTHREAD_SETNAME_NP)
pthread_setname_np(tid_library, "library");
@ -821,11 +846,6 @@ library_init(void)
#endif
return 0;
thread_fail:
event_base_free(evbase_lib);
return -1;
}
/* Thread: main */

View File

@ -86,6 +86,9 @@ library_set_scanning(bool is_scanning);
bool
library_is_exiting();
void
library_update_trigger(void);
int
library_exec_async(command_function func, void *arg);

View File

@ -61,7 +61,6 @@
#include "player.h"
#include "cache.h"
#include "artwork.h"
#include "listener.h"
#include "commands.h"
#include "library.h"
@ -108,16 +107,9 @@ struct stacked_dir {
static int inofd;
static struct event *inoev;
static struct event *updateev;
static struct deferred_pl *playlists;
static struct stacked_dir *dirstack;
// After being told by db that the library was updated through update_trigger(),
// wait 60 seconds before notifying listeners of LISTENER_DATABASE. This is to
// avoid bombarding the listeners while there are many db updates, and to make
// sure they only get a single update (useful for the cache).
static struct timeval library_update_wait = { 60, 0 };
/* From library.c */
extern struct event_base *evbase_lib;
@ -848,8 +840,6 @@ bulk_scan(int flags)
{
DPRINTF(E_LOG, L_SCAN, "Bulk library scan completed in %.f sec\n", difftime(end, start));
}
listener_notify(LISTENER_DATABASE); // TODO Move to library.c
}
static int
@ -1478,21 +1468,6 @@ filescanner_initscan()
return 0;
}
static void
update_trigger_cb(int fd, short what, void *arg)
{
listener_notify(LISTENER_DATABASE);
}
static enum command_state
update_trigger(void *arg, int *retval)
{
evtimer_add(updateev, &library_update_wait);
*retval = 0;
return COMMAND_END;
}
static int
filescanner_rescan()
{
@ -1518,16 +1493,6 @@ filescanner_fullrescan()
return 0;
}
// TODO Move to abstraction
void
library_update_trigger(void)
{
if (scanning)
return;
commands_exec_async(cmdbase, update_trigger, NULL);
}
/* Thread: main */
static int
filescanner_init(void)
@ -1540,14 +1505,6 @@ filescanner_init(void)
return -1;
}
updateev = evtimer_new(evbase_lib, update_trigger_cb, NULL);
if (!updateev)
{
DPRINTF(E_FATAL, L_SCAN, "Could not create library update event\n");
close(inofd);
return -1;
}
return 0;
}

View File

@ -31,7 +31,4 @@ void
scan_itunes_itml(char *file);
#endif
void
library_update_trigger(void); // TODO Move to library abstraction
#endif /* !__FILESCANNER_H__ */