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

Log unhandled errors if they occur #508

Open
wants to merge 1 commit into
base: master
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
4 changes: 2 additions & 2 deletions cmd/mutagen-agent/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func housekeepRegularly(ctx context.Context, logger *logging.Logger) {
// Perform an initial housekeeping operation since the ticker won't fire
// straight away.
logger.Info("Performing initial housekeeping")
housekeeping.Housekeep()
housekeeping.Housekeep(logger)

// Create a ticker to regulate housekeeping and defer its shutdown.
ticker := time.NewTicker(housekeepingInterval)
Expand All @@ -42,7 +42,7 @@ func housekeepRegularly(ctx context.Context, logger *logging.Logger) {
return
case <-ticker.C:
logger.Info("Performing regular housekeeping")
housekeeping.Housekeep()
housekeeping.Housekeep(logger)
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/mutagen/daemon/register.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package daemon

import (
"os"

"github.com/mutagen-io/mutagen/pkg/logging"
"github.com/spf13/cobra"

"github.com/mutagen-io/mutagen/cmd"
Expand All @@ -10,7 +13,8 @@ import (

// registerMain is the entry point for the register command.
func registerMain(_ *cobra.Command, _ []string) error {
return daemon.Register()
logger := logging.NewLogger(logging.LevelError, os.Stderr)
return daemon.Register(logger)
}

// registerCommand is the register command.
Expand Down
32 changes: 17 additions & 15 deletions cmd/mutagen/daemon/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/signal"

"github.com/mutagen-io/mutagen/pkg/must"
"github.com/spf13/cobra"

"google.golang.org/grpc"
Expand All @@ -27,18 +28,6 @@ import (

// runMain is the entry point for the run command.
func runMain(_ *cobra.Command, _ []string) error {
// Attempt to acquire the daemon lock and defer its release.
lock, err := daemon.AcquireLock()
if err != nil {
return fmt.Errorf("unable to acquire daemon lock: %w", err)
}
defer lock.Release()

// Create a channel to track termination signals. We do this before creating
// and starting other infrastructure so that we can ensure things terminate
// smoothly, not mid-initialization.
signalTermination := make(chan os.Signal, 1)
signal.Notify(signalTermination, cmd.TerminationSignals...)

// Create the root logger.
logLevel := logging.LevelInfo
Expand All @@ -51,6 +40,19 @@ func runMain(_ *cobra.Command, _ []string) error {
}
logger := logging.NewLogger(logLevel, os.Stderr)

// Attempt to acquire the daemon lock and defer its release.
lock, err := daemon.AcquireLock(logger)
if err != nil {
return fmt.Errorf("unable to acquire daemon lock: %w", err)
}
defer must.Release(lock, logger)

// Create a channel to track termination signals. We do this before creating
// and starting other infrastructure so that we can ensure things terminate
// smoothly, not mid-initialization.
signalTermination := make(chan os.Signal, 1)
signal.Notify(signalTermination, cmd.TerminationSignals...)

// Create a forwarding session manager and defer its shutdown.
forwardingManager, err := forwarding.NewManager(logger.Sublogger("forward"))
if err != nil {
Expand All @@ -74,7 +76,7 @@ func runMain(_ *cobra.Command, _ []string) error {
defer server.Stop()

// Create the daemon server, defer its shutdown, and register it.
daemonServer := daemonsvc.NewServer()
daemonServer := daemonsvc.NewServer(logger)
defer daemonServer.Shutdown()
daemonsvc.RegisterDaemonServer(server, daemonServer)

Expand All @@ -101,11 +103,11 @@ func runMain(_ *cobra.Command, _ []string) error {
if err := os.Remove(endpoint); err != nil && !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("unable to remove existing daemon endpoint: %w", err)
}
listener, err := ipc.NewListener(endpoint)
listener, err := ipc.NewListener(endpoint, logger)
if err != nil {
return fmt.Errorf("unable to create daemon listener: %w", err)
}
defer listener.Close()
defer must.Close(listener, logger)

// Serve incoming requests and watch for server failure.
serverErrors := make(chan error, 1)
Expand Down
7 changes: 5 additions & 2 deletions cmd/mutagen/daemon/unregister.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package daemon

import (
"os"

"github.com/spf13/cobra"

"github.com/mutagen-io/mutagen/cmd"

"github.com/mutagen-io/mutagen/pkg/daemon"
"github.com/mutagen-io/mutagen/pkg/logging"
)

// unregisterMain is the entry point for the unregister command.
func unregisterMain(_ *cobra.Command, _ []string) error {
return daemon.Unregister()
logger := logging.NewLogger(logging.LevelError, os.Stderr)
return daemon.Unregister(logger)
}

// unregisterCommand is the unregister command.
Expand Down
18 changes: 11 additions & 7 deletions cmd/mutagen/project/flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path/filepath"
"runtime"

"github.com/mutagen-io/mutagen/pkg/logging"
"github.com/mutagen-io/mutagen/pkg/must"
"github.com/spf13/cobra"

"github.com/mutagen-io/mutagen/cmd"
Expand All @@ -22,6 +24,8 @@ import (

// flushMain is the entry point for the flush command.
func flushMain(_ *cobra.Command, _ []string) error {
logger := logging.NewLogger(logging.LevelError, os.Stderr)

// Compute the name of the configuration file and ensure that our working
// directory is that in which the file resides. This is required for
// relative paths (including relative synchronization paths and relative
Expand Down Expand Up @@ -51,9 +55,9 @@ func flushMain(_ *cobra.Command, _ []string) error {
return fmt.Errorf("unable to create project locker: %w", err)
}
defer func() {
locker.Close()
must.Close(locker, logger)
if removeLockFileOnReturn && runtime.GOOS == "windows" {
os.Remove(lockPath)
must.OSRemove(lockPath, logger)
}
}()

Expand All @@ -70,12 +74,12 @@ func flushMain(_ *cobra.Command, _ []string) error {
defer func() {
if removeLockFileOnReturn {
if runtime.GOOS == "windows" {
locker.Truncate(0)
must.Truncate(locker, 0, logger)
} else {
os.Remove(lockPath)
must.OSRemove(lockPath, logger)
}
}
locker.Unlock()
must.Unlock(locker, logger)
}()

// Read the project identifier from the lock file. If the lock file is
Expand All @@ -100,7 +104,7 @@ func flushMain(_ *cobra.Command, _ []string) error {
if err != nil {
return fmt.Errorf("unable to connect to daemon: %w", err)
}
defer daemonConnection.Close()
defer must.Close(daemonConnection, logger)

// Compute the selection that we're going to use to flush sessions.
selection := &selection.Selection{
Expand Down Expand Up @@ -132,7 +136,7 @@ var flushConfiguration struct {
// projectFile is the path to the project file, if non-default.
projectFile string
// skipWait indicates whether or not the flush operation should block until
// a synchronization cycle completes for each sesion requested.
// a synchronization cycle completes for each session requested.
skipWait bool
}

Expand Down
16 changes: 10 additions & 6 deletions cmd/mutagen/project/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path/filepath"
"runtime"

"github.com/mutagen-io/mutagen/pkg/logging"
"github.com/mutagen-io/mutagen/pkg/must"
"github.com/spf13/cobra"

"github.com/mutagen-io/mutagen/cmd"
Expand All @@ -23,6 +25,8 @@ import (

// listMain is the entry point for the list command.
func listMain(_ *cobra.Command, _ []string) error {
logger := logging.NewLogger(logging.LevelError, &bytes.Buffer{})

// Compute the name of the configuration file and ensure that our working
// directory is that in which the file resides. This is required for
// relative paths (including relative synchronization paths and relative
Expand Down Expand Up @@ -52,9 +56,9 @@ func listMain(_ *cobra.Command, _ []string) error {
return fmt.Errorf("unable to create project locker: %w", err)
}
defer func() {
locker.Close()
must.Close(locker, logger)
if removeLockFileOnReturn && runtime.GOOS == "windows" {
os.Remove(lockPath)
must.OSRemove(lockPath, logger)
}
}()

Expand All @@ -71,12 +75,12 @@ func listMain(_ *cobra.Command, _ []string) error {
defer func() {
if removeLockFileOnReturn {
if runtime.GOOS == "windows" {
locker.Truncate(0)
must.Truncate(locker, 0, logger)
} else {
os.Remove(lockPath)
must.OSRemove(lockPath, logger)
}
}
locker.Unlock()
must.Unlock(locker, logger)
}()

// Read the project identifier from the lock file. If the lock file is
Expand All @@ -101,7 +105,7 @@ func listMain(_ *cobra.Command, _ []string) error {
if err != nil {
return fmt.Errorf("unable to connect to daemon: %w", err)
}
defer daemonConnection.Close()
defer must.Close(daemonConnection, logger)

// Compute the selection that we're going to use to list sessions.
selection := &selection.Selection{
Expand Down
16 changes: 10 additions & 6 deletions cmd/mutagen/project/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path/filepath"
"runtime"

"github.com/mutagen-io/mutagen/pkg/logging"
"github.com/mutagen-io/mutagen/pkg/must"
"github.com/spf13/cobra"

"github.com/mutagen-io/mutagen/cmd"
Expand All @@ -23,6 +25,8 @@ import (

// pauseMain is the entry point for the pause command.
func pauseMain(_ *cobra.Command, _ []string) error {
logger := logging.NewLogger(logging.LevelError, os.Stderr)

// Compute the name of the configuration file and ensure that our working
// directory is that in which the file resides. This is required for
// relative paths (including relative synchronization paths and relative
Expand Down Expand Up @@ -52,9 +56,9 @@ func pauseMain(_ *cobra.Command, _ []string) error {
return fmt.Errorf("unable to create project locker: %w", err)
}
defer func() {
locker.Close()
must.Close(locker, logger)
if removeLockFileOnReturn && runtime.GOOS == "windows" {
os.Remove(lockPath)
must.OSRemove(lockPath, logger)
}
}()

Expand All @@ -71,12 +75,12 @@ func pauseMain(_ *cobra.Command, _ []string) error {
defer func() {
if removeLockFileOnReturn {
if runtime.GOOS == "windows" {
locker.Truncate(0)
must.Truncate(locker, 0, logger)
} else {
os.Remove(lockPath)
must.OSRemove(lockPath, logger)
}
}
locker.Unlock()
must.Unlock(locker, logger)
}()

// Read the project identifier from the lock file. If the lock file is
Expand Down Expand Up @@ -115,7 +119,7 @@ func pauseMain(_ *cobra.Command, _ []string) error {
if err != nil {
return fmt.Errorf("unable to connect to daemon: %w", err)
}
defer daemonConnection.Close()
defer must.Close(daemonConnection, logger)

// Compute the selection that we're going to use to pause sessions.
selection := &selection.Selection{
Expand Down
15 changes: 9 additions & 6 deletions cmd/mutagen/project/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"path/filepath"
"runtime"

"github.com/mutagen-io/mutagen/pkg/logging"
"github.com/mutagen-io/mutagen/pkg/must"
"github.com/spf13/cobra"

"github.com/mutagen-io/mutagen/cmd"
Expand All @@ -22,6 +24,7 @@ import (

// resetMain is the entry point for the reset command.
func resetMain(_ *cobra.Command, _ []string) error {
logger := logging.NewLogger(logging.LevelError, os.Stderr)
// Compute the name of the configuration file and ensure that our working
// directory is that in which the file resides. This is required for
// relative paths (including relative synchronization paths and relative
Expand Down Expand Up @@ -51,9 +54,9 @@ func resetMain(_ *cobra.Command, _ []string) error {
return fmt.Errorf("unable to create project locker: %w", err)
}
defer func() {
locker.Close()
must.Close(locker, logger)
if removeLockFileOnReturn && runtime.GOOS == "windows" {
os.Remove(lockPath)
must.OSRemove(lockPath, logger)
}
}()

Expand All @@ -70,12 +73,12 @@ func resetMain(_ *cobra.Command, _ []string) error {
defer func() {
if removeLockFileOnReturn {
if runtime.GOOS == "windows" {
locker.Truncate(0)
must.Truncate(locker, 0, logger)
} else {
os.Remove(lockPath)
must.OSRemove(lockPath, logger)
}
}
locker.Unlock()
must.Unlock(locker, logger)
}()

// Read the project identifier from the lock file. If the lock file is
Expand All @@ -100,7 +103,7 @@ func resetMain(_ *cobra.Command, _ []string) error {
if err != nil {
return fmt.Errorf("unable to connect to daemon: %w", err)
}
defer daemonConnection.Close()
defer must.Close(daemonConnection, logger)

// Compute the selection that we're going to use to reset sessions.
selection := &selection.Selection{
Expand Down
Loading