2018-11-06 09:49:48 +00:00
package cron
import (
"log"
2019-03-21 01:20:14 +00:00
"github.com/portainer/portainer/api"
2018-11-06 09:49:48 +00:00
)
// SnapshotJobRunner is used to run a SnapshotJob
type SnapshotJobRunner struct {
2018-11-13 01:39:26 +00:00
schedule * portainer . Schedule
context * SnapshotJobContext
2018-11-06 09:49:48 +00:00
}
// SnapshotJobContext represents the context of execution of a SnapshotJob
type SnapshotJobContext struct {
endpointService portainer . EndpointService
snapshotter portainer . Snapshotter
}
// NewSnapshotJobContext returns a new context that can be used to execute a SnapshotJob
func NewSnapshotJobContext ( endpointService portainer . EndpointService , snapshotter portainer . Snapshotter ) * SnapshotJobContext {
return & SnapshotJobContext {
endpointService : endpointService ,
snapshotter : snapshotter ,
}
}
// NewSnapshotJobRunner returns a new runner that can be scheduled
2018-11-13 01:39:26 +00:00
func NewSnapshotJobRunner ( schedule * portainer . Schedule , context * SnapshotJobContext ) * SnapshotJobRunner {
2018-11-06 09:49:48 +00:00
return & SnapshotJobRunner {
2018-11-13 01:39:26 +00:00
schedule : schedule ,
context : context ,
2018-11-06 09:49:48 +00:00
}
}
2018-11-13 01:39:26 +00:00
// GetSchedule returns the schedule associated to the runner
func ( runner * SnapshotJobRunner ) GetSchedule ( ) * portainer . Schedule {
return runner . schedule
2018-11-06 09:49:48 +00:00
}
2018-11-13 01:39:26 +00:00
// Run triggers the execution of the schedule.
2018-11-06 09:49:48 +00:00
// It will iterate through all the endpoints available in the database to
// create a snapshot of each one of them.
2018-11-13 02:18:38 +00:00
// As a snapshot can be a long process, to avoid any concurrency issue we
// retrieve the latest version of the endpoint right after a snapshot.
2018-11-06 09:49:48 +00:00
func ( runner * SnapshotJobRunner ) Run ( ) {
2018-12-12 04:00:15 +00:00
go func ( ) {
endpoints , err := runner . context . endpointService . Endpoints ( )
if err != nil {
log . Printf ( "background schedule error (endpoint snapshot). Unable to retrieve endpoint list (err=%s)\n" , err )
return
2018-11-06 09:49:48 +00:00
}
2018-12-12 04:00:15 +00:00
for _ , endpoint := range endpoints {
if endpoint . Type == portainer . AzureEnvironment {
continue
}
2018-11-13 02:18:38 +00:00
2018-12-12 04:00:15 +00:00
snapshot , snapshotError := runner . context . snapshotter . CreateSnapshot ( & endpoint )
2018-11-13 02:18:38 +00:00
2018-12-12 04:00:15 +00:00
latestEndpointReference , err := runner . context . endpointService . Endpoint ( endpoint . ID )
if latestEndpointReference == nil {
log . Printf ( "background schedule error (endpoint snapshot). Endpoint not found inside the database anymore (endpoint=%s, URL=%s) (err=%s)\n" , endpoint . Name , endpoint . URL , err )
continue
}
2018-11-06 09:49:48 +00:00
2018-12-12 04:00:15 +00:00
latestEndpointReference . Status = portainer . EndpointStatusUp
if snapshotError != nil {
log . Printf ( "background schedule error (endpoint snapshot). Unable to create snapshot (endpoint=%s, URL=%s) (err=%s)\n" , endpoint . Name , endpoint . URL , snapshotError )
latestEndpointReference . Status = portainer . EndpointStatusDown
}
2018-11-06 09:49:48 +00:00
2018-12-12 04:00:15 +00:00
if snapshot != nil {
latestEndpointReference . Snapshots = [ ] portainer . Snapshot { * snapshot }
}
err = runner . context . endpointService . UpdateEndpoint ( latestEndpointReference . ID , latestEndpointReference )
if err != nil {
log . Printf ( "background schedule error (endpoint snapshot). Unable to update endpoint (endpoint=%s, URL=%s) (err=%s)\n" , endpoint . Name , endpoint . URL , err )
return
}
2018-11-06 09:49:48 +00:00
}
2018-12-12 04:00:15 +00:00
} ( )
2018-11-06 09:49:48 +00:00
}