mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-08-28 10:54:59 +00:00
* Remove Node from data model; standardize on Worker This commit does a lot of things, but in general it attempts to simplify the data model by getting rid of the Node and NodeRole models. Instead, these will use the Worker model, which itself has individual boolean fields for role types. Get rid of roleType in some FreeWorker methods rename NodeService to WorkerRegistry simplify the freeworker interface fix the tests * Remove DeleteWorker method from workerJobService
80 lines
2.8 KiB
Go
80 lines
2.8 KiB
Go
package models
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/featurebasedb/featurebase/v3/dax"
|
|
"github.com/featurebasedb/featurebase/v3/errors"
|
|
"github.com/gobuffalo/nulls"
|
|
"github.com/gobuffalo/pop/v6"
|
|
"github.com/gobuffalo/validate/v3"
|
|
"github.com/gobuffalo/validate/v3/validators"
|
|
"github.com/gofrs/uuid"
|
|
)
|
|
|
|
// Worker is a node plus a role that gets assigned to a database and
|
|
// can be assigned jobs for that database.
|
|
type Worker struct {
|
|
ID uuid.UUID `json:"id" db:"id"`
|
|
Address dax.Address `json:"address" db:"address"`
|
|
DatabaseID nulls.String `json:"database_id" db:"database_id"` // this can be empty which means the worker is unassigned
|
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
|
Jobs Jobs `json:"jobs" has_many:"jobs" order_by:"name asc"`
|
|
RoleCompute bool `json:"role_compute" db:"role_compute"`
|
|
RoleTranslate bool `json:"role_translate" db:"role_translate"`
|
|
RoleQuery bool `json:"role_query" db:"role_query"`
|
|
}
|
|
|
|
// String is not required by pop and may be deleted
|
|
func (t *Worker) String() string {
|
|
jt, _ := json.MarshalIndent(t, " ", " ") //nolint:errchkjson
|
|
return string(jt)
|
|
}
|
|
|
|
// SetRole applies a dax.RoleType to one of the boolean fields on the Worker
|
|
// model. It returns an error if the model does not support that role type.
|
|
func (t *Worker) SetRole(role dax.RoleType) error {
|
|
switch role {
|
|
case dax.RoleTypeCompute:
|
|
t.RoleCompute = true
|
|
case dax.RoleTypeTranslate:
|
|
t.RoleTranslate = true
|
|
case dax.RoleTypeQuery:
|
|
t.RoleQuery = true
|
|
default:
|
|
errors.Errorf("invalid role type for worker: %s", role)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Workers is not required by pop and may be deleted
|
|
type Workers []Worker
|
|
|
|
// String is not required by pop and may be deleted
|
|
func (t Workers) String() string {
|
|
jt, _ := json.MarshalIndent(t, " ", " ") //nolint:errchkjson
|
|
return string(jt)
|
|
}
|
|
|
|
// Validate gets run every time you call a "pop.Validate*" (pop.ValidateAndSave, pop.ValidateAndCreate, pop.ValidateAndUpdate) method.
|
|
// This method is not required and may be deleted.
|
|
func (t *Worker) Validate(tx *pop.Connection) (*validate.Errors, error) {
|
|
return validate.Validate(
|
|
&validators.StringIsPresent{Field: string(t.Address), Name: "Address"},
|
|
), nil
|
|
}
|
|
|
|
// ValidateCreate gets run every time you call "pop.ValidateAndCreate" method.
|
|
// This method is not required and may be deleted.
|
|
func (t *Worker) ValidateCreate(tx *pop.Connection) (*validate.Errors, error) {
|
|
return validate.NewErrors(), nil
|
|
}
|
|
|
|
// ValidateUpdate gets run every time you call "pop.ValidateAndUpdate" method.
|
|
// This method is not required and may be deleted.
|
|
func (t *Worker) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) {
|
|
return validate.NewErrors(), nil
|
|
}
|