snapshot: Lock instances before making snapshot
This commit is contained in:
parent
f1c5d518c2
commit
5e7c5b2d23
11 changed files with 167 additions and 0 deletions
|
|
@ -20,6 +20,7 @@
|
|||
<b>Excluded from Resolver:</b> <code>{{ .Info.NoPrefixes }}</code><br />
|
||||
<hr />
|
||||
<b>Running:</b> <code>{{ .Info.Running }}</code> <br />
|
||||
<b>Locked:</b> <code>{{ .Info.Locked }}</code> <br />
|
||||
<!-- <b>OwnerEmail:</b> <code>{{ .Instance.OwnerEmail }}</code> <br /> -->
|
||||
<hr />
|
||||
<b>Created:</b> <code class="date">{{ .Instance.Created.Format "2006-01-02T15:04:05Z07:00" }}</code> <br />
|
||||
|
|
|
|||
47
internal/component/instances/wisski_lock.go
Normal file
47
internal/component/instances/wisski_lock.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package instances
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||
)
|
||||
|
||||
var ErrLocked = errors.New("instance is locked")
|
||||
|
||||
// TryLock attemps to lock this WissKI
|
||||
// If this is not possible, returns ErrLocked
|
||||
func (wisski WissKI) TryLock() error {
|
||||
table, err := wisski.instances.SQL.QueryTable(true, models.LockTable)
|
||||
if err != nil {
|
||||
return ErrLocked
|
||||
}
|
||||
|
||||
result := table.FirstOrCreate(&models.Lock{}, models.Lock{Slug: wisski.Slug})
|
||||
locked := result.Error == nil && result.RowsAffected == 1
|
||||
|
||||
if !locked {
|
||||
return ErrLocked
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (wisski WissKI) IsLocked() (locked bool) {
|
||||
table, err := wisski.instances.SQL.QueryTable(true, models.LockTable)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// check if this instance is locked
|
||||
table.Select("count(*) > 0").Where("slug = ?", wisski.Slug).Find(&locked)
|
||||
return
|
||||
}
|
||||
|
||||
// Unlock unlocks this WissKI instance and returns if it succeeded
|
||||
func (wisski WissKI) Unlock() bool {
|
||||
table, err := wisski.instances.SQL.QueryTable(true, models.LockTable)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
result := table.Where("slug = ?", wisski.Slug).Delete(&models.Lock{})
|
||||
return result.Error == nil && result.RowsAffected == 1
|
||||
}
|
||||
|
|
@ -70,6 +70,11 @@ func (wisski *WissKI) setLastRebuild() error {
|
|||
//
|
||||
// It also logs the current time into the metadata belonging to this instance.
|
||||
func (wisski *WissKI) Build(stream stream.IOStream, start bool) error {
|
||||
if err := wisski.TryLock(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer wisski.Unlock()
|
||||
|
||||
barrel := wisski.Barrel()
|
||||
|
||||
var context component.InstallationContext
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ type WissKIInfo struct {
|
|||
Slug string // slug
|
||||
URL string // complete URL, including http(s)
|
||||
|
||||
Locked bool // Is this instance currently locked?
|
||||
|
||||
// Information about the running instance
|
||||
Running bool
|
||||
LastRebuild time.Time
|
||||
|
|
@ -48,6 +50,12 @@ func (wisski *WissKI) Info(quick bool) (info WissKIInfo, err error) {
|
|||
return
|
||||
})
|
||||
|
||||
// quick check if this instance is locked
|
||||
group.Go(func() (err error) {
|
||||
info.Locked = wisski.IsLocked()
|
||||
return nil
|
||||
})
|
||||
|
||||
// slower checks for extra properties.
|
||||
// these might execute php code or require additional database queries.
|
||||
if !quick {
|
||||
|
|
|
|||
|
|
@ -44,6 +44,21 @@ type Snapshot struct {
|
|||
|
||||
// Snapshot creates a new snapshot of this instance into dest
|
||||
func (snapshots *Manager) NewSnapshot(instance instances.WissKI, io stream.IOStream, desc SnapshotDescription) (snapshot Snapshot) {
|
||||
|
||||
logging.LogMessage(io, "Locking instance")
|
||||
if err := instance.TryLock(); err != nil {
|
||||
io.EPrintln(err)
|
||||
logging.LogMessage(io, "Aborting snapshot creation")
|
||||
|
||||
return Snapshot{
|
||||
ErrPanic: err,
|
||||
}
|
||||
}
|
||||
defer func() {
|
||||
logging.LogMessage(io, "Unlocking instance")
|
||||
instance.Unlock()
|
||||
}()
|
||||
|
||||
// setup the snapshot
|
||||
snapshot.Description = desc
|
||||
snapshot.Instance = instance.Instance
|
||||
|
|
|
|||
|
|
@ -96,6 +96,11 @@ func (sql *SQL) Update(io stream.IOStream) error {
|
|||
&models.Export{},
|
||||
models.ExportTable,
|
||||
},
|
||||
{
|
||||
"lock",
|
||||
&models.Lock{},
|
||||
models.LockTable,
|
||||
},
|
||||
}
|
||||
|
||||
// migrate all of the tables!
|
||||
|
|
|
|||
11
internal/models/lock.go
Normal file
11
internal/models/lock.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package models
|
||||
|
||||
// LockTable is the name of the table the 'Metadatum' model is stored in.
|
||||
const LockTable = "locks"
|
||||
|
||||
// Lock represents a log on WissKI Instances
|
||||
type Lock struct {
|
||||
Pk uint `gorm:"column:pk;primaryKey"`
|
||||
|
||||
Slug string `gorm:"column:slug;not null"` // slug of instance
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue