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

fix: self-update in parallel processes does not work properly #33

Merged
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
50 changes: 47 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/inconshreveable/go-update"
"github.com/werf/lockgate"
Expand All @@ -15,6 +16,12 @@ import (
"github.com/werf/trdl/pkg/util"
)

const (
selfUpdateLockFilename = "self-update"
selfUpdateDelayFilename = "self-update"
selfUpdateDelay = time.Second * 30
)

type Client struct {
dir string
configuration configurationInterface
Expand Down Expand Up @@ -122,9 +129,41 @@ func (c Client) SetRepoDefaultChannel(repoName, channel string) error {
}

func (c Client) DoSelfUpdate() error {
return lockgate.WithAcquire(c.locker, "self-update", lockgate.AcquireOptions{Shared: false, Timeout: trdl.DefaultLockerTimeout}, func(_ bool) error {
return c.doSelfUpdate()
})
acquired, lock, err := c.locker.Acquire(selfUpdateLockFilename, lockgate.AcquireOptions{Shared: false, NonBlocking: true})
if err != nil {
return fmt.Errorf("unable to acquire lock: %s", err)
}

// skip due to execution in a parallel process
if !acquired {
return nil
}

// skip due to delay between updates has not passed yet
{
passed, err := c.selfUpdateDelayFile().IsDelayPassed()
if err != nil {
return fmt.Errorf("unable to check delay file: %s", err)
}

if !passed {
return nil
}
}

if err := c.doSelfUpdate(); err != nil {
return err
}

if err := c.selfUpdateDelayFile().UpdateTimestamp(); err != nil {
return fmt.Errorf("unable to update delay file timestamp: %s", err)
}

if err := c.locker.Release(lock); err != nil {
return fmt.Errorf("unable to release lock: %s", err)
}

return nil
}

func (c Client) doSelfUpdate() error {
Expand Down Expand Up @@ -408,3 +447,8 @@ func (c *Client) repoLogsDir(repoName string) string {
func (c *Client) tmpDir() string {
return filepath.Join(c.dir, ".tmp")
}

func (c *Client) selfUpdateDelayFile() util.DelayFile {
filePath := filepath.Join(c.dir, ".delay", selfUpdateDelayFilename)
return util.NewDelayFile(c.locker, filePath, selfUpdateDelay)
}
84 changes: 84 additions & 0 deletions pkg/util/delay_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package util

import (
"os"
"path/filepath"
"time"

"github.com/werf/lockgate"
)

type DelayFile struct {
Locker lockgate.Locker
FilePath string
Delay time.Duration
}

func NewDelayFile(locker lockgate.Locker, filePath string, delay time.Duration) DelayFile {
return DelayFile{
Locker: locker,
FilePath: filePath,
Delay: delay,
}
}

func (u DelayFile) IsDelayPassed() (passed bool, err error) {
err = lockgate.WithAcquire(u.Locker, u.FilePath, lockgate.AcquireOptions{Shared: true, Timeout: 30 * time.Second}, func(_ bool) error {
passed, err = u.isDelayPassed()
return err
})

return
}

func (u DelayFile) isDelayPassed() (bool, error) {
info, err := os.Stat(u.FilePath)
if err != nil {
if isNotExistErr(err) {
return true, nil
}

return false, nil
}

fTime := info.ModTime()
if fTime.Add(u.Delay).Before(time.Now()) {
return true, nil
}

return false, nil
}

func (u DelayFile) UpdateTimestamp() error {
return lockgate.WithAcquire(u.Locker, u.FilePath, lockgate.AcquireOptions{Shared: false, Timeout: 30 * time.Second}, func(_ bool) error {
return u.updateTimestamp()
})
}

func (u DelayFile) updateTimestamp() error {
exist, err := IsRegularFileExist(u.FilePath)
if err != nil {
return err
}

if exist {
if err := os.Remove(u.FilePath); err != nil {
return err
}
}

if err := os.MkdirAll(filepath.Dir(u.FilePath), os.ModePerm); err != nil {
return err
}

f, err := os.Create(u.FilePath)
if err != nil {
return err
}

if err := f.Close(); err != nil {
return err
}

return nil
}