New JobQueue worker

This commit is contained in:
Jamie Curnow
2022-07-15 08:52:38 +10:00
parent 3c0af95468
commit f51c12ed9a
10 changed files with 209 additions and 103 deletions

View File

@ -8,6 +8,7 @@ import (
"npm/internal/database"
"npm/internal/entity"
"npm/internal/errors"
"npm/internal/jobqueue"
"npm/internal/logger"
"npm/internal/model"
)
@ -172,3 +173,25 @@ func GetByStatus(status string) ([]Model, error) {
return models, err
}
// AddPendingJobs is intended to be used at startup to add
// anything pending to the JobQueue just once, based on
// the database row status
func AddPendingJobs() {
rows, err := GetByStatus(StatusReady)
if err != nil {
logger.Error("AddPendingJobsError", err)
return
}
for _, row := range rows {
logger.Debug("Adding RequestCertificate job: %+v", row)
err := jobqueue.AddJob(jobqueue.Job{
Name: "RequestCertificate",
Action: row.Request,
})
if err != nil {
logger.Error("AddPendingJobsError", err)
}
}
}

View File

@ -50,6 +50,8 @@ func create(host *Model) (int, error) {
paths,
upstream_options,
advanced_config,
status,
error_message,
is_disabled,
is_deleted
) VALUES (
@ -73,6 +75,8 @@ func create(host *Model) (int, error) {
:paths,
:upstream_options,
:advanced_config,
:status,
:error_message,
:is_disabled,
:is_deleted
)`, host)
@ -86,6 +90,8 @@ func create(host *Model) (int, error) {
return 0, lastErr
}
logger.Debug("Created Host: %+v", host)
return int(last), nil
}
@ -120,10 +126,14 @@ func update(host *Model) error {
paths = :paths,
upstream_options = :upstream_options,
advanced_config = :advanced_config,
status = :status,
error_message = :error_message,
is_disabled = :is_disabled,
is_deleted = :is_deleted
WHERE id = :id`, host)
logger.Debug("Updated Host: %+v", host)
return err
}
@ -181,3 +191,10 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (Lis
return result, nil
}
// AddPendingJobs is intended to be used at startup to add
// anything pending to the JobQueue just once, based on
// the database row status
func AddPendingJobs() {
// todo
}

View File

@ -20,6 +20,12 @@ const (
RedirectionHostType = "redirection"
// DeadHostType is self explanatory
DeadHostType = "dead"
// StatusReady means a host is ready to configure
StatusReady = "ready"
// StatusOK means a host is configured within Nginx
StatusOK = "ok"
// StatusError is self explanatory
StatusError = "error"
)
// Model is the user model
@ -45,6 +51,8 @@ type Model struct {
Paths string `json:"paths" db:"paths" filter:"paths,string"`
UpstreamOptions string `json:"upstream_options" db:"upstream_options" filter:"upstream_options,string"`
AdvancedConfig string `json:"advanced_config" db:"advanced_config" filter:"advanced_config,string"`
Status string `json:"status" db:"status" filter:"status,string"`
ErrorMessage string `json:"error_message" db:"error_message" filter:"error_message,string"`
IsDisabled bool `json:"is_disabled" db:"is_disabled" filter:"is_disabled,boolean"`
IsDeleted bool `json:"is_deleted,omitempty" db:"is_deleted"`
// Expansions
@ -81,6 +89,9 @@ func (m *Model) Save() error {
return fmt.Errorf("User ID must be specified")
}
// Set this host as requiring reconfiguration
m.Status = StatusReady
if m.ID == 0 {
m.ID, err = create(m)
} else {