Skip to content

Commit

Permalink
refactor: use a single database connection (Cloudbox#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
l3uddz authored Jan 14, 2021
1 parent d3221a3 commit 9552298
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 76 deletions.
28 changes: 17 additions & 11 deletions cmd/autoscan/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"database/sql"
"errors"
"fmt"
"io"
Expand All @@ -26,6 +27,9 @@ import (
"github.com/cloudbox/autoscan/triggers/manual"
"github.com/cloudbox/autoscan/triggers/radarr"
"github.com/cloudbox/autoscan/triggers/sonarr"

// sqlite3 driver
_ "github.com/mattn/go-sqlite3"
)

type config struct {
Expand Down Expand Up @@ -90,8 +94,6 @@ func (v versionFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error {
return nil
}

/* Version */

func main() {
// parse cli
ctx := kong.Parse(&cli,
Expand Down Expand Up @@ -138,6 +140,15 @@ func main() {
log.Logger = logger.Level(zerolog.InfoLevel)
}

// datastore
db, err := sql.Open("sqlite3", cli.Database)
if err != nil {
log.Fatal().
Err(err).
Msg("Failed opening datastore")
}
db.SetMaxOpenConns(1)

// run
mux := http.NewServeMux()

Expand Down Expand Up @@ -166,10 +177,9 @@ func main() {
}

proc, err := processor.New(processor.Config{
Anchors: c.Anchors,
DatastorePath: cli.Database,
MinimumAge: c.MinimumAge,
})
Anchors: c.Anchors,
MinimumAge: c.MinimumAge,
}, db)

if err != nil {
log.Fatal().
Expand All @@ -191,11 +201,7 @@ func main() {

// Daemon Triggers
for _, t := range c.Triggers.Bernard {
if t.DatastorePath == "" {
t.DatastorePath = cli.Database
}

trigger, err := bernard.New(t)
trigger, err := bernard.New(t, db)
if err != nil {
log.Fatal().
Err(err).
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ go 1.14

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/alecthomas/kong v0.2.11
github.com/alecthomas/kong v0.2.12
github.com/fsnotify/fsnotify v1.4.9
github.com/justinas/alice v1.2.0
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
github.com/m-rots/bernard v0.3.3-0.20200804121414-38394a889536
github.com/m-rots/bernard v0.3.4
github.com/m-rots/stubbs v1.1.0
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pkg/errors v0.9.1 // indirect
github.com/robfig/cron/v3 v3.0.1
github.com/rs/zerolog v1.19.0
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
github.com/rs/zerolog v1.20.0
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
golang.org/x/sys v0.0.0-20210112091331-59c308dcf3cc
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.3.0
gopkg.in/yaml.v2 v2.4.0
)
26 changes: 14 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/alecthomas/kong v0.2.9/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE=
github.com/alecthomas/kong v0.2.11 h1:RKeJXXWfg9N47RYfMm0+igkxBCTF4bzbneAxaqid0c4=
github.com/alecthomas/kong v0.2.11/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE=
github.com/alecthomas/kong v0.2.12 h1:X3kkCOXGUNzLmiu+nQtoxWqj4U2a39MpSJR3QdQXOwI=
github.com/alecthomas/kong v0.2.12/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -12,8 +14,8 @@ github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo=
github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=
github.com/m-rots/bernard v0.3.3-0.20200804121414-38394a889536 h1:0aY4VrX9R+Y0VArW9EyeZF5JJws4w6aLf7V/YdVHOTQ=
github.com/m-rots/bernard v0.3.3-0.20200804121414-38394a889536/go.mod h1:yDQffALXQDh6sTXdFCbI2rJtYuXgx41MyJM6Sf/j7Sc=
github.com/m-rots/bernard v0.3.4 h1:QTip7rcepstmNFJs86RCbeymHgaJS1BJdExZUct5fEg=
github.com/m-rots/bernard v0.3.4/go.mod h1:yDQffALXQDh6sTXdFCbI2rJtYuXgx41MyJM6Sf/j7Sc=
github.com/m-rots/stubbs v1.0.0 h1:lBrjn27J32/iGHp7eKPYGcphuqDIg5UIs/YI4q1m63Q=
github.com/m-rots/stubbs v1.0.0/go.mod h1:iDS6z2oonw2UMo2l0S1WTPJ9git7FWU4YEo6fq7F2WU=
github.com/m-rots/stubbs v1.1.0 h1:QR1LHxFYPasju/sEO0KLmI5/RADF70CW3ZtisCs7XrQ=
Expand All @@ -32,27 +34,27 @@ github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.19.0 h1:hYz4ZVdUgjXTBUmrkrw55j1nHx68LfOKIQk5IYtyScg=
github.com/rs/zerolog v1.19.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo=
github.com/rs/zerolog v1.20.0 h1:38k9hgtUBdxFwE34yS8rTHmHBa4eN16E4DJlv177LNs=
github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c h1:UIcGWL6/wpCfyGuJnRFJRurA+yj8RrW7Q6x2YMCXt6c=
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210112091331-59c308dcf3cc h1:y0Og6AYdwus7SIAnKnDxjc4gJetRiYEWOx4AKbOeyEI=
golang.org/x/sys v0.0.0-20210112091331-59c308dcf3cc/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e h1:EHBhcS0mlXEAVwNyO2dLfjToGsyY4j24pTs2ScHnX7s=
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 h1:Hir2P/De0WpUhtrKGGjvSb2YxUgyZ7EFOSLIcSSpiwE=
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
13 changes: 3 additions & 10 deletions processor/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,10 @@ CREATE TABLE IF NOT EXISTS scan (
)
`

func newDatastore(path string) (*datastore, error) {
db, err := sql.Open("sqlite3", fmt.Sprintf("%s?%s", path, "cache=shared&mode=rwc&_busy_timeout=5000"))
func newDatastore(db *sql.DB) (*datastore, error) {
_, err := db.Exec(sqlSchema)
if err != nil {
return nil, err
}

db.SetMaxOpenConns(1)

_, err = db.Exec(sqlSchema)
if err != nil {
return nil, err
return nil, fmt.Errorf("exec schema: %w", err)
}

store := &datastore{db}
Expand Down
39 changes: 21 additions & 18 deletions processor/datastore_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package processor

import (
"database/sql"
"errors"
"reflect"
"testing"
Expand All @@ -26,6 +27,20 @@ func (store *datastore) GetScan(folder string) (autoscan.Scan, error) {
return scan, err
}

func getDatastore(t *testing.T) *datastore {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}

ds, err := newDatastore(db)
if err != nil {
t.Fatal(err)
}

return ds
}

func TestUpsert(t *testing.T) {
type Test struct {
Name string
Expand Down Expand Up @@ -74,12 +89,8 @@ func TestUpsert(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
store, err := newDatastore(":memory:")
if err != nil {
t.Fatal(err)
}

err = store.Upsert(tc.Scans)
store := getDatastore(t)
err := store.Upsert(tc.Scans)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -152,12 +163,8 @@ func TestGetAvailableScan(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
store, err := newDatastore(":memory:")
if err != nil {
t.Fatal(err)
}

err = store.Upsert(tc.GiveScans)
store := getDatastore(t)
err := store.Upsert(tc.GiveScans)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -206,12 +213,8 @@ func TestDelete(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
store, err := newDatastore(":memory:")
if err != nil {
t.Fatal(err)
}

err = store.Upsert(tc.GiveScans)
store := getDatastore(t)
err := store.Upsert(tc.GiveScans)
if err != nil {
t.Fatal(err)
}
Expand Down
10 changes: 5 additions & 5 deletions processor/processor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package processor

import (
"database/sql"
"fmt"
"os"
"time"
Expand All @@ -10,13 +11,12 @@ import (
)

type Config struct {
Anchors []string
DatastorePath string
MinimumAge time.Duration
Anchors []string
MinimumAge time.Duration
}

func New(c Config) (*Processor, error) {
store, err := newDatastore(c.DatastorePath)
func New(c Config, db *sql.DB) (*Processor, error) {
store, err := newDatastore(db)
if err != nil {
return nil, err
}
Expand Down
25 changes: 12 additions & 13 deletions triggers/bernard/bernard.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bernard

import (
"database/sql"
"errors"
"fmt"
"path/filepath"
Expand All @@ -21,16 +22,15 @@ const (
)

type Config struct {
AccountPath string `yaml:"account"`
CronSchedule string `yaml:"cron"`
DatastorePath string `yaml:"database"`
Priority int `yaml:"priority"`
TimeOffset time.Duration `yaml:"time-offset"`
Verbosity string `yaml:"verbosity"`
Rewrite []autoscan.Rewrite `yaml:"rewrite"`
Include []string `yaml:"include"`
Exclude []string `yaml:"exclude"`
Drives []struct {
AccountPath string `yaml:"account"`
CronSchedule string `yaml:"cron"`
Priority int `yaml:"priority"`
TimeOffset time.Duration `yaml:"time-offset"`
Verbosity string `yaml:"verbosity"`
Rewrite []autoscan.Rewrite `yaml:"rewrite"`
Include []string `yaml:"include"`
Exclude []string `yaml:"exclude"`
Drives []struct {
ID string `yaml:"id"`
TimeOffset time.Duration `yaml:"time-offset"`
Rewrite []autoscan.Rewrite `yaml:"rewrite"`
Expand All @@ -39,7 +39,7 @@ type Config struct {
} `yaml:"drives"`
}

func New(c Config) (autoscan.Trigger, error) {
func New(c Config, db *sql.DB) (autoscan.Trigger, error) {
l := autoscan.GetLogger(c.Verbosity).With().
Str("trigger", "bernard").
Logger()
Expand All @@ -50,11 +50,10 @@ func New(c Config) (autoscan.Trigger, error) {
return nil, fmt.Errorf("%v: %w", err, autoscan.ErrFatal)
}

store, err := sqlite.New(fmt.Sprintf("%s?%s", c.DatastorePath, "cache=shared&mode=rwc&_busy_timeout=5000"))
store, err := sqlite.FromDB(db)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, autoscan.ErrFatal)
}
store.DB.SetMaxOpenConns(1)

limiter, err := getRateLimiter(auth.Email())
if err != nil {
Expand Down

0 comments on commit 9552298

Please sign in to comment.