2024-02-14 11:26:42 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent/lock"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-03-19 09:29:16 +00:00
|
|
|
CAPIPullLockTimeout = 10
|
|
|
|
CapiPullLockName = "pullCAPI"
|
2024-02-14 11:26:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Client) AcquireLock(name string) error {
|
2024-03-19 09:29:16 +00:00
|
|
|
log.Debugf("acquiring lock %s", name)
|
2024-02-14 11:26:42 +00:00
|
|
|
_, err := c.Ent.Lock.Create().
|
|
|
|
SetName(name).
|
|
|
|
SetCreatedAt(types.UtcNow()).
|
|
|
|
Save(c.CTX)
|
|
|
|
if ent.IsConstraintError(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(InsertFail, "insert lock: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) ReleaseLock(name string) error {
|
2024-03-19 09:29:16 +00:00
|
|
|
log.Debugf("releasing lock %s", name)
|
2024-02-14 11:26:42 +00:00
|
|
|
_, err := c.Ent.Lock.Delete().Where(lock.NameEQ(name)).Exec(c.CTX)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(DeleteFail, "delete lock: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) ReleaseLockWithTimeout(name string, timeout int) error {
|
2024-03-19 09:29:16 +00:00
|
|
|
log.Debugf("releasing lock %s with timeout of %d minutes", name, timeout)
|
2024-02-14 11:26:42 +00:00
|
|
|
_, err := c.Ent.Lock.Delete().Where(
|
|
|
|
lock.NameEQ(name),
|
2024-03-19 09:29:16 +00:00
|
|
|
lock.CreatedAtLT(time.Now().UTC().Add(-time.Duration(timeout)*time.Minute)),
|
2024-02-14 11:26:42 +00:00
|
|
|
).Exec(c.CTX)
|
2024-03-19 09:29:16 +00:00
|
|
|
|
2024-02-14 11:26:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(DeleteFail, "delete lock: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) IsLocked(err error) bool {
|
|
|
|
return ent.IsConstraintError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) AcquirePullCAPILock() error {
|
2024-03-19 09:29:16 +00:00
|
|
|
|
|
|
|
/*delete orphan "old" lock if present*/
|
|
|
|
err := c.ReleaseLockWithTimeout(CapiPullLockName, CAPIPullLockTimeout)
|
2024-02-14 11:26:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to release pullCAPI lock: %s", err)
|
|
|
|
}
|
2024-03-19 09:29:16 +00:00
|
|
|
return c.AcquireLock(CapiPullLockName)
|
2024-02-14 11:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) ReleasePullCAPILock() error {
|
2024-03-19 09:29:16 +00:00
|
|
|
log.Debugf("deleting lock %s", CapiPullLockName)
|
|
|
|
_, err := c.Ent.Lock.Delete().Where(
|
|
|
|
lock.NameEQ(CapiPullLockName),
|
|
|
|
).Exec(c.CTX)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(DeleteFail, "delete lock: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
2024-02-14 11:26:42 +00:00
|
|
|
}
|