Skip to content

Commit

Permalink
refactor: Moved ticker to HandleResetCache()
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashk767 committed Feb 29, 2024
1 parent 120c800 commit 436eb3f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cmd/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ type UtilsCmdInterface interface {
CalculateSecret(account types.Account, epoch uint32, keystorePath string, chainId *big.Int) ([]byte, []byte, error)
HandleBlock(client *ethclient.Client, account types.Account, blockNumber *big.Int, config types.Configurations, rogueData types.Rogue, backupNodeActionsToIgnore []string)
ExecuteVote(flagSet *pflag.FlagSet)
Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string, resetAssetCacheChan chan bool) error
Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string) error
HandleExit()
ExecuteListAccounts(flagSet *pflag.FlagSet)
ClaimCommission(flagSet *pflag.FlagSet)
Expand Down
10 changes: 5 additions & 5 deletions cmd/mocks/utils_cmd_interface.go

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

12 changes: 3 additions & 9 deletions cmd/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,10 @@ func (*UtilsStruct) ExecuteVote(flagSet *pflag.FlagSet) {
err = cmdUtils.InitAssetCache(client)
utils.CheckError("Error in initializing asset cache: ", err)

resetAssetCacheChan := make(chan bool)
go utils.HandleResetCache(client, config.BufferPercent, resetAssetCacheChan)
go utils.HandleResetCache(client, config.BufferPercent)

log.Debugf("Calling Vote() with arguments rogueData = %+v, account address = %s, backup node actions to ignore = %s", rogueData, account.Address, backupNodeActionsToIgnore)
if err := cmdUtils.Vote(context.Background(), config, client, rogueData, account, backupNodeActionsToIgnore, resetAssetCacheChan); err != nil {
if err := cmdUtils.Vote(context.Background(), config, client, rogueData, account, backupNodeActionsToIgnore); err != nil {
log.Errorf("%v\n", err)
osUtils.Exit(1)
}
Expand Down Expand Up @@ -128,16 +127,11 @@ func (*UtilsStruct) HandleExit() {
}

//This function handles all the states of voting
func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string, resetAssetCacheChan chan bool) error {
assetCacheTicker := time.NewTicker(time.Second * time.Duration(core.AssetCacheExpiry))

func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string) error {
header, err := clientUtils.GetLatestBlockWithRetry(client)
utils.CheckError("Error in getting block: ", err)
for {
select {
case <-assetCacheTicker.C:
log.Info("ASSET CACHE EXPIRY TIME! Sending signal to reset asset cache")
resetAssetCacheChan <- true
case <-ctx.Done():
return nil
default:
Expand Down
2 changes: 1 addition & 1 deletion cmd/vote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestExecuteVote(t *testing.T) {
flagSetMock.On("GetStringSliceRogueMode", mock.AnythingOfType("*pflag.FlagSet")).Return(tt.args.rogueMode, tt.args.rogueModeErr)
cmdUtilsMock.On("InitAssetCache", mock.Anything).Return(tt.args.initAssetCacheErr)
cmdUtilsMock.On("HandleExit").Return()
cmdUtilsMock.On("Vote", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.voteErr)
cmdUtilsMock.On("Vote", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.voteErr)
osMock.On("Exit", mock.AnythingOfType("int")).Return()

utils := &UtilsStruct{}
Expand Down
16 changes: 8 additions & 8 deletions utils/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,20 +531,20 @@ func (*UtilsStruct) HandleOfficialJobsFromJSONFile(client *ethclient.Client, col
return overrideJobs, overriddenJobIds
}

func HandleResetCache(client *ethclient.Client, bufferPercent int32, resetAssetCacheChan chan bool) {
func HandleResetCache(client *ethclient.Client, bufferPercent int32) {
assetCacheTicker := time.NewTicker(time.Second * time.Duration(core.AssetCacheExpiry))
defer assetCacheTicker.Stop()

for {
select {
case <-resetAssetCacheChan:
// Call the ResetAssetCache function when a signal is received
if err := UtilsInterface.ResetAssetCache(client, bufferPercent); err != nil {
log.Errorf("Error resetting asset cache: %v", err)
}
<-assetCacheTicker.C // Wait for the next tick
log.Info("ASSET CACHE EXPIRED! INITIALIZING JOBS AND COLLECTIONS CACHE AGAIN...")
if err := UtilsInterface.ResetAssetCache(client, bufferPercent); err != nil {
log.Errorf("Error resetting asset cache: %v", err)
}
}
}

func (*UtilsStruct) ResetAssetCache(client *ethclient.Client, bufferPercent int32) error {
log.Info("ASSET CACHE EXPIRED! INITIALIZING JOBS AND COLLECTIONS CACHE AGAIN...")
state, err := UtilsInterface.GetBufferedState(client, bufferPercent)
if err != nil {
log.Error("Error in getting buffered state: ", err)
Expand Down

0 comments on commit 436eb3f

Please sign in to comment.