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 .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2.1

orbs:
go: circleci/go@1.3.0
loom-go: loomhq/go@0.2.0
loom-go: loomhq/go@5.0.0
loom-docker: loomhq/docker@0.0.5
docker: circleci/docker@1.0.1

Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var keyName string
var tableName string
var regionName string

// rootCmd represents the base command when called without any subcommands
// rootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{
Use: "lock-exec",
Short: "A CLI tool for running any shell based commands in a distributed environment with DynamoDB locking",
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var shellCommand string
var sleepStartRandom int
var holdLockBy int

// runCmd represents the run command
// runCmd represents the run command.
var runCmd = &cobra.Command{
Use: "run",
Short: "Run a shell command with acquire lock",
Expand Down
2 changes: 1 addition & 1 deletion cmd/unlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

// unlockCmd represents the unlock command
// unlockCmd represents the unlock command.
var unlockCmd = &cobra.Command{
Use: "unlock",
Short: "Force release an already acquired lock using key name",
Expand Down
4 changes: 2 additions & 2 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
)

var (
// Version is defined at compile time via -ldflags
// Version is defined at compile time via -ldflags.
Version = "undefined1"
)

// unlockCmd represents the unlock command
// unlockCmd represents the unlock command.
var versionkCmd = &cobra.Command{
Use: "version",
Short: "Print version",
Expand Down
16 changes: 8 additions & 8 deletions exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ const (

var execCommand = exec.Command

// Exec is a struct used to work with dynamo client interface
// Exec is a struct used to work with dynamo client interface.
type Exec struct {
dynamodbiface.DynamoDBAPI
}

// NewDynamoClient returns a Dynamo struct with client
// session
// NewDynamoClient returns a Dynamo struct with client session.
func NewDynamoClient(awsRegionName string) (*Exec, error) {
sess := utils.AWSSession(awsRegionName)
dynamoClient := dynamodb.New(sess)
Expand All @@ -45,7 +44,7 @@ func NewDynamoClient(awsRegionName string) (*Exec, error) {
// If sleepStartRandom & holdLockBy have non-zero values, it accordingly
// introduces randomized sleep before start and holds the lock by that duration
// before stop.
func (d *Exec) Run(tableName string, keyName string, command string, sleepStartRandom int, holdLockBy int) error {
func (d *Exec) Run(tableName string, keyName string, command string, sleepStartRandom int, holdLockBy int) error { //nolint:funlen
if sleepStartRandom > 0 {
randomizeSleep(sleepStartRandom)
}
Expand Down Expand Up @@ -74,6 +73,7 @@ func (d *Exec) Run(tableName string, keyName string, command string, sleepStartR

// Exit early, just not as an error.
logrus.Warning(err)

return nil
}
logrus.Info("Lock acquired")
Expand Down Expand Up @@ -117,7 +117,7 @@ func (d *Exec) Run(tableName string, keyName string, command string, sleepStartR
}

// RunCommand executes the command.
// It returns the output into STDOUT
// It returns the output into STDOUT.
func runExec(command string) (string, error) {
args := strings.Fields(command)

Expand All @@ -126,8 +126,8 @@ func runExec(command string) (string, error) {
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
return string(stdoutStderr), err

}

return string(stdoutStderr), nil
}

Expand All @@ -137,14 +137,14 @@ func randomizeSleep(i int) {
rand.Seed(time.Now().UnixNano())
ms := i * 1000
offsetMS := 100 // milliseconds
r := minRandomSleep + rand.Intn(ms-minRandomSleep+offsetMS) // ensures that the random range is [1, i]. To avoid sleeping for 0 seconds (0 ms).
r := minRandomSleep + rand.Intn(ms-minRandomSleep+offsetMS) //nolint:gosec // ensures that the random range is [1, i]. To avoid sleeping for 0 seconds (0 ms).

logrus.Infof("Randomized execution. Sleeping for %d milliseconds.", r)

time.Sleep(time.Duration(r) * time.Millisecond)
}

// sleepBy takes a int input, and sleeps for that duration
// sleepBy takes a int input, and sleeps for that duration.
func sleepBy(i int) {
logrus.Infof("Holding lock - Sleeping for %d seconds.", i)

Expand Down
9 changes: 4 additions & 5 deletions lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (
"github.com/sirupsen/logrus"
)

// Dynamo is a struct used to work with dynamo client interface
// Dynamo is a struct used to work with dynamo client interface.
type Dynamo struct {
dynamodbiface.DynamoDBAPI
}

// NewDynamoClient returns a Dynamo struct with client
// session
// NewDynamoClient returns a Dynamo struct with client session.
func NewDynamoClient(awsRegionName string) *Dynamo {
sess := utils.AWSSession(awsRegionName)

Expand All @@ -35,14 +34,14 @@ func (d *Dynamo) ReleaseLock(keyName string, tableName string) error {
TableName: aws.String(tableName),
}

_, err := d.DeleteItem(input)
if err != nil {
if _, err := d.DeleteItem(input); err != nil {
return err
}

logrus.WithFields(logrus.Fields{
"key": keyName,
"table": tableName,
}).Info("Releasing lock")

return nil
}
6 changes: 3 additions & 3 deletions utils/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

const (
// Region is default AWS region when none provided
// Region is default AWS region when none provided.
Region = "us-west-2"

// TableName is the default table name when none provided
// TableName is the default table name when none provided.
TableName = "lock-exec"
)

// AWSSession generates an aws api session in us-west-2
// AWSSession generates an aws api session in us-west-2.
func AWSSession(region string) *session.Session {
return session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Expand Down