Skip to content
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

feat: notify own error #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ func main() {
var err error
newFilePaths, err := pkg.FilesByPattern(f.FilePath, f.NotifyOnlyRecent)
if err != nil {
pkg.NotifyOwnError(fmt.Errorf("error finding files: %s", err.Error()), f)
slog.Error("Error finding files", "error", err.Error())
return
}
if len(newFilePaths) == 0 {
pkg.NotifyOwnError(fmt.Errorf("no files found"), f)
slog.Error("No files found", "filePath", f.FilePath)
slog.Warn("Keep watching for new files")
}
Expand All @@ -55,10 +57,12 @@ func main() {
for _, filePath := range filePaths {
isText, err := pkg.IsTextFile(filePath)
if err != nil {
pkg.NotifyOwnError(fmt.Errorf("error checking if file is text: %s", err.Error()), f)
slog.Error("Error checking if file is text", "error", err.Error(), "filePath", filePath)
return
}
if !isText {
pkg.NotifyOwnError(fmt.Errorf("file is not a text file"), f)
slog.Error("File is not a text file", "filePath", filePath)
return
}
Expand All @@ -74,21 +78,25 @@ func main() {

func startCron() {
if err := gocron.Every(1).Second().Do(pkg.PrintMemUsage, &f); err != nil {
pkg.NotifyOwnError(fmt.Errorf("error scheduling memory usage: %s", err.Error()), f)
slog.Error("Error scheduling memory usage", "error", err.Error())
return
}
if err := gocron.Every(f.Every).Second().Do(syncFilePaths); err != nil {
pkg.NotifyOwnError(fmt.Errorf("error scheduling syncFilePaths: %s", err.Error()), f)
slog.Error("Error scheduling syncFilePaths", "error", err.Error())
return
}
if f.HealthCheckEvery > 0 {
if err := gocron.Every(f.HealthCheckEvery).Second().Do(sendHealthCheck); err != nil {
pkg.NotifyOwnError(fmt.Errorf("error scheduling health check: %s", err.Error()), f)
slog.Error("Error scheduling health check", "error", err.Error())
return
}
}

if err := gocron.Every(f.Every).Second().Do(cron); err != nil {
pkg.NotifyOwnError(fmt.Errorf("error scheduling cron: %s", err.Error()), f)
slog.Error("Error scheduling cron", "error", err.Error())
return
}
Expand All @@ -104,6 +112,7 @@ func cron() {
}
if f.PostAlways != "" {
if _, err := pkg.ExecShell(f.PostAlways); err != nil {
pkg.NotifyOwnError(fmt.Errorf("error running post always command: %s", err.Error()), f)
slog.Error("Error running post command", "error", err.Error())
}
}
Expand All @@ -113,10 +122,12 @@ func syncFilePaths() {
var err error
newFilePaths, err := pkg.FilesByPattern(f.FilePath, f.NotifyOnlyRecent)
if err != nil {
pkg.NotifyOwnError(fmt.Errorf("error finding files: %s", err.Error()), f)
slog.Error("Error finding files", "error", err.Error())
return
}
if len(newFilePaths) == 0 {
pkg.NotifyOwnError(fmt.Errorf("no files found"), f)
slog.Error("No files found", "filePath", f.FilePath)
slog.Warn("Keep watching for new files")
return
Expand Down Expand Up @@ -159,6 +170,7 @@ func sendHealthCheck() {

func validate() {
if f.FilePath == "" {
pkg.NotifyOwnError(fmt.Errorf("file-path is required"), f)
slog.Error("file-path is required")
os.Exit(1)
}
Expand All @@ -168,6 +180,7 @@ func watch(filePath string) {
watcher, err := pkg.NewWatcher(filePath, f)

if err != nil {
pkg.NotifyOwnError(fmt.Errorf("error creating watcher: %s", err.Error()), f)
slog.Error("Error creating watcher", "error", err.Error(), "filePath", filePath)
return
}
Expand All @@ -177,6 +190,7 @@ func watch(filePath string) {

result, err := watcher.Scan()
if err != nil {
pkg.NotifyOwnError(fmt.Errorf("error scanning file: %s", err.Error()), f)
slog.Error("Error scanning file", "error", err.Error(), "filePath", filePath)
return
}
Expand All @@ -202,6 +216,7 @@ func watch(filePath string) {
}
if f.PostCommand != "" {
if _, err := pkg.ExecShell(f.PostCommand); err != nil {
pkg.NotifyOwnError(fmt.Errorf("error running post command: %s", err.Error()), f)
slog.Error("Error running post command", "error", err.Error())
}
}
Expand Down
Loading