-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement SynchronizerAppCreator for managing application synch…
…ronization and add related tests
- Loading branch information
Showing
9 changed files
with
339 additions
and
11 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
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
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
87 changes: 87 additions & 0 deletions
87
pkg/convenience/synchronizer_node/synchronizer_app_create.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,87 @@ | ||
package synchronizernode | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"github.com/cartesi/rollups-graphql/pkg/convenience/repository" | ||
) | ||
|
||
type SynchronizerAppCreator struct { | ||
AppRepository *repository.ApplicationRepository | ||
RawRepository *RawRepository | ||
} | ||
|
||
func NewSynchronizerAppCreator( | ||
AppRepository *repository.ApplicationRepository, | ||
RawRepository *RawRepository, | ||
) *SynchronizerAppCreator { | ||
return &SynchronizerAppCreator{ | ||
AppRepository, | ||
RawRepository, | ||
} | ||
} | ||
|
||
func (s *SynchronizerAppCreator) startTransaction(ctx context.Context) (context.Context, error) { | ||
db := s.AppRepository.Db | ||
ctxWithTx, err := repository.StartTransaction(ctx, &db) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
return ctxWithTx, nil | ||
} | ||
|
||
func (s *SynchronizerAppCreator) rollbackTransaction(ctx context.Context) { | ||
tx, hasTx := repository.GetTransaction(ctx) | ||
if hasTx && tx != nil { | ||
err := tx.Rollback() | ||
if err != nil { | ||
slog.Error("transaction rollback error", "err", err) | ||
panic(err) | ||
} | ||
} | ||
} | ||
|
||
func (s *SynchronizerAppCreator) commitTransaction(ctx context.Context) error { | ||
tx, hasTx := repository.GetTransaction(ctx) | ||
if hasTx && tx != nil { | ||
return tx.Commit() | ||
} | ||
return nil | ||
} | ||
|
||
func (s SynchronizerAppCreator) SyncApps(ctx context.Context) error { | ||
txCtx, err := s.startTransaction(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
err = s.syncApps(txCtx) | ||
if err != nil { | ||
s.rollbackTransaction(txCtx) | ||
return err | ||
} | ||
err = s.commitTransaction(txCtx) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (s *SynchronizerAppCreator) syncApps(ctx context.Context) error { | ||
lastAppRef, err := s.RawRepository.GetLatestApp(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
apps, err := s.RawRepository.GetApplicationRef(ctx, lastAppRef.ID) | ||
if err != nil { | ||
return err | ||
} | ||
for _, app := range apps { | ||
_, err = s.AppRepository.Create(ctx, &app) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.