Capture os signals to gracefully shutdown fission components (#2261)
- Currently, fission components don't handle shutdown signals. So we don't get any to do the required cleanup before the fission process exits. Adding signal capture process with cancelling context so that all dependent processes stop working when the process gets term signal. - Set log level to error in otel shutdown function Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
@@ -75,13 +75,13 @@ func (pruner *ArchivePruner) insertArchive(archiveID string) {
|
||||
// A user may have deleted pkgs with kubectl or fission cli. That only deletes crd.Package objects from kubernetes
|
||||
// and not the archives that are referenced by them, leaving the archives as orphans.
|
||||
// getOrphanArchives reaps the orphaned archives.
|
||||
func (pruner *ArchivePruner) getOrphanArchives() {
|
||||
func (pruner *ArchivePruner) getOrphanArchives(ctx context.Context) {
|
||||
pruner.logger.Debug("getting orphan archives")
|
||||
archivesRefByPkgs := make([]string, 0)
|
||||
var archiveID string
|
||||
|
||||
// get all pkgs from kubernetes
|
||||
pkgList, err := pruner.crdClient.CoreV1().Packages(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
|
||||
pkgList, err := pruner.crdClient.CoreV1().Packages(metav1.NamespaceAll).List(ctx, metav1.ListOptions{})
|
||||
if err != nil {
|
||||
pruner.logger.Error("error getting package list from kubernetes", zap.Error(err))
|
||||
return
|
||||
@@ -137,12 +137,12 @@ func (pruner *ArchivePruner) getOrphanArchives() {
|
||||
// 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() {
|
||||
func (pruner *ArchivePruner) Start(ctx context.Context) {
|
||||
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()
|
||||
pruner.getOrphanArchives(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ func runMinioDockerContainer(pool *dockertest.Pool) *dockertest.Resource {
|
||||
return resource
|
||||
}
|
||||
|
||||
func startS3StorageService(endpoint, bucketName, subDir string) {
|
||||
func startS3StorageService(ctx context.Context, endpoint, bucketName, subDir string) {
|
||||
// testID := uniuri.NewLen(8)
|
||||
port := 8081
|
||||
|
||||
@@ -94,7 +94,7 @@ func startS3StorageService(endpoint, bucketName, subDir string) {
|
||||
os.Setenv("STORAGE_S3_REGION", minioRegion)
|
||||
|
||||
storage := storagesvc.NewS3Storage()
|
||||
_ = storagesvc.Start(logger, storage, port, true)
|
||||
_ = storagesvc.Start(ctx, logger, storage, port, true)
|
||||
}
|
||||
|
||||
func TestS3StorageService(t *testing.T) {
|
||||
@@ -135,7 +135,7 @@ func TestS3StorageService(t *testing.T) {
|
||||
// Start storagesvc
|
||||
bucketName := "test-s3-service"
|
||||
subDir := "x/y/z"
|
||||
startS3StorageService(endpoint, bucketName, subDir)
|
||||
startS3StorageService(context.Background(), endpoint, bucketName, subDir)
|
||||
|
||||
time.Sleep(time.Second)
|
||||
client := MakeClient(fmt.Sprintf("http://localhost:%v/", 8081))
|
||||
@@ -211,7 +211,7 @@ func TestLocalStorageService(t *testing.T) {
|
||||
localPath := fmt.Sprintf("/tmp/%v", testID)
|
||||
_ = os.Mkdir(localPath, os.ModePerm)
|
||||
storage := storagesvc.NewLocalStorage(localPath)
|
||||
_ = storagesvc.Start(logger, storage, port, true)
|
||||
_ = storagesvc.Start(context.Background(), logger, storage, port, true)
|
||||
|
||||
time.Sleep(time.Second)
|
||||
client := MakeClient(fmt.Sprintf("http://localhost:%v/", port))
|
||||
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package storagesvc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -221,7 +222,7 @@ func (ss *StorageService) Start(port int, openTracingEnabled bool) {
|
||||
}
|
||||
|
||||
// Start runs storage service
|
||||
func Start(logger *zap.Logger, storage Storage, port int, openTracingEnabled bool) error {
|
||||
func Start(ctx context.Context, logger *zap.Logger, storage Storage, port int, openTracingEnabled bool) error {
|
||||
enablePruner := true
|
||||
// create a storage client
|
||||
storageClient, err := MakeStowClient(logger, storage)
|
||||
@@ -244,7 +245,7 @@ func Start(logger *zap.Logger, storage Storage, port int, openTracingEnabled boo
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Error creating archivePruner")
|
||||
}
|
||||
go pruner.Start()
|
||||
go pruner.Start(ctx)
|
||||
}
|
||||
|
||||
logger.Info("storage service started")
|
||||
|
||||
Reference in New Issue
Block a user