[add] migrate to percentages vs pages
[add] task runner [fix] calculate word count on upload [remove] unused queries
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -29,35 +30,72 @@ func NewServer() *Server {
|
||||
// Create Paths
|
||||
docDir := filepath.Join(c.DataPath, "documents")
|
||||
coversDir := filepath.Join(c.DataPath, "covers")
|
||||
_ = os.Mkdir(docDir, os.ModePerm)
|
||||
_ = os.Mkdir(coversDir, os.ModePerm)
|
||||
os.Mkdir(docDir, os.ModePerm)
|
||||
os.Mkdir(coversDir, os.ModePerm)
|
||||
|
||||
return &Server{
|
||||
API: api,
|
||||
Config: c,
|
||||
Database: db,
|
||||
httpServer: &http.Server{
|
||||
Handler: api.Router,
|
||||
Addr: (":" + c.ListenPort),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) StartServer() {
|
||||
listenAddr := (":" + s.Config.ListenPort)
|
||||
func (s *Server) StartServer(wg *sync.WaitGroup, done <-chan struct{}) {
|
||||
ticker := time.NewTicker(15 * time.Minute)
|
||||
|
||||
s.httpServer = &http.Server{
|
||||
Handler: s.API.Router,
|
||||
Addr: listenAddr,
|
||||
}
|
||||
wg.Add(2)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
err := s.httpServer.ListenAndServe()
|
||||
if err != nil {
|
||||
log.Error("Error starting server ", err)
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
log.Error("Error Starting Server:", err)
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
defer ticker.Stop()
|
||||
|
||||
s.RunScheduledTasks()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
s.RunScheduledTasks()
|
||||
case <-done:
|
||||
log.Info("Stopping Task Runner...")
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *Server) StopServer() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
s.httpServer.Shutdown(ctx)
|
||||
s.API.DB.DB.Close()
|
||||
func (s *Server) RunScheduledTasks() {
|
||||
log.Info("[RunScheduledTasks] Refreshing Temp Table Cache")
|
||||
if err := s.API.DB.CacheTempTables(); err != nil {
|
||||
log.Warn("[RunScheduledTasks] Refreshing Temp Table Cache Failure:", err)
|
||||
}
|
||||
log.Info("[RunScheduledTasks] Refreshing Temp Table Success")
|
||||
}
|
||||
|
||||
func (s *Server) StopServer(wg *sync.WaitGroup, done chan<- struct{}) {
|
||||
log.Info("Stopping HTTP Server...")
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := s.httpServer.Shutdown(ctx); err != nil {
|
||||
log.Info("Shutting Error")
|
||||
}
|
||||
s.API.DB.Shutdown()
|
||||
|
||||
close(done)
|
||||
wg.Wait()
|
||||
|
||||
log.Info("Server Stopped")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user