Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
feat: re-sign Synchronizer's API
Browse files Browse the repository at this point in the history
  • Loading branch information
yeqown committed Feb 25, 2022
1 parent 7808957 commit 8e586f2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cmd/asy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func action(c *cli.Context) error {
return errors.Wrap(err, "create synchronizer failed")
}

return s.Synchronize(context.Background())
_, err = s.Synchronize(context.Background())
return err
}

var flags = []cli.Flag{
Expand Down
21 changes: 18 additions & 3 deletions cmd/gui/backend/app_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ func (b *App) Synchronize(param *SynchronizeScope) (result *synchronizeResult) {
if !result.Succeeded {
b.statistics.DownloadFailedCount++
}
// TODO(@yeqown): count download files and total file size.
case asy.SynchronizeMode_UPLOAD:
b.statistics.UploadCount++
if !result.Succeeded {
b.statistics.UploadFailedCount++
}
// TODO(@yeqown): count upload files and total file size.
}
}()

Expand Down Expand Up @@ -101,12 +99,29 @@ func (b *App) Synchronize(param *SynchronizeScope) (result *synchronizeResult) {
return result
}

if err := s.Synchronize(ctx); err != nil {
results, err := s.Synchronize(ctx)
if err != nil {
b.errorf("synchronize failed: %v", err)
result.markFailure(err)
return result
}

// file and size statistics
{
switch param.Mode {
case asy.SynchronizeMode_UPLOAD:
b.statistics.UploadFileCount += int64(len(results))
for _, v := range results {
b.statistics.UploadFileSize += int64(v.Bytes)
}
case asy.SynchronizeMode_DOWNLOAD:
b.statistics.DownloadFileCount += int64(len(results))
for _, v := range results {
b.statistics.DownloadFileSize += int64(v.Bytes)
}
}
}

result.markSuccess()
return result
}
Expand Down
2 changes: 1 addition & 1 deletion synchronize.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// Synchronizer 's duty is synchronizing between remote apollo portal and local filesystem.
type Synchronizer interface {
Synchronize(ctx context.Context) error
Synchronize(ctx context.Context) ([]*SynchronizeResult, error)
}

type SynchronizeMode uint8
Expand Down
16 changes: 10 additions & 6 deletions synchronize_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func NewSynchronizer(scope *SynchronizeScope) (Synchronizer, error) {

// Synchronize scheduling components to display information and execute CURD action with resources.
// NOTICE: properties will be ignored.
func (s *synchronizer) Synchronize(ctx context.Context) error {
func (s *synchronizer) Synchronize(ctx context.Context) ([]*SynchronizeResult, error) {
scope := s.scope
log.Info("prepare to fetching remote namespace resources. please wait")
// load app/env/cluster/remote info
namespaceInfos, err := s.apollo.ListNamespaces(ctx, scope.ApolloAppID, scope.ApolloEnv, scope.ApolloClusterName)
if err != nil {
return errors.Wrap(err, "failed to ListNamespaces in synchronizer.Synchronize")
return nil, errors.Wrap(err, "failed to ListNamespaces in synchronizer.Synchronize")
}
namespaces := make([]string, 0, len(namespaceInfos))
for _, v := range namespaceInfos {
Expand Down Expand Up @@ -96,15 +96,15 @@ func (s *synchronizer) Synchronize(ctx context.Context) error {
default:
// interrupt the synchronization
log.Info("you cancel the synchronization. quit")
return nil
return nil, nil
}

log.Info("synchronizing ...")
syncResults := s.doSynchronize(scope, diffs)
results := s.doSynchronize(scope, diffs)
log.Info("synchronization finished, please check the result")
s.renderingResult(syncResults)
s.renderingResult(results)

return nil
return results, nil
}

// compare calculates the difference between local and remote.
Expand Down Expand Up @@ -159,6 +159,7 @@ type SynchronizeResult struct {
Error string `json:"error"` // modified failed reason
Succeeded bool `json:"succeeded"` // modified Succeeded
Published bool `json:"published"` // changes Published
Bytes int `json:"bytes"` // file size (byte)
}

// doSynchronize execute synchronization between local and remote.
Expand Down Expand Up @@ -224,6 +225,7 @@ func (s synchronizer) download(ctx context.Context, d Diff1) (r *SynchronizeResu
Succeeded: false,
// download always Published by default since it has no version control mechanism.
Published: true,
Bytes: 0,
}
var err error

Expand All @@ -239,6 +241,7 @@ func (s synchronizer) download(ctx context.Context, d Diff1) (r *SynchronizeResu
err = err2
goto Failed
}
r.Bytes = len([]byte(item.Value))
err = os.WriteFile(d.AbsFilepath, []byte(item.Value), 0644)
}

Expand Down Expand Up @@ -280,6 +283,7 @@ func (s synchronizer) upload(ctx context.Context, d Diff1, autoPublish bool) (r
err = err2
goto Failed
}
r.Bytes = len(bytes)
_, err = s.apollo.CreateNamespace(ctx,
namespaceName, s.scope.ApolloAppID, api.Format(format), false, "created by apollo-synchronizer")
if err != nil {
Expand Down

0 comments on commit 8e586f2

Please sign in to comment.