Add context
This commit adds and passes context around to (almost) every function. This allows cancelling (almost) every function call globally.
This commit is contained in:
parent
996ecb9f80
commit
3455f491ca
104 changed files with 836 additions and 511 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package triplestore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
|
||||
|
|
@ -10,19 +11,17 @@ import (
|
|||
func (ts *Triplestore) BackupName() string { return "triplestore" }
|
||||
|
||||
// Backup makes a backup of all Triplestore repositories databases into the path dest.
|
||||
func (ts *Triplestore) Backup(context component.StagingContext) error {
|
||||
func (ts *Triplestore) Backup(scontext component.StagingContext) error {
|
||||
return scontext.AddDirectory("", func(ctx context.Context) error {
|
||||
// list all the directories
|
||||
repos, err := ts.listRepositories(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// list all the directories
|
||||
repos, err := ts.listRepositories()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// then backup each file separatly
|
||||
return context.AddDirectory("", func() error {
|
||||
for _, repo := range repos {
|
||||
if err := context.AddFile(repo.ID+".nq", func(file io.Writer) error {
|
||||
_, err := ts.SnapshotDB(file, repo.ID)
|
||||
if err := scontext.AddFile(repo.ID+".nq", func(ctx context.Context, file io.Writer) error {
|
||||
_, err := ts.SnapshotDB(ctx, file, repo.ID)
|
||||
return err
|
||||
}); err != nil {
|
||||
return err
|
||||
|
|
@ -32,8 +31,8 @@ func (ts *Triplestore) Backup(context component.StagingContext) error {
|
|||
})
|
||||
}
|
||||
|
||||
func (ts Triplestore) listRepositories() (repos []Repository, err error) {
|
||||
res, err := ts.OpenRaw("GET", "/rest/repositories", nil, "", "application/json")
|
||||
func (ts Triplestore) listRepositories(ctx context.Context) (repos []Repository, err error) {
|
||||
res, err := ts.OpenRaw(ctx, "GET", "/rest/repositories", nil, "", "application/json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package triplestore
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
|
|
@ -30,7 +31,7 @@ type TriplestoreUserAppSettings struct {
|
|||
//
|
||||
// When bodyName is non-empty, expect body to be a byte slice representing a multipart/form-data upload with the given name.
|
||||
// When bodyName is empty, simply marshal body as application/json
|
||||
func (ts Triplestore) OpenRaw(method, url string, body interface{}, bodyName string, accept string) (*http.Response, error) {
|
||||
func (ts Triplestore) OpenRaw(ctx context.Context, method, url string, body interface{}, bodyName string, accept string) (*http.Response, error) {
|
||||
var reader io.Reader
|
||||
|
||||
var contentType string
|
||||
|
|
@ -66,7 +67,7 @@ func (ts Triplestore) OpenRaw(method, url string, body interface{}, bodyName str
|
|||
DisableKeepAlives: true,
|
||||
},
|
||||
}
|
||||
req, err := http.NewRequest(method, ts.BaseURL+url, reader)
|
||||
req, err := http.NewRequestWithContext(ctx, method, ts.BaseURL+url, reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -86,23 +87,23 @@ func (ts Triplestore) OpenRaw(method, url string, body interface{}, bodyName str
|
|||
|
||||
// Wait waits for the connection to the Triplestore to succeed.
|
||||
// This is achieved using a polling strategy.
|
||||
func (ts Triplestore) Wait() error {
|
||||
func (ts Triplestore) Wait(ctx context.Context) error {
|
||||
n := stream.FromNil()
|
||||
return timex.TickUntilFunc(func(time.Time) bool {
|
||||
res, err := ts.OpenRaw("GET", "/rest/repositories", nil, "", "")
|
||||
res, err := ts.OpenRaw(ctx, "GET", "/rest/repositories", nil, "", "")
|
||||
n.EPrintf("[Triplestore.Wait]: %s\n", err)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer res.Body.Close()
|
||||
return true
|
||||
}, ts.PollContext, ts.PollInterval)
|
||||
}, ctx, ts.PollInterval)
|
||||
}
|
||||
|
||||
// PurgeUser deletes the specified user from the triplestore.
|
||||
// When the user does not exist, returns no error.
|
||||
func (ts Triplestore) PurgeUser(user string) error {
|
||||
res, err := ts.OpenRaw("DELETE", "/rest/security/users/"+user, nil, "", "")
|
||||
func (ts Triplestore) PurgeUser(ctx context.Context, user string) error {
|
||||
res, err := ts.OpenRaw(ctx, "DELETE", "/rest/security/users/"+user, nil, "", "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -114,8 +115,8 @@ func (ts Triplestore) PurgeUser(user string) error {
|
|||
|
||||
// PurgeRepo deletes the specified repo from the triplestore.
|
||||
// When the repo does not exist, returns no error.
|
||||
func (ts Triplestore) PurgeRepo(repo string) error {
|
||||
res, err := ts.OpenRaw("DELETE", "/rest/repositories/"+repo, nil, "", "")
|
||||
func (ts Triplestore) PurgeRepo(ctx context.Context, repo string) error {
|
||||
res, err := ts.OpenRaw(ctx, "DELETE", "/rest/repositories/"+repo, nil, "", "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package triplestore
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
_ "embed"
|
||||
|
|
@ -20,19 +21,19 @@ var errTripleStoreFailedRepository = exit.Error{
|
|||
//go:embed create-repo.ttl
|
||||
var createRepoTTL []byte
|
||||
|
||||
func (ts *Triplestore) Provision(instance models.Instance, domain string) error {
|
||||
return ts.CreateRepository(instance.GraphDBRepository, domain, instance.GraphDBUsername, instance.GraphDBPassword)
|
||||
func (ts *Triplestore) Provision(ctx context.Context, instance models.Instance, domain string) error {
|
||||
return ts.CreateRepository(ctx, instance.GraphDBRepository, domain, instance.GraphDBUsername, instance.GraphDBPassword)
|
||||
}
|
||||
|
||||
func (ts *Triplestore) Purge(instance models.Instance, domain string) error {
|
||||
func (ts *Triplestore) Purge(ctx context.Context, instance models.Instance, domain string) error {
|
||||
return errorx.First(
|
||||
ts.PurgeRepo(instance.GraphDBRepository),
|
||||
ts.PurgeUser(instance.GraphDBUsername),
|
||||
ts.PurgeRepo(ctx, instance.GraphDBRepository),
|
||||
ts.PurgeUser(ctx, instance.GraphDBUsername),
|
||||
)
|
||||
}
|
||||
|
||||
func (ts *Triplestore) CreateRepository(name, domain, user, password string) error {
|
||||
if err := ts.Wait(); err != nil {
|
||||
func (ts *Triplestore) CreateRepository(ctx context.Context, name, domain, user, password string) error {
|
||||
if err := ts.Wait(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -48,7 +49,7 @@ func (ts *Triplestore) CreateRepository(name, domain, user, password string) err
|
|||
|
||||
// do the create!
|
||||
{
|
||||
res, err := ts.OpenRaw("POST", "/rest/repositories", createRepo.Bytes(), "config", "")
|
||||
res, err := ts.OpenRaw(ctx, "POST", "/rest/repositories", createRepo.Bytes(), "config", "")
|
||||
if err != nil {
|
||||
return errTripleStoreFailedRepository.WithMessageF(err)
|
||||
}
|
||||
|
|
@ -60,7 +61,7 @@ func (ts *Triplestore) CreateRepository(name, domain, user, password string) err
|
|||
|
||||
// create the user and grant them access
|
||||
{
|
||||
res, err := ts.OpenRaw("POST", "/rest/security/users/"+user, TriplestoreUserPayload{
|
||||
res, err := ts.OpenRaw(ctx, "POST", "/rest/security/users/"+user, TriplestoreUserPayload{
|
||||
Password: password,
|
||||
AppSettings: TriplestoreUserAppSettings{
|
||||
DefaultInference: true,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package triplestore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
|
|
@ -13,10 +14,10 @@ func (Triplestore) SnapshotNeedsRunning() bool { return false }
|
|||
|
||||
func (Triplestore) SnapshotName() string { return "triplestore" }
|
||||
|
||||
func (ts *Triplestore) Snapshot(wisski models.Instance, context component.StagingContext) error {
|
||||
return context.AddDirectory(".", func() error {
|
||||
return context.AddFile(wisski.GraphDBRepository+".nq", func(file io.Writer) error {
|
||||
_, err := ts.SnapshotDB(file, wisski.GraphDBRepository)
|
||||
func (ts *Triplestore) Snapshot(wisski models.Instance, scontext component.StagingContext) error {
|
||||
return scontext.AddDirectory(".", func(ctx context.Context) error {
|
||||
return scontext.AddFile(wisski.GraphDBRepository+".nq", func(ctx context.Context, file io.Writer) error {
|
||||
_, err := ts.SnapshotDB(ctx, file, wisski.GraphDBRepository)
|
||||
return err
|
||||
})
|
||||
})
|
||||
|
|
@ -25,8 +26,8 @@ func (ts *Triplestore) Snapshot(wisski models.Instance, context component.Stagin
|
|||
var errTSBackupWrongStatusCode = errors.New("Triplestore.Backup: Wrong status code")
|
||||
|
||||
// SnapshotDB snapshots the provided repository into dst
|
||||
func (ts Triplestore) SnapshotDB(dst io.Writer, repo string) (int64, error) {
|
||||
res, err := ts.OpenRaw("GET", "/repositories/"+repo+"/statements?infer=false", nil, "", "application/n-quads")
|
||||
func (ts Triplestore) SnapshotDB(ctx context.Context, dst io.Writer, repo string) (int64, error) {
|
||||
res, err := ts.OpenRaw(ctx, "GET", "/repositories/"+repo+"/statements?infer=false", nil, "", "application/n-quads")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package triplestore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
|
@ -15,8 +14,7 @@ type Triplestore struct {
|
|||
|
||||
BaseURL string // upstream server url
|
||||
|
||||
PollContext context.Context // context to abort polling with
|
||||
PollInterval time.Duration // duration to wait for during wait
|
||||
PollInterval time.Duration // duration to wait for during wait
|
||||
}
|
||||
|
||||
func (ts *Triplestore) Path() string {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package triplestore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
|
|
@ -11,15 +12,15 @@ import (
|
|||
|
||||
var errTriplestoreFailedSecurity = errors.New("failed to enable triplestore security: request did not succeed with HTTP 200 OK")
|
||||
|
||||
func (ts Triplestore) Update(io stream.IOStream) error {
|
||||
func (ts Triplestore) Update(ctx context.Context, io stream.IOStream) error {
|
||||
logging.LogMessage(io, "Waiting for Triplestore")
|
||||
if err := ts.Wait(); err != nil {
|
||||
if err := ts.Wait(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logging.LogMessage(io, "Resetting admin user password")
|
||||
{
|
||||
res, err := ts.OpenRaw("PUT", "/rest/security/users/"+ts.Config.TriplestoreAdminUser, TriplestoreUserPayload{
|
||||
res, err := ts.OpenRaw(ctx, "PUT", "/rest/security/users/"+ts.Config.TriplestoreAdminUser, TriplestoreUserPayload{
|
||||
Password: ts.Config.TriplestoreAdminPassword,
|
||||
AppSettings: TriplestoreUserAppSettings{
|
||||
DefaultInference: true,
|
||||
|
|
@ -51,7 +52,7 @@ func (ts Triplestore) Update(io stream.IOStream) error {
|
|||
|
||||
logging.LogMessage(io, "Enabling Triplestore security")
|
||||
{
|
||||
res, err := ts.OpenRaw("POST", "/rest/security", true, "", "")
|
||||
res, err := ts.OpenRaw(ctx, "POST", "/rest/security", true, "", "")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to enable triplestore security: %s", err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue