Add utils

This commit is contained in:
Ben Sarmiento
2024-01-28 20:20:33 +01:00
parent 3cf3351210
commit 573cfa1436
6 changed files with 65 additions and 21 deletions

View File

@@ -175,8 +175,8 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<td>%s</td>
</tr>
<tr>
<td>Number of Workers</td>
<td>%d</td>
<td>Workers</td>
<td>%d running / %d free / %d total</td>
</tr>
<tr>
<td>Refresh Every...</td>
@@ -220,7 +220,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
</tr>
<tr>
<td>Network Buffer Size</td>
<td>%d</td>
<td>%d bytes</td>
</tr>
<tr>
<td>Serve From Rclone</td>
@@ -238,6 +238,15 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
<td>On Library Update</td>
<td>%s</td>
</tr>
<tr>
<td>Utilities</td>
<td colspan="2">
<button type="button" onclick="window.open('/logs/upload')">Upload logs</button>
<button type="button" onclick="window.open('/reboot/worker')">Reboot worker pool</button>
<button type="button" onclick="window.open('/reboot/refresh')">Reboot refresh pool</button>
<button type="button" onclick="window.open('/reboot/repair')">Reboot repair pool</button>
</td>
</tr>
</table>
`
out = fmt.Sprintf(out,
@@ -273,7 +282,9 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
strings.Replace(response.Config.Token, response.Config.Token[len(response.Config.Token)-48:], "*****", 1),
response.Config.GetHost(),
response.Config.GetPort(),
response.Config.GetNumOfWorkers(),
zr.workerPool.Running(),
zr.workerPool.Free(),
zr.workerPool.Cap(),
response.Config.GetRefreshEverySecs(),
response.Config.EnableRetainRDTorrentName(),
response.Config.EnableRetainFolderNameExtension(),
@@ -294,6 +305,29 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
fmt.Fprint(resp, out)
}
func (zr *Handlers) handleRebootWorkerPool(resp http.ResponseWriter, req *http.Request) {
// zr.workerPool.Release()
zr.workerPool.Reboot()
zr.log.Infof("Rebooted worker pool")
fmt.Fprint(resp, "Rebooted worker pool, please close this window")
}
func (zr *Handlers) handleRebootRefreshPool(resp http.ResponseWriter, req *http.Request) {
zr.refreshPool.Release()
// zr.refreshPool.Reboot()
// zr.torMgr.StartRefreshJob()
zr.log.Infof("Rebooted refresh pool")
fmt.Fprint(resp, "Rebooted refresh pool, please close this window")
}
func (zr *Handlers) handleRebootRepairPool(resp http.ResponseWriter, req *http.Request) {
zr.repairPool.Release()
// zr.repairPool.Reboot()
// zr.torMgr.StartRepairJob()
zr.log.Infof("Rebooted repair pool")
fmt.Fprint(resp, "Rebooted repair pool, please close this window")
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}

View File

@@ -16,14 +16,18 @@ import (
"github.com/debridmediamanager/zurg/pkg/logutil"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/go-chi/chi/v5"
"github.com/panjf2000/ants/v2"
)
type Handlers struct {
downloader *universal.Downloader
torMgr *torrent.TorrentManager
cfg config.ConfigInterface
api *realdebrid.RealDebrid
log *logutil.Logger
downloader *universal.Downloader
torMgr *torrent.TorrentManager
cfg config.ConfigInterface
api *realdebrid.RealDebrid
workerPool *ants.Pool
refreshPool *ants.Pool
repairPool *ants.Pool
log *logutil.Logger
}
func init() {
@@ -32,13 +36,16 @@ func init() {
chi.RegisterMethod("MOVE")
}
func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *torrent.TorrentManager, cfg config.ConfigInterface, api *realdebrid.RealDebrid, log *logutil.Logger) {
func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *torrent.TorrentManager, cfg config.ConfigInterface, api *realdebrid.RealDebrid, workerPool, refreshPool, repairPool *ants.Pool, log *logutil.Logger) {
hs := &Handlers{
downloader: downloader,
torMgr: torMgr,
cfg: cfg,
api: api,
log: log,
downloader: downloader,
torMgr: torMgr,
cfg: cfg,
api: api,
workerPool: workerPool,
refreshPool: refreshPool,
repairPool: repairPool,
log: log,
}
if cfg.GetUsername() != "" {
@@ -47,6 +54,9 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
router.Use(hs.options)
router.Get("/", hs.handleHome)
router.Get("/reboot/worker", hs.handleRebootWorkerPool)
router.Get("/reboot/refresh", hs.handleRebootRefreshPool)
router.Get("/reboot/repair", hs.handleRebootRepairPool)
// version
router.Get(fmt.Sprintf("/{mountType}/%s", version.FILE), hs.handleVersionFile)
router.Head(fmt.Sprintf("/{mountType}/%s", version.FILE), hs.handleCheckVersionFile)