This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(server): use repos in datacatalogv2
- Loading branch information
Showing
5 changed files
with
96 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
server/datacatalog/datacatalogv2/datacatalogv2adapter/repos.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package datacatalogv2adapter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/eukarya-inc/reearth-plateauview/server/datacatalog/datacatalogv2" | ||
"github.com/eukarya-inc/reearth-plateauview/server/datacatalog/plateauapi" | ||
"github.com/reearth/reearthx/log" | ||
"github.com/reearth/reearthx/util" | ||
) | ||
|
||
type Repos struct { | ||
fetchers *util.SyncMap[string, datacatalogv2.Fetchable] | ||
*plateauapi.Repos | ||
} | ||
|
||
func NewRepos() *Repos { | ||
r := &Repos{ | ||
fetchers: util.NewSyncMap[string, datacatalogv2.Fetchable](), | ||
} | ||
r.Repos = plateauapi.NewRepos(r.update) | ||
return r | ||
} | ||
|
||
func (r *Repos) Prepare(ctx context.Context, project string, f datacatalogv2.Fetchable) error { | ||
if _, ok := r.fetchers.Load(project); ok { | ||
return nil | ||
} | ||
|
||
r.setCMS(project, f) | ||
_, err := r.Update(ctx, project) | ||
return err | ||
} | ||
|
||
func (r *Repos) update(ctx context.Context, project string) (*plateauapi.ReposUpdateResult, error) { | ||
fetcher, ok := r.fetchers.Load(project) | ||
if !ok { | ||
return nil, fmt.Errorf("fetcher is not initialized for %s", project) | ||
} | ||
|
||
updated := r.UpdatedAt(project) | ||
var updatedStr string | ||
if !updated.IsZero() { | ||
updatedStr = updated.Format(time.RFC3339) | ||
} | ||
log.Debugfc(ctx, "datacatalogv2: updating repo %s: last_update=%s", project, updatedStr) | ||
|
||
repo, err := fetchAndCreateCache(ctx, project, fetcher, datacatalogv2.FetcherDoOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
adminRepo := repo.Clone() | ||
adminRepo.SetAdmin(true) | ||
|
||
log.Debugfc(ctx, "datacatalogv2: updated repo %s", project) | ||
|
||
return &plateauapi.ReposUpdateResult{ | ||
Repo: repo, | ||
AdminRepo: adminRepo, | ||
}, nil | ||
} | ||
|
||
func (r *Repos) setCMS(project string, f datacatalogv2.Fetchable) { | ||
r.fetchers.Store(project, f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters