Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
enhancement: refactor and improve process function and webhook endpoi…
Browse files Browse the repository at this point in the history
…nts (#59)

* change from gofunc to regular range loop

to avoid sqlite locks
its probably a bit slower, but should not get a "database is locked" anymore

* Improved summary of errors

* small correction to errMsg

* fixed run commands and improved webhook endpoints

you can now trigger arrs or lists individually via POST
`omegabrr arr` and `omegabrr lists` now only trigger the given type

* update README with the new webhook endpoints

* refactor with zs suggestions

* handle the case where the config is nil

* "Run complete" only if 0 errors

* replace Msgf with Msg for non-formatted log

* optimize func GetFilters by reusing autobrrClient

* add configPath getenv for OMEGABRR_CONFIG

* improve help msg and config search methods
  • Loading branch information
s0up4200 authored May 15, 2023
1 parent 7807fb2 commit 851eda8
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 165 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,11 @@ Run as a service and process on cron schedule. Defaults to every 6 hour `0 */6 *

When run as a service it exposes an HTTP server as well. Generate an **API Token** (see instructions above) and add to your config.

To refresh the filters you can make a **POST** or **GET** request to `http://localhost:7441/api/webhook/trigger`.
To refresh the filters you can make a **POST** or **GET** request to the following:

- `http://localhost:7441/api/webhook/trigger/arr`. - This will trigger all arr filters. (Use this in you arr instances)
- `http://localhost:7441/api/webhook/trigger/lists`. - This will trigger all lists filters.
- `http://localhost:7441/api/webhook/trigger`. - This will trigger all filters.

The API Token can be set as either an HTTP header like `X-API-Token`, or be passed in the url as a query param like `?apikey=MY_NEW_LONG_SECURE_TOKEN`.

Expand Down
102 changes: 86 additions & 16 deletions cmd/omegabrr/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
netHTTP "net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"

Expand All @@ -28,17 +30,30 @@ var (
commit = ""
)

const usage = `omegabrr
Turn your monitored shows from your arrs into autobrr filters, automagically!
const usage = `omegabrr - Automagically turn your monitored titles from your arrs and lists into autobrr filters.
Usage:
omegabrr generate-token Generate API Token Optionally call with --length <number>
omegabrr arr Run omegabrr arr once
omegabrr lists Run omegabrr lists once
omegabrr run Run omegabrr service
omegabrr version Print version info
omegabrr help Show this help message
omegabrr [command] [flags]
Commands:
arr Run omegabrr arr once
lists Run omegabrr lists once
run Run omegabrr service on schedule
generate-token Generate an API Token (optionally call with --length <number>)
version Print version info
help Show this help message
Flags:
-c, --config <path> Path to configuration file (default is $OMEGABRR_CONFIG, or config.yaml in the default user config directory)
--dry-run Dry-run without inserting filters (default false)
--length <number> Length of the generated API token (default 16)
Provide a configuration file using one of the following methods:
1. Use the --config <path> or -c <path> flag.
2. Place a config.yaml file in the default user configuration directory (e.g., ~/.config/omegabrr/).
3. Set the OMEGABRR_CONFIG environment variable.
For more information and examples, visit https://github.com/autobrr/omegabrr
` + "\n"

func init() {
Expand All @@ -51,13 +66,29 @@ func main() {
var configPath string
var dryRun bool

pflag.StringVar(&configPath, "config", "", "path to configuration file")
pflag.StringVarP(&configPath, "config", "c", "", "path to configuration file")
pflag.BoolVar(&dryRun, "dry-run", false, "dry-run without inserting filters")

// Define and parse flags using pflag
length := pflag.Int("length", 16, "length of the generated API token")
pflag.Parse()

if configPath == "" {
configPath = os.Getenv("OMEGABRR_CONFIG")

if configPath == "" {
userConfigDir, err := os.UserConfigDir()
if err != nil {
log.Fatal().Err(err).Msg("failed to get user config directory")
}
defaultConfigPath := filepath.Join(userConfigDir, "omegabrr", "config.yaml")

if _, err := os.Stat(defaultConfigPath); err == nil {
configPath = defaultConfigPath
}
}
}

zerolog.TimeFieldFormat = time.RFC3339
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339})
Expand Down Expand Up @@ -108,17 +139,33 @@ func main() {
cfg := domain.NewConfig(configPath)

p := processor.NewService(cfg)
if err := p.Process(dryRun); err != nil {
log.Error().Err(err).Msgf("error during processing")
ctx := context.Background()
errors := p.ProcessArrs(ctx, dryRun)
if len(errors) == 0 {
log.Info().Msg("Run complete.")
} else {
log.Warn().Msg("Run complete, with errors.")
log.Warn().Msg("Errors encountered during processing:")
for _, err := range errors {
log.Warn().Msg(err)
}
os.Exit(1)
}

case "lists":
cfg := domain.NewConfig(configPath)

p := processor.NewService(cfg)
if err := p.Process(dryRun); err != nil {
log.Error().Err(err).Msgf("error during processing")
ctx := context.Background()
errors := p.ProcessLists(ctx, dryRun)
if len(errors) == 0 {
log.Info().Msg("Run complete.")
} else {
log.Warn().Msg("Run complete, with errors.")
log.Warn().Msg("Errors encountered during processing:")
for _, err := range errors {
log.Warn().Msg(err)
}
os.Exit(1)
}

Expand Down Expand Up @@ -149,9 +196,32 @@ func main() {

time.Sleep(15 * time.Second)

if err := p.Process(false); err != nil {
log.Error().Err(err).Msgf("error during initial processing")
ctx := context.Background()

// Store processing errors for ProcessArrs and ProcessLists
var processingErrors []string

arrsErrors := p.ProcessArrs(ctx, false)
if len(arrsErrors) > 0 {
processingErrors = append(processingErrors, arrsErrors...)
}

listsErrors := p.ProcessLists(ctx, false)
if len(listsErrors) > 0 {
processingErrors = append(processingErrors, listsErrors...)
}

// Print the summary of potential errors
if len(processingErrors) == 0 {
log.Info().Msg("Run complete.")
} else {
log.Warn().Msg("Run complete, with errors.")
log.Warn().Msg("Errors encountered during processing:")
for _, errMsg := range processingErrors {
log.Warn().Msg(errMsg)
}
}

}()

for sig := range sigCh {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/rs/zerolog v1.29.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.0
golang.org/x/sync v0.1.0
golift.io/starr v0.14.0
)

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,6 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
27 changes: 26 additions & 1 deletion internal/http/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,35 @@ func newWebhookHandler(cfg *domain.Config, processorSvc *processor.Service) *web
func (h webhookHandler) Routes(r chi.Router) {
r.Get("/trigger", h.run)
r.Post("/trigger", h.run)
r.Get("/trigger/arr", h.arr)
r.Get("/trigger/lists", h.lists)
r.Post("/trigger/arr", h.arr)
r.Post("/trigger/lists", h.lists)
}

func (h webhookHandler) run(w http.ResponseWriter, r *http.Request) {
if err := h.processorService.Process(false); err != nil {
ctx := r.Context()
errArrs := h.processorService.ProcessArrs(ctx, false)
errLists := h.processorService.ProcessLists(ctx, false)

if errArrs != nil || errLists != nil {
render.NoContent(w, r)
} else {
render.Status(r, http.StatusOK)
}
}

func (h webhookHandler) arr(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if err := h.processorService.ProcessArrs(ctx, false); err != nil {
render.NoContent(w, r)
}
render.Status(r, http.StatusOK)
}

func (h webhookHandler) lists(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if err := h.processorService.ProcessLists(ctx, false); err != nil {
render.NoContent(w, r)
}
render.Status(r, http.StatusOK)
Expand Down
6 changes: 3 additions & 3 deletions internal/processor/lidarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package processor

import (
"context"
"fmt"
"strings"
"time"

"github.com/autobrr/omegabrr/internal/domain"
"github.com/autobrr/omegabrr/pkg/autobrr"
"github.com/pkg/errors"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -47,8 +47,8 @@ func (s Service) lidarr(ctx context.Context, cfg *domain.ArrConfig, dryRun bool,

if !dryRun {
if err := brr.UpdateFilterByID(ctx, filterID, f); err != nil {
l.Error().Err(err).Msgf("something went wrong updating music filter: %v", filterID)
return fmt.Errorf("error updating music filter: %v, %w", filterID, err)
l.Error().Err(err).Msgf("error updating filter: %v", filterID)
return errors.Wrapf(err, "error updating filter: %v", filterID)
}
}

Expand Down
10 changes: 6 additions & 4 deletions internal/processor/mdblist.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/autobrr/omegabrr/internal/domain"
"github.com/autobrr/omegabrr/pkg/autobrr"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"net/http"
"strings"
)

func (s Service) mdblist(ctx context.Context, cfg *domain.ListConfig, dryRun bool, brr *autobrr.Client) error {
Expand Down Expand Up @@ -82,8 +84,8 @@ func (s Service) mdblist(ctx context.Context, cfg *domain.ListConfig, dryRun boo

if !dryRun {
if err := brr.UpdateFilterByID(ctx, filterID, f); err != nil {
l.Error().Err(err).Msgf("something went wrong updating filter: %v", filterID)
return fmt.Errorf("error updating filter: %v, %w", filterID, err)
l.Error().Err(err).Msgf("error updating filter: %v", filterID)
return errors.Wrapf(err, "error updating filter: %v", filterID)
}
}

Expand Down
5 changes: 3 additions & 2 deletions internal/processor/metacritic.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/autobrr/omegabrr/internal/domain"
"github.com/autobrr/omegabrr/pkg/autobrr"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -117,8 +118,8 @@ func (s Service) metacritic(ctx context.Context, cfg *domain.ListConfig, dryRun

if !dryRun {
if err := brr.UpdateFilterByID(ctx, filterID, f); err != nil {
l.Error().Err(err).Msgf("something went wrong updating filter: %v", filterID)
return fmt.Errorf("error updating filter: %v, %w", filterID, err)
l.Error().Err(err).Msgf("error updating filter: %v", filterID)
return errors.Wrapf(err, "error updating filter: %v", filterID)
}
}

Expand Down
5 changes: 3 additions & 2 deletions internal/processor/plaintext.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/autobrr/omegabrr/internal/domain"
"github.com/autobrr/omegabrr/pkg/autobrr"
"github.com/pkg/errors"

"github.com/fatih/color"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -98,8 +99,8 @@ func (s Service) plaintext(ctx context.Context, cfg *domain.ListConfig, dryRun b

if !dryRun {
if err := brr.UpdateFilterByID(ctx, filterID, f); err != nil {
l.Error().Err(err).Msgf("something went wrong updating filter: %v", filterID)
return fmt.Errorf("error updating filter: %v, %w", filterID, err)
l.Error().Err(err).Msgf("error updating filter: %v", filterID)
return errors.Wrapf(err, "error updating filter: %v", filterID)
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/processor/radarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package processor

import (
"context"
"fmt"
"strings"
"time"

"github.com/autobrr/omegabrr/internal/domain"
"github.com/autobrr/omegabrr/pkg/autobrr"
"github.com/pkg/errors"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -47,8 +47,8 @@ func (s Service) radarr(ctx context.Context, cfg *domain.ArrConfig, dryRun bool,

if !dryRun {
if err := brr.UpdateFilterByID(ctx, filterID, f); err != nil {
l.Error().Err(err).Msgf("something went wrong updating movie filter: %v", filterID)
return fmt.Errorf("error updating movie filter: %v, %w", filterID, err)
l.Error().Err(err).Msgf("error updating filter: %v", filterID)
return errors.Wrapf(err, "error updating filter: %v", filterID)
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/processor/readarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package processor

import (
"context"
"fmt"
"strings"
"time"

"github.com/autobrr/omegabrr/internal/domain"
"github.com/autobrr/omegabrr/pkg/autobrr"
"github.com/pkg/errors"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -44,8 +44,8 @@ func (s Service) readarr(ctx context.Context, cfg *domain.ArrConfig, dryRun bool

if !dryRun {
if err := brr.UpdateFilterByID(ctx, filterID, f); err != nil {
l.Error().Err(err).Msgf("something went wrong updating ebook filter: %v", filterID)
return fmt.Errorf("error updating ebook filter: %v, %w", filterID, err)
l.Error().Err(err).Msgf("error updating filter: %v", filterID)
return errors.Wrapf(err, "error updating filter: %v", filterID)
}
}
}
Expand Down
Loading

0 comments on commit 851eda8

Please sign in to comment.