Add informers and internal go routines in manager (#2870)
* used manager's Add function in more places * exit when ctx.Done is received in archivePruner go routines * fix manager tests * fix data race * added more gpm function in manager and removed manager from a util function * closed unused channel and stopped ticker after context is done * added log statements * used context.Done inside function instead of stopper channel
This commit is contained in:
@@ -26,6 +26,7 @@ import (
|
||||
"github.com/fission/fission/pkg/crd"
|
||||
"github.com/fission/fission/pkg/generated/clientset/versioned"
|
||||
"github.com/fission/fission/pkg/utils"
|
||||
"github.com/fission/fission/pkg/utils/manager"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@@ -55,17 +56,24 @@ func MakeArchivePruner(logger *zap.Logger, clientGen crd.ClientGeneratorInterfac
|
||||
}
|
||||
|
||||
// pruneArchives listens to archiveChannel for archive ids that need to be deleted
|
||||
func (pruner *ArchivePruner) pruneArchives() {
|
||||
func (pruner *ArchivePruner) pruneArchives(ctx context.Context) {
|
||||
pruner.logger.Debug("listening to archiveChannel to prune archives")
|
||||
for archiveID := range pruner.archiveChan {
|
||||
pruner.logger.Info("sending delete request for archive",
|
||||
zap.String("archive_id", archiveID))
|
||||
if err := pruner.stowClient.removeFileByID(archiveID); err != nil {
|
||||
// logging the error and continuing with other deletions.
|
||||
// hopefully this archive will be deleted in the next iteration.
|
||||
pruner.logger.Error("ignoring error while deleting archive",
|
||||
zap.Error(err),
|
||||
for {
|
||||
select {
|
||||
case archiveID := <-pruner.archiveChan:
|
||||
pruner.logger.Info("sending delete request for archive",
|
||||
zap.String("archive_id", archiveID))
|
||||
if err := pruner.stowClient.removeFileByID(archiveID); err != nil {
|
||||
// logging the error and continuing with other deletions.
|
||||
// hopefully this archive will be deleted in the next iteration.
|
||||
pruner.logger.Error("ignoring error while deleting archive",
|
||||
zap.Error(err),
|
||||
zap.String("archive_id", archiveID))
|
||||
}
|
||||
case <-ctx.Done():
|
||||
close(pruner.archiveChan)
|
||||
pruner.logger.Info("stopped listening to archiveChannel to prune archives, context cancelled")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -142,12 +150,20 @@ func (pruner *ArchivePruner) getOrphanArchives(ctx context.Context) {
|
||||
// Start starts a go routine that listens to a channel for archive IDs that need to deleted.
|
||||
// Also wakes up at regular intervals to make a list of archive IDs that need to be reaped
|
||||
// and sends them over to the channel for deletion
|
||||
func (pruner *ArchivePruner) Start(ctx context.Context) {
|
||||
func (pruner *ArchivePruner) Start(ctx context.Context, mgr manager.Interface) {
|
||||
ticker := time.NewTicker(pruner.pruneInterval * time.Minute)
|
||||
go pruner.pruneArchives()
|
||||
for range ticker.C {
|
||||
// This method fetches unused archive IDs and sends them to archiveChannel for deletion
|
||||
// silencing the errors, hoping they go away in next iteration.
|
||||
pruner.getOrphanArchives(ctx)
|
||||
mgr.Add(ctx, func(ctx context.Context) {
|
||||
pruner.pruneArchives(ctx)
|
||||
})
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
// This method fetches unused archive IDs and sends them to archiveChannel for deletion
|
||||
// silencing the errors, hoping they go away in next iteration.
|
||||
pruner.getOrphanArchives(ctx)
|
||||
case <-ctx.Done():
|
||||
ticker.Stop()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -316,7 +316,9 @@ func Start(ctx context.Context, clientGen crd.ClientGeneratorInterface, logger *
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Error creating archivePruner")
|
||||
}
|
||||
go pruner.Start(ctx)
|
||||
mgr.Add(ctx, func(ctx context.Context) {
|
||||
pruner.Start(ctx, mgr)
|
||||
})
|
||||
}
|
||||
|
||||
logger.Info("storage service started")
|
||||
|
||||
Reference in New Issue
Block a user