2022-05-18 16:12:54 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2022 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2022-04-26 17:57:01 +00:00
|
|
|
package shutdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-06 15:03:15 +00:00
|
|
|
"os"
|
2022-04-26 17:57:01 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/beaconclient"
|
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/database/sql"
|
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/gracefulshutdown"
|
|
|
|
"github.com/vulcanize/ipld-ethcl-indexer/pkg/loghelper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Shutdown all the internal services for the application.
|
2022-06-03 16:47:13 +00:00
|
|
|
func ShutdownServices(ctx context.Context, notifierCh chan os.Signal, waitTime time.Duration, DB sql.Database, BC *beaconclient.BeaconClient, shutdownOperations map[string]gracefulshutdown.Operation) error {
|
|
|
|
//successCh, errCh := gracefulshutdown.Shutdown(ctx, notifierCh, waitTime, )
|
|
|
|
successCh, errCh := gracefulshutdown.Shutdown(ctx, notifierCh, waitTime, shutdownOperations)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-successCh:
|
|
|
|
return nil
|
|
|
|
case err := <-errCh:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper function for shutting down the head tracking process.
|
|
|
|
func ShutdownHeadTracking(ctx context.Context, notifierCh chan os.Signal, waitTime time.Duration, DB sql.Database, BC *beaconclient.BeaconClient) error {
|
|
|
|
return ShutdownServices(ctx, notifierCh, waitTime, DB, BC, map[string]gracefulshutdown.Operation{
|
2022-05-06 15:03:15 +00:00
|
|
|
// Combining DB shutdown with BC because BC needs DB open to cleanly shutdown.
|
2022-04-26 17:57:01 +00:00
|
|
|
"beaconClient": func(ctx context.Context) error {
|
2022-05-06 15:03:15 +00:00
|
|
|
defer DB.Close()
|
2022-04-26 17:57:01 +00:00
|
|
|
err := BC.StopHeadTracking()
|
|
|
|
if err != nil {
|
|
|
|
loghelper.LogError(err).Error("Unable to trigger shutdown of head tracking")
|
|
|
|
}
|
2022-06-03 16:47:13 +00:00
|
|
|
if BC.KnownGapsProcess != (beaconclient.KnownGapsProcessing{}) {
|
|
|
|
err = BC.StopKnownGapsProcessing()
|
|
|
|
if err != nil {
|
|
|
|
loghelper.LogError(err).Error("Unable to stop processing known gaps")
|
|
|
|
}
|
|
|
|
}
|
2022-04-26 17:57:01 +00:00
|
|
|
return err
|
|
|
|
},
|
|
|
|
})
|
2022-06-03 16:47:13 +00:00
|
|
|
}
|
2022-04-27 14:28:42 +00:00
|
|
|
|
2022-06-03 16:47:13 +00:00
|
|
|
// Wrapper function for shutting down the head tracking process.
|
|
|
|
func ShutdownHistoricProcessing(ctx context.Context, notifierCh chan os.Signal, waitTime time.Duration, DB sql.Database, BC *beaconclient.BeaconClient) error {
|
|
|
|
return ShutdownServices(ctx, notifierCh, waitTime, DB, BC, map[string]gracefulshutdown.Operation{
|
|
|
|
// Combining DB shutdown with BC because BC needs DB open to cleanly shutdown.
|
|
|
|
"beaconClient": func(ctx context.Context) error {
|
|
|
|
defer DB.Close()
|
|
|
|
err := BC.StopHistoric()
|
|
|
|
if err != nil {
|
|
|
|
loghelper.LogError(err).Error("Unable to stop processing historic")
|
|
|
|
}
|
|
|
|
if BC.KnownGapsProcess != (beaconclient.KnownGapsProcessing{}) {
|
|
|
|
err = BC.StopKnownGapsProcessing()
|
|
|
|
if err != nil {
|
|
|
|
loghelper.LogError(err).Error("Unable to stop processing known gaps")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrapper function for shutting down the application in boot mode.
|
|
|
|
func ShutdownBoot(ctx context.Context, notifierCh chan os.Signal, waitTime time.Duration, DB sql.Database, BC *beaconclient.BeaconClient) error {
|
|
|
|
return ShutdownServices(ctx, notifierCh, waitTime, DB, BC, map[string]gracefulshutdown.Operation{
|
|
|
|
// Combining DB shutdown with BC because BC needs DB open to cleanly shutdown.
|
|
|
|
"Database": func(ctx context.Context) error {
|
|
|
|
return DB.Close()
|
|
|
|
},
|
|
|
|
})
|
2022-04-26 17:57:01 +00:00
|
|
|
}
|