Skip to content

panic handling #6588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package main

import (
"github.com/devtron-labs/authenticator/middleware"
async "github.com/devtron-labs/common-lib/async"
cloudProviderIdentifier "github.com/devtron-labs/common-lib/cloud-provider-identifier"
pubsub1 "github.com/devtron-labs/common-lib/pubsub-lib"
util4 "github.com/devtron-labs/common-lib/utils/k8s"
Expand Down Expand Up @@ -245,7 +246,7 @@ func InitializeApp() (*App, error) {
wire.Bind(new(chartConfig.PipelineOverrideRepository), new(*chartConfig.PipelineOverrideRepositoryImpl)),
wire.Struct(new(util.MergeUtil), "*"),
util.NewSugardLogger,

async.NewAsync,
deployment.NewDeploymentConfigRestHandlerImpl,
wire.Bind(new(deployment.DeploymentConfigRestHandler), new(*deployment.DeploymentConfigRestHandlerImpl)),
deployment.NewDeploymentRouterImpl,
Expand Down
52 changes: 31 additions & 21 deletions api/appStore/deployment/AppStoreDeploymentRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/devtron-labs/common-lib/async"
service2 "github.com/devtron-labs/devtron/api/helm-app/service"
"github.com/devtron-labs/devtron/pkg/appStore/installedApp/service/EAMode"
"net/http"
Expand Down Expand Up @@ -70,6 +71,7 @@ type AppStoreDeploymentRestHandlerImpl struct {
argoUserService argo.ArgoUserService
attributesService attributes.AttributesService
installAppService EAMode.InstalledAppDBService
async *async.Async
}

func NewAppStoreDeploymentRestHandlerImpl(Logger *zap.SugaredLogger, userAuthService user.UserService,
Expand All @@ -78,6 +80,7 @@ func NewAppStoreDeploymentRestHandlerImpl(Logger *zap.SugaredLogger, userAuthSer
appStoreDeploymentDBService service.AppStoreDeploymentDBService,
validator *validator.Validate, helmAppService service2.HelmAppService,
argoUserService argo.ArgoUserService, attributesService attributes.AttributesService,
async *async.Async,
installAppService EAMode.InstalledAppDBService) *AppStoreDeploymentRestHandlerImpl {
return &AppStoreDeploymentRestHandlerImpl{
Logger: Logger,
Expand All @@ -92,6 +95,7 @@ func NewAppStoreDeploymentRestHandlerImpl(Logger *zap.SugaredLogger, userAuthSer
argoUserService: argoUserService,
attributesService: attributesService,
installAppService: installAppService,
async: async,
}
}

Expand Down Expand Up @@ -158,13 +162,15 @@ func (handler AppStoreDeploymentRestHandlerImpl) InstallApp(w http.ResponseWrite
handler.Logger.Infow("request payload, CreateInstalledApp", "payload", request)
ctx, cancel := context.WithCancel(r.Context())
if cn, ok := w.(http.CloseNotifier); ok {
go func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
handler.async.RunAsync(func() {
func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
})
}
if util2.IsBaseStack() || util2.IsHelmApp(request.AppOfferingMode) {
ctx = context.WithValue(r.Context(), "token", token)
Expand Down Expand Up @@ -341,13 +347,15 @@ func (handler AppStoreDeploymentRestHandlerImpl) DeleteInstalledApp(w http.Respo
request.AcdPartialDelete = partialDelete
ctx, cancel := context.WithCancel(r.Context())
if cn, ok := w.(http.CloseNotifier); ok {
go func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
handler.async.RunAsync(func() {
func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
})
}
if util2.IsBaseStack() || util2.IsHelmApp(request.AppOfferingMode) {
ctx = context.WithValue(r.Context(), "token", token)
Expand Down Expand Up @@ -469,13 +477,15 @@ func (handler AppStoreDeploymentRestHandlerImpl) UpdateInstalledApp(w http.Respo
request.UserId = userId
ctx, cancel := context.WithCancel(r.Context())
if cn, ok := w.(http.CloseNotifier); ok {
go func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
handler.async.RunAsync(func() {
func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
})
}
if util2.IsBaseStack() || util2.IsHelmApp(request.AppOfferingMode) {
ctx = context.WithValue(r.Context(), "token", token)
Expand Down
68 changes: 40 additions & 28 deletions api/cluster/ClusterRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"errors"
"github.com/devtron-labs/common-lib/async"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -71,6 +72,7 @@ type ClusterRestHandlerImpl struct {
argoUserService argo.ArgoUserService
environmentService cluster.EnvironmentService
clusterRbacService cluster.ClusterRbacService
async *async.Async
}

func NewClusterRestHandlerImpl(clusterService cluster.ClusterService,
Expand All @@ -83,6 +85,7 @@ func NewClusterRestHandlerImpl(clusterService cluster.ClusterService,
deleteService delete2.DeleteService,
argoUserService argo.ArgoUserService,
environmentService cluster.EnvironmentService,
async *async.Async,
clusterRbacService cluster.ClusterRbacService) *ClusterRestHandlerImpl {
return &ClusterRestHandlerImpl{
clusterService: clusterService,
Expand All @@ -96,6 +99,7 @@ func NewClusterRestHandlerImpl(clusterService cluster.ClusterService,
argoUserService: argoUserService,
environmentService: environmentService,
clusterRbacService: clusterRbacService,
async: async,
}
}

Expand Down Expand Up @@ -125,13 +129,15 @@ func (impl ClusterRestHandlerImpl) SaveClusters(w http.ResponseWriter, r *http.R
//RBAC enforcer Ends
ctx, cancel := context.WithCancel(r.Context())
if cn, ok := w.(http.CloseNotifier); ok {
go func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
impl.async.RunAsync(func() {
func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
})
}
if util2.IsBaseStack() {
ctx = context.WithValue(ctx, "token", token)
Expand Down Expand Up @@ -201,13 +207,15 @@ func (impl ClusterRestHandlerImpl) Save(w http.ResponseWriter, r *http.Request)
//RBAC enforcer Ends
ctx, cancel := context.WithCancel(r.Context())
if cn, ok := w.(http.CloseNotifier); ok {
go func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
impl.async.RunAsync(func() {
func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
})
}
if util2.IsBaseStack() {
ctx = context.WithValue(ctx, "token", token)
Expand Down Expand Up @@ -270,13 +278,15 @@ func (impl ClusterRestHandlerImpl) ValidateKubeconfig(w http.ResponseWriter, r *
//RBAC enforcer Ends
ctx, cancel := context.WithCancel(r.Context())
if cn, ok := w.(http.CloseNotifier); ok {
go func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
impl.async.RunAsync(func() {
func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
})
}
if util2.IsBaseStack() {
ctx = context.WithValue(ctx, "token", token)
Expand Down Expand Up @@ -420,13 +430,15 @@ func (impl ClusterRestHandlerImpl) Update(w http.ResponseWriter, r *http.Request
// RBAC enforcer ends
ctx, cancel := context.WithCancel(r.Context())
if cn, ok := w.(http.CloseNotifier); ok {
go func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
impl.async.RunAsync(func() {
func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
})
}
if util2.IsBaseStack() {
ctx = context.WithValue(ctx, "token", token)
Expand Down
37 changes: 22 additions & 15 deletions api/k8s/application/k8sApplicationRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/devtron-labs/common-lib/async"
"github.com/devtron-labs/common-lib/utils"
util3 "github.com/devtron-labs/common-lib/utils/k8s"
k8sCommonBean "github.com/devtron-labs/common-lib/utils/k8s/commonBean"
Expand Down Expand Up @@ -90,9 +91,10 @@ type K8sApplicationRestHandlerImpl struct {
userService user.UserService
k8sCommonService k8s.K8sCommonService
terminalEnvVariables *util.TerminalEnvVariables
async *async.Async
}

func NewK8sApplicationRestHandlerImpl(logger *zap.SugaredLogger, k8sApplicationService application2.K8sApplicationService, pump connector.Pump, terminalSessionHandler terminal.TerminalSessionHandler, enforcer casbin.Enforcer, enforcerUtilHelm rbac.EnforcerUtilHelm, enforcerUtil rbac.EnforcerUtil, helmAppService client.HelmAppService, userService user.UserService, k8sCommonService k8s.K8sCommonService, validator *validator.Validate, envVariables *util.EnvironmentVariables) *K8sApplicationRestHandlerImpl {
func NewK8sApplicationRestHandlerImpl(logger *zap.SugaredLogger, k8sApplicationService application2.K8sApplicationService, pump connector.Pump, terminalSessionHandler terminal.TerminalSessionHandler, enforcer casbin.Enforcer, enforcerUtilHelm rbac.EnforcerUtilHelm, enforcerUtil rbac.EnforcerUtil, helmAppService client.HelmAppService, userService user.UserService, k8sCommonService k8s.K8sCommonService, validator *validator.Validate, envVariables *util.EnvironmentVariables, async *async.Async) *K8sApplicationRestHandlerImpl {
return &K8sApplicationRestHandlerImpl{
logger: logger,
k8sApplicationService: k8sApplicationService,
Expand All @@ -106,6 +108,7 @@ func NewK8sApplicationRestHandlerImpl(logger *zap.SugaredLogger, k8sApplicationS
userService: userService,
k8sCommonService: k8sCommonService,
terminalEnvVariables: envVariables.TerminalEnvVariables,
async: async,
}
}

Expand Down Expand Up @@ -671,13 +674,15 @@ func (handler *K8sApplicationRestHandlerImpl) GetPodLogs(w http.ResponseWriter,
//err is handled inside StartK8sStreamWithHeartBeat method
ctx, cancel := context.WithCancel(r.Context())
if cn, ok := w.(http.CloseNotifier); ok {
go func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
handler.async.RunAsync(func() {
func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
})
}
defer cancel()
defer util.Close(stream, handler.logger)
Expand All @@ -703,13 +708,15 @@ func (handler *K8sApplicationRestHandlerImpl) DownloadPodLogs(w http.ResponseWri
}
ctx, cancel := context.WithCancel(r.Context())
if cn, ok := w.(http.CloseNotifier); ok {
go func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
handler.async.RunAsync(func() {
func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
})
}
defer cancel()
defer util.Close(stream, handler.logger)
Expand Down
6 changes: 4 additions & 2 deletions cmd/external-app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"fmt"
async2 "github.com/devtron-labs/common-lib/async"
"log"
"os"
"os/signal"
Expand All @@ -33,12 +34,13 @@ func main() {
var gracefulStop = make(chan os.Signal)
signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
go func() {
async := async2.NewAsync(nil)
async.RunAsync(func() {
sig := <-gracefulStop
fmt.Printf("caught sig: %+v", sig)
app.Stop()
os.Exit(0)
}()
})
// gracefulStop end
app.Start()
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/deckarep/golang-set v1.8.0
github.com/devtron-labs/authenticator v0.4.35-0.20240607135426-c86e868ecee1
github.com/devtron-labs/common-lib v0.0.19-0.20240612123206-80cb4388edf6
github.com/devtron-labs/common-lib v0.0.19-0.20240613124451-4ffa8b6df744
github.com/devtron-labs/protos v0.0.3-0.20240326053929-48e42d9d4534
github.com/evanphx/json-patch v5.7.0+incompatible
github.com/gammazero/workerpool v1.1.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ github.com/denisenkom/go-mssqldb v0.0.0-20190707035753-2be1aa521ff4 h1:YcpmyvADG
github.com/denisenkom/go-mssqldb v0.0.0-20190707035753-2be1aa521ff4/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM=
github.com/devtron-labs/authenticator v0.4.35-0.20240607135426-c86e868ecee1 h1:qdkpTAo2Kr0ZicZIVXfNwsGSshpc9OB9j9RzmKYdIwY=
github.com/devtron-labs/authenticator v0.4.35-0.20240607135426-c86e868ecee1/go.mod h1:IkKPPEfgLCMR29he5yv2OCC6iM2R7K5/0AA3k8b9XNc=
github.com/devtron-labs/common-lib v0.0.19-0.20240612123206-80cb4388edf6 h1:Rg/td8sit6zUw3zO67FBkdSeEPbTz8lBuaeiC98KKHU=
github.com/devtron-labs/common-lib v0.0.19-0.20240612123206-80cb4388edf6/go.mod h1:UZGPt1ep9Tnd9Ak2sibGSiLr7p3ijO2/JLT+h+pqBuU=
github.com/devtron-labs/common-lib v0.0.19-0.20240613124451-4ffa8b6df744 h1:55uBP+CR4/YZmO1DgUZcFWnq5qEX/cNKrLN7XiBmXy8=
github.com/devtron-labs/common-lib v0.0.19-0.20240613124451-4ffa8b6df744/go.mod h1:UZGPt1ep9Tnd9Ak2sibGSiLr7p3ijO2/JLT+h+pqBuU=
github.com/devtron-labs/protos v0.0.3-0.20240326053929-48e42d9d4534 h1:TElPRU69QedW7DIQiiQxtjwSQ6cK0fCTAMGvSLhP0ac=
github.com/devtron-labs/protos v0.0.3-0.20240326053929-48e42d9d4534/go.mod h1:ypUknVph8Ph4dxSlrFoouf7wLedQxHku2LQwgRrdgS4=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
_ "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
_ "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/devtron-labs/common-lib/async"
util2 "github.com/devtron-labs/devtron/util"
"log"
"os"
Expand All @@ -45,12 +46,13 @@ func main() {
var gracefulStop = make(chan os.Signal)
signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
go func() {
async := async.NewAsync(nil)
async.RunAsync(func() {
sig := <-gracefulStop
fmt.Printf("caught sig: %+v", sig)
app.Stop()
os.Exit(0)
}()
})
// gracefulStop end

app.Start()
Expand Down
Loading