Skip to content

Commit

Permalink
add logging to copier
Browse files Browse the repository at this point in the history
  • Loading branch information
nourbalaha committed Jan 15, 2025
1 parent 7015005 commit 922a5f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
7 changes: 7 additions & 0 deletions server/internal/infrastructure/gcp/taskrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func NewTaskRunner(ctx context.Context, conf *TaskConfig) (gateway.TaskRunner, e
// Run implements gateway.TaskRunner
func (t *TaskRunner) Run(ctx context.Context, p task.Payload) error {
if p.Webhook == nil {
log.Info("copy: run cloud build!")
return t.runCloudBuild(ctx, p)
}
return t.runPubSub(ctx, p)
Expand Down Expand Up @@ -74,6 +75,7 @@ func (t *TaskRunner) runCloudBuild(ctx context.Context, p task.Payload) error {
if p.DecompressAsset != nil {
return decompressAsset(ctx, p, t.conf)
}
log.Info("copy: run copy!")
return copy(ctx, p, t.conf)
}

Expand Down Expand Up @@ -148,6 +150,7 @@ func copy(ctx context.Context, p task.Payload, conf *TaskConfig) error {
if !p.Copy.Validate() {
return nil
}
log.Info("copy: copy event running")

cb, err := cloudbuild.NewService(ctx)
if err != nil {
Expand All @@ -156,6 +159,7 @@ func copy(ctx context.Context, p task.Payload, conf *TaskConfig) error {

project := conf.GCPProject
region := conf.GCPRegion
log.Infof("copy: project %v, region %v", project, region)

build := &cloudbuild.Build{
Timeout: "86400s", // 1 day
Expand Down Expand Up @@ -187,13 +191,16 @@ func copy(ctx context.Context, p task.Payload, conf *TaskConfig) error {
if region != "" {
call := cb.Projects.Locations.Builds.Create(path.Join("projects", project, "locations", region), build)
_, err = call.Do()
log.Info("copy: call build with region!")
} else {
call := cb.Projects.Builds.Create(project, build)
_, err = call.Do()
log.Info("copy: call build without region!")
}
if err != nil {
return rerror.ErrInternalBy(err)
}
log.Info("copy: cloud build done!")
return nil
}

Expand Down
23 changes: 20 additions & 3 deletions server/internal/usecase/interactor/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,25 +327,33 @@ func (i Model) Copy(ctx context.Context, params interfaces.CopyModelParam, opera
if err != nil {
return nil, err
}
log.Info("copy: model copied")
// copy schema
if err := i.copySchema(ctx, oldModel.Schema(), newModel.Schema()); err != nil {
err = i.copySchema(ctx, oldModel.Schema(), newModel.Schema());
if err != nil {
return nil, err
}
log.Info("copy: schema copied")
// copy items
if err := i.copyItems(ctx, oldModel.Schema(), newModel.Schema(), newModel.ID()); err != nil {
err = i.copyItems(ctx, oldModel.Schema(), newModel.Schema(), newModel.ID());
if err != nil {
return nil, err
}
log.Info("copy: items copied")
// copy metadata
if oldModel.Metadata() != nil {
// copy meta schema
newMetaSchema, err := i.copyMetaSchema(ctx, *oldModel.Metadata(), newModel)
if err != nil {
return nil, err
}
log.Info("copy: meta schema copied")
// copy meta items
if err := i.copyItems(ctx, *oldModel.Metadata(), newMetaSchema.ID(), newModel.ID()); err != nil {
err = i.copyItems(ctx, *oldModel.Metadata(), newMetaSchema.ID(), newModel.ID());
if err != nil {
return nil, err
}
log.Info("copy: meta items copied")
}
// return the new model
return newModel, nil
Expand All @@ -357,6 +365,7 @@ func (i Model) copyModel(ctx context.Context, params interfaces.CopyModelParam,
if err != nil {
return nil, nil, err
}
log.Infof("copy: old model with id %v found", oldModel.ID().String())
name := lo.ToPtr(oldModel.Name() + " Copy")
if params.Name != nil {
name = params.Name
Expand All @@ -375,6 +384,7 @@ func (i Model) copyModel(ctx context.Context, params interfaces.CopyModelParam,
if err != nil {
return nil, nil, err
}
log.Infof("copy: new model with id %v created", newModel.ID().String())
return oldModel, newModel, nil
}

Expand All @@ -383,10 +393,12 @@ func (i Model) copySchema(ctx context.Context, oldSchemaId, newSchemaId id.Schem
if err != nil {
return err
}
log.Infof("copy: old schema with id %v found", oldSchema.ID().String())
newSchema, err := i.repos.Schema.FindByID(ctx, newSchemaId)
if err != nil {
return err
}
log.Infof("copy: new schema with id %v found", oldSchema.ID().String())
newSchema.CopyFrom(oldSchema)
return i.repos.Schema.Save(ctx, newSchema)
}
Expand All @@ -396,6 +408,7 @@ func (i Model) copyMetaSchema(ctx context.Context, oldMetaSchemaId id.SchemaID,
if err != nil {
return nil, err
}
log.Infof("copy: old meta schema with id %v found", oldMetaSchema.ID().String())
newMetaSchema, err := schema.New().
NewID().
Workspace(oldMetaSchema.Workspace()).
Expand All @@ -410,9 +423,11 @@ func (i Model) copyMetaSchema(ctx context.Context, oldMetaSchemaId id.SchemaID,
if err := i.repos.Model.Save(ctx, newModel); err != nil {
return nil, err
}
log.Info("copy: new model saved!")
if err := i.repos.Schema.Save(ctx, newMetaSchema); err != nil {
return nil, err
}
log.Info("copy: new meta schema saved!")
return newMetaSchema, nil
}

Expand All @@ -439,6 +454,7 @@ func (i Model) copyItems(ctx context.Context, oldSchemaID, newSchemaID id.Schema
if err != nil {
return err
}
log.Infof("copy: copy event triggered. collection: s%, filter: s%, changes: s%", collection, filter, changes)
return i.triggerCopyEvent(ctx, collection, string(filter), string(changes))
}

Expand All @@ -453,6 +469,7 @@ func (i Model) triggerCopyEvent(ctx context.Context, collection, filter, changes
Filter: filter,
Changes: changes,
}
log.Infof("copy: task payload created: %v", taskPayload)

if err := i.gateways.TaskRunner.Run(ctx, taskPayload.Payload()); err != nil {
return fmt.Errorf("failed to trigger copy event: %w", err)
Expand Down

0 comments on commit 922a5f6

Please sign in to comment.