jobs.go 957 B

123456789101112131415161718192021222324252627282930313233343536
  1. package schema
  2. import (
  3. "context"
  4. "github.com/gravitl/netmaker/db"
  5. "time"
  6. )
  7. // Job represents a task that netmaker server
  8. // wants to do.
  9. //
  10. // Ideally, a jobs table should have details
  11. // about its type, status, who initiated it,
  12. // etc. But, for now, the table only contains
  13. // records of jobs that have been done, so
  14. // that it is easier to prevent a task from
  15. // being executed again.
  16. type Job struct {
  17. ID string `gorm:"id;primary_key"`
  18. CreatedAt time.Time `gorm:"created_at"`
  19. }
  20. // TableName returns the name of the jobs table.
  21. func (j *Job) TableName() string {
  22. return "jobs"
  23. }
  24. // Create creates a job record in the jobs table.
  25. func (j *Job) Create(ctx context.Context) error {
  26. return db.FromContext(ctx).Table(j.TableName()).Create(j).Error
  27. }
  28. // Get returns a job record with the given Job.ID.
  29. func (j *Job) Get(ctx context.Context) error {
  30. return db.FromContext(ctx).Table(j.TableName()).Where("id = ?", j.ID).First(j).Error
  31. }