Skip to content
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ $ go run main.go run testkey 'echo "hello world"' -t loomctl-locks
{"level":"info","ts":1651558192.944402,"caller":"cmd/run.go:33","msg":"command succeeded","key":"testkey","command":"echo \"hello world\"","output":"\"hello world\"\n"}
```

Once the command finishes running `lock-exec` will unlock the key. It also listens for os interrupts and unlocks the key before exiting. In the rare case where `lock-exec` exits and fails to unlock the key will remain locked for the next 24 hours. The key can manually be unlocked earlier using `lock-exec unlock <key>`.
Once the command finishes running `lock-exec` will unlock the key. It also listens for os interrupts and unlocks the key before exiting. In the rare case where `lock-exec` exits and fails to unlock the key will remain locked for the next 24 hours (you can customize this with `--expire <duration>`). The key can manually be unlocked earlier using `lock-exec unlock <key>`.
2 changes: 2 additions & 0 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os/signal"
"syscall"
"time"

"github.com/spf13/cobra"
"go.uber.org/zap"
Expand All @@ -22,6 +23,7 @@ type cli struct {
log *zap.SugaredLogger

table string
expire time.Duration
version string
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"time"

"github.com/spf13/cobra"
)

Expand All @@ -15,6 +17,7 @@ func (c *cli) newRootCmd() *cobra.Command {
}

cmd.PersistentFlags().StringVarP(&c.table, "table", "t", "lock-exec", "table name in dynamodb to use for locking")
cmd.PersistentFlags().DurationVarP(&c.expire, "expire", "e", time.Hour*24, "lock duration in the event that the post-run unlock fails") //nolint:gomnd

cmd.AddCommand(
c.newRunCmd(),
Expand Down
1 change: 1 addition & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (c *cli) newRunCmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
log := c.log.With("key", args[0], "command", args[1])
locker := c.newLocker()
locker.SetExpire(c.expire)

log.Info("running command")
err := locker.Run(c.cmd.Context(), args[0], args[1])
Expand Down
13 changes: 12 additions & 1 deletion lock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lock
import (
"context"
"errors"
"time"

"github.com/aws/aws-sdk-go-v2/service/dynamodb"
)
Expand All @@ -23,9 +24,19 @@ type storageI interface {
type Client struct {
storage storageI
table string
expire time.Duration
}

// New creates a new lock client.
func New(ddb storageI, table string) *Client {
return &Client{storage: ddb, table: table}
return &Client{
storage: ddb,
table: table,
expire: time.Hour * 24, //nolint:gomnd
}
}

// SetExpire sets the default expire duration for locks. Defaults to 24 hours if not set.
func (c *Client) SetExpire(d time.Duration) {
c.expire = d
}
3 changes: 1 addition & 2 deletions lock/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"os/exec"
"strings"
"time"
)

// Run acquires a lock under the specified key, executes the command, and then unlocks the key.
Expand All @@ -16,7 +15,7 @@ func (c *Client) Run(ctx context.Context, key, command string) error {
// use context.Background here so that unlock runs even if the context is cancelled
defer c.Unlock(context.Background(), key) //nolint:errcheck,contextcheck

err := c.Lock(ctx, key, time.Hour*24) //nolint:gomnd
err := c.Lock(ctx, key, c.expire)
if err != nil {
return fmt.Errorf("lock failed: %w", err)
}
Expand Down