Skip to content

Commit

Permalink
feat(server): auto-update
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Dec 3, 2019
1 parent 5b9997d commit dafddc4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ include rules.mk
.PHONY: run
run: install
time depviz --debug run --no-graph moul/depviz-test moul/depviz moul-bot/depviz-test moul/multipmuri moul/graphman
time depviz --debug server --without-recovery --godmode
time depviz --debug server --without-recovery --godmode --realm "DEV"

.PHONY: update_examples
update_examples:
Expand Down
17 changes: 16 additions & 1 deletion cmd/depviz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"moul.io/depviz/internal/dvcore"
"moul.io/depviz/internal/dvparser"
"moul.io/depviz/internal/dvserver"
"moul.io/depviz/internal/dvstore"
"moul.io/godev"
Expand Down Expand Up @@ -48,12 +49,16 @@ var (
serverRequestTimeout = serverFlags.Duration("request-timeout", 5*time.Second, "request timeout")
serverShutdownTimeout = serverFlags.Duration("shutdowm-timeout", 6*time.Second, "shutdown timeout")
serverCORSAllowedOrigins = serverFlags.String("cors-allowed-origins", "*", "allowed CORS origins")
serverGitHubToken = serverFlags.String("github-token", "", "GitHub token")
serverGitLabToken = serverFlags.String("gitlab-token", "", "GitLab token")
serverNoAutoUpdate = serverFlags.Bool("no-auto-update", false, "don't auto-update projects in background")
serverGodmode = serverFlags.Bool("godmode", false, "enable dangerous API calls")
serverWithPprof = serverFlags.Bool("with-pprof", false, "enable pprof endpoints")
serverWithoutRecovery = serverFlags.Bool("without-recovery", false, "disable panic recovery (dev)")
serverWithoutCache = serverFlags.Bool("without-cache", false, "disable HTTP caching")
serverAuth = serverFlags.String("auth", "", "authentication password")
serverRealm = serverFlags.String("realm", "DepViz", "server Realm")
serverAutoUpdateInterval = serverFlags.Duration("auto-update-interval", 2*time.Minute, "time between two auto-updates")

runFlags = flag.NewFlagSet("run", flag.ExitOnError)
runNoPull = runFlags.Bool("no-pull", false, "don't pull providers (graph only)")
Expand Down Expand Up @@ -257,8 +262,8 @@ func execRun(args []string) error {
NoGraph: *runNoGraph,
NoPull: *runNoPull,
Format: *runFormat,
GitHubToken: *runGitHubToken,
Resync: *runResync,
GitHubToken: *runGitHubToken,
GitLabToken: *runGitLabToken,
ShowClosed: *runShowClosed,
HideIsolated: *runHideIsolated,
Expand All @@ -285,6 +290,11 @@ func execServer(args []string) error {
return fmt.Errorf("init store: %w", err)
}

targets, err := dvparser.ParseTargets(args)
if err != nil {
return fmt.Errorf("parse targets: %w", err)
}

opts := dvserver.Opts{
Logger: logger,
HTTPBind: *serverHTTPBind,
Expand All @@ -298,6 +308,11 @@ func execServer(args []string) error {
Auth: *serverAuth,
Realm: *serverRealm,
Godmode: *serverGodmode,
GitHubToken: *serverGitHubToken,
GitLabToken: *serverGitLabToken,
NoAutoUpdate: *serverNoAutoUpdate,
AutoUpdateTargets: targets,
AutoUpdateInterval: *serverAutoUpdateInterval,
}
svc, err = dvserver.New(ctx, store, schemaConfig, opts)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 20 additions & 13 deletions internal/dvcore/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,8 @@ func Run(h *cayley.Handle, args []string, opts RunOpts) error {
}

if !opts.NoPull {
batches, err := pullBatches(targets, h, opts)
if err != nil {
return fmt.Errorf("pull batches: %w", err)
}
err = saveBatches(h, opts.Schema, batches)
if err != nil {
return fmt.Errorf("save batches: %w", err)
if err := PullAndSave(targets, h, opts.Schema, opts.GitHubToken, opts.GitLabToken, opts.Resync, opts.Logger); err != nil {
return fmt.Errorf("pull: %w", err)
}
}

Expand Down Expand Up @@ -156,7 +151,19 @@ func Run(h *cayley.Handle, args []string, opts RunOpts) error {
return nil
}

func pullBatches(targets []multipmuri.Entity, h *cayley.Handle, opts RunOpts) ([]dvmodel.Batch, error) {
func PullAndSave(targets []multipmuri.Entity, h *cayley.Handle, schema *schema.Config, githubToken string, gitlabToken string, resync bool, logger *zap.Logger) error {
batches, err := pullBatches(targets, h, githubToken, gitlabToken, resync, logger)
if err != nil {
return fmt.Errorf("pull batches: %w", err)
}
err = saveBatches(h, schema, batches)
if err != nil {
return fmt.Errorf("save batches: %w", err)
}
return nil
}

func pullBatches(targets []multipmuri.Entity, h *cayley.Handle, githubToken string, gitlabToken string, resync bool, logger *zap.Logger) ([]dvmodel.Batch, error) {
// FIXME: handle the special '@me' target
var (
wg sync.WaitGroup
Expand All @@ -175,22 +182,22 @@ func pullBatches(targets []multipmuri.Entity, h *cayley.Handle, opts RunOpts) ([

ghOpts := githubprovider.Opts{
// FIXME: Since: lastUpdated,
Logger: opts.Logger.Named("github"),
Logger: logger.Named("github"),
}

if !opts.Resync {
if !resync {
since, err := dvstore.LastUpdatedIssueInRepo(ctx, h, repo)
if err != nil {
opts.Logger.Warn("failed to get last updated issue", zap.Error(err))
logger.Warn("failed to get last updated issue", zap.Error(err))

}
ghOpts.Since = &since
}

githubprovider.FetchRepo(ctx, repo, opts.GitHubToken, out, ghOpts)
githubprovider.FetchRepo(ctx, repo, githubToken, out, ghOpts)
}(target)
//case multipmuri.GitLabProvider:
//go gitlab.Pull(target, &wg, opts.GitlabToken, db, out)
//go gitlab.Pull(target, &wg, gitlabToken, db, out)
default:
// FIXME: clean context-based exit
panic(fmt.Sprintf("unsupported provider: %v", provider))
Expand Down
35 changes: 35 additions & 0 deletions internal/dvserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"go.uber.org/zap"
"google.golang.org/grpc"
"moul.io/depviz/internal/chiutil"
"moul.io/depviz/internal/dvcore"
"moul.io/multipmuri"
)

type Opts struct {
Expand All @@ -42,6 +44,11 @@ type Opts struct {
WithoutCache bool
Auth string
Realm string
GitHubToken string
GitLabToken string
NoAutoUpdate bool
AutoUpdateTargets []multipmuri.Entity
AutoUpdateInterval time.Duration
}

type Service interface {
Expand Down Expand Up @@ -81,6 +88,9 @@ func New(ctx context.Context, h *cayley.Handle, schema *schema.Config, opts Opts
if opts.Realm == "" {
opts.Realm = "DepViz"
}
if opts.AutoUpdateInterval == 0 {
opts.AutoUpdateInterval = 2 * time.Minute
}

svc := service{
ctx: ctx,
Expand Down Expand Up @@ -236,11 +246,36 @@ func New(ctx context.Context, h *cayley.Handle, schema *schema.Config, opts Opts
})
}

if !opts.NoAutoUpdate && len(opts.AutoUpdateTargets) > 0 {
ctx, cancel := context.WithCancel(context.Background())

svc.workers.Add(func() error {
svc.recurringTask(opts.AutoUpdateTargets)
for {
select {
case <-ctx.Done():
return nil
case <-time.After(opts.AutoUpdateInterval):
svc.recurringTask(opts.AutoUpdateTargets)
}
}
}, func(error) {
cancel()
})
}

// FIXME: add grpc-web support?

return &svc, nil
}

func (s *service) recurringTask(targets []multipmuri.Entity) {
s.opts.Logger.Debug("pull and save", zap.Any("targets", targets))
if err := dvcore.PullAndSave(targets, s.h, s.schema, s.opts.GitHubToken, s.opts.GitLabToken, false, s.opts.Logger); err != nil {
s.opts.Logger.Warn("pull and save", zap.Error(err))
}
}

func (s *service) Run() error {
return s.workers.Run()
}
Expand Down

0 comments on commit dafddc4

Please sign in to comment.