-
Notifications
You must be signed in to change notification settings - Fork 324
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
[pkg] panic if no space is left #3636
Changes from 5 commits
6dc29de
0bbce28
d0d57a4
e4688a8
7c3bf2d
dc643c7
2d5eac7
0fd1091
9f3b564
2d38686
259f084
7919ab0
41f1a09
b3d8b12
b6bbdd2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) 2022 IoTeX Foundation | ||
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no | ||
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent | ||
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache | ||
// License 2.0 that can be found in the LICENSE file. | ||
|
||
package watch | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/shirou/gopsutil/v3/disk" | ||
"go.uber.org/zap" | ||
|
||
"github.com/iotexproject/iotex-core/pkg/log" | ||
"github.com/iotexproject/iotex-core/pkg/routine" | ||
) | ||
|
||
// Start creates a timer task to check device per watchInternal | ||
func Start(ctx context.Context, watchInternal time.Duration) func() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. |
||
task := routine.NewRecurringTask(checkDiskSpace, watchInternal) | ||
if err := task.Start(ctx); err != nil { | ||
log.L().Panic("Failed to start watch disk space", zap.Error(err)) | ||
} | ||
return func() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why return a |
||
if err := task.Stop(ctx); err != nil { | ||
log.L().Panic("Failed to stop watch disk space.", zap.Error(err)) | ||
} | ||
} | ||
} | ||
|
||
func checkDiskSpace() { | ||
usage, err := disk.Usage("/") | ||
if err != nil { | ||
log.L().Error("Failed to get disk usage.", zap.Error(err)) | ||
return | ||
} | ||
// panic if left less than 2% | ||
if usage.UsedPercent > 98.0 || usage.InodesUsedPercent > 98.0 { | ||
log.L().Fatal("No space in device, pls clean up.", zap.Float64("UsedPercent", usage.UsedPercent), zap.Float64("InodesUsedPercent", usage.InodesUsedPercent)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) 2022 IoTeX Foundation | ||
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no | ||
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent | ||
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache | ||
// License 2.0 that can be found in the LICENSE file. | ||
|
||
package watch | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/iotexproject/iotex-core/testutil" | ||
) | ||
|
||
func TestStart(t *testing.T) { | ||
require := require.New(t) | ||
h := Start(context.Background(), 30*time.Millisecond) | ||
require.NoError(testutil.WaitUntil(100*time.Millisecond, 1*time.Second, func() (b bool, e error) { | ||
h() | ||
return true, nil | ||
})) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import ( | |
"os/signal" | ||
"strings" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/iotexproject/go-pkgs/hash" | ||
_ "go.uber.org/automaxprocs" | ||
|
@@ -31,6 +32,7 @@ import ( | |
"github.com/iotexproject/iotex-core/pkg/log" | ||
"github.com/iotexproject/iotex-core/pkg/probe" | ||
"github.com/iotexproject/iotex-core/pkg/recovery" | ||
"github.com/iotexproject/iotex-core/pkg/watch" | ||
"github.com/iotexproject/iotex-core/server/itx" | ||
) | ||
|
||
|
@@ -74,8 +76,7 @@ func init() { | |
|
||
func main() { | ||
stop := make(chan os.Signal, 1) | ||
signal.Notify(stop, os.Interrupt) | ||
signal.Notify(stop, syscall.SIGTERM) | ||
signal.Notify(stop, os.Interrupt, syscall.SIGTERM) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
stopped := make(chan struct{}) | ||
livenessCtx, livenessCancel := context.WithCancel(context.Background()) | ||
|
@@ -103,6 +104,10 @@ func main() { | |
glog.Fatalln("Cannot config global logger, use default one: ", zap.Error(err)) | ||
} | ||
|
||
// check device | ||
stopWatch := watch.Start(ctx, 5*time.Minute) | ||
defer stopWatch() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just check like heartbeat. |
||
|
||
if err = recovery.SetCrashlogDir(cfg.System.SystemLogDBPath); err != nil { | ||
glog.Fatalln("Failed to set directory of crashlog: ", zap.Error(err)) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
package disk