Documentation
¶
Overview ¶
Package services provides business logic implementation for the API
Package services provides business logic implementation for the API
Index ¶
- Constants
- Variables
- type Instance
- func (s *Instance) CreateInstance(ctx context.Context, instances []types.InstanceRequest) ([]*models.Instance, error)
- func (s *Instance) Get(ctx context.Context, ownerID uint, instanceID uint) (*models.Instance, error)
- func (s *Instance) GetInstance(ctx context.Context, ownerID, id uint) (*models.Instance, error)
- func (s *Instance) ListInstances(ctx context.Context, ownerID uint, opts *models.ListOptions) ([]models.Instance, error)
- func (s *Instance) MarkAsTerminated(ctx context.Context, ownerID, instanceID uint) error
- func (s *Instance) Terminate(ctx context.Context, ownerID uint, projectName string, instanceIDs []uint) error
- func (s *Instance) Update(ctx context.Context, ownerID uint, instanceID uint, instance *models.Instance) error
- type Project
- func (s *Project) Create(ctx context.Context, project *models.Project) error
- func (s *Project) Delete(ctx context.Context, ownerID uint, name string) error
- func (s *Project) GetByName(ctx context.Context, ownerID uint, name string) (*models.Project, error)
- func (s *Project) List(ctx context.Context, ownerID uint, opts *models.ListOptions) ([]models.Project, error)
- func (s *Project) ListInstances(ctx context.Context, ownerID uint, projectName string, ...) ([]models.Instance, error)
- type Task
- func (s *Task) AcquireTaskLock(ctx context.Context, taskID uint) error
- func (s *Task) AddLogs(ctx context.Context, ownerID uint, taskID uint, logs string) error
- func (s *Task) CompleteTask(ctx context.Context, ownerID uint, taskID uint, result json.RawMessage) error
- func (s *Task) Create(ctx context.Context, task *models.Task) error
- func (s *Task) CreateBatch(ctx context.Context, tasks []*models.Task) error
- func (s *Task) Get(ctx context.Context, ownerID uint, taskID uint) (*models.Task, error)
- func (s *Task) GetSchedulableTasks(ctx context.Context, priority models.TaskPriority, limit int) ([]models.Task, error)
- func (s *Task) IncrementAttempts(ctx context.Context, taskID uint) error
- func (s *Task) ListByProject(ctx context.Context, ownerID uint, projectName string, ...) ([]models.Task, error)
- func (s *Task) ListTasksByInstanceID(ctx context.Context, ownerID uint, instanceID uint, ...) ([]models.Task, error)
- func (s *Task) RecoverStaleTasks(ctx context.Context) (int64, error)
- func (s *Task) ReleaseTaskLock(ctx context.Context, taskID uint) error
- func (s *Task) SetError(ctx context.Context, ownerID uint, taskID uint, errMsg string) error
- func (s *Task) SetResult(ctx context.Context, ownerID uint, taskID uint, result json.RawMessage) error
- func (s *Task) Update(ctx context.Context, ownerID uint, task *models.Task) error
- func (s *Task) UpdateFailed(ctx context.Context, task *models.Task, errMsg, logMsg string) error
- func (s *Task) UpdateStatus(ctx context.Context, ownerID uint, taskID uint, status models.TaskStatus) error
- type User
- func (s User) CreateUser(ctx context.Context, user *models.User) (uint, error)
- func (s User) DeleteUser(ctx context.Context, userID uint) error
- func (s User) GetAllUsers(ctx context.Context, opts *models.ListOptions) ([]models.User, error)
- func (s User) GetUserByID(ctx context.Context, userID uint) (*models.User, error)
- func (s User) GetUserByUsername(ctx context.Context, username string) (*models.User, error)
- type WorkerPool
Constants ¶
const ( // DefaultWorkerCount is the default number of workers in the pool DefaultWorkerCount = 100 // DefaultHighPriorityRatio is the default ratio of workers assigned to high priority tasks DefaultHighPriorityRatio = 0.7 // QueueSize is the size of the task queue QueueSize = 100 )
const DefaultBackoff = time.Second
DefaultBackoff is the default backoff time for the worker
Variables ¶
var ( ErrUserNotFound = errors.New("user not found") ErrUserCreateFailed = errors.New("failed to create user") )
User service errors
var ErrTaskLockNotAcquired = fmt.Errorf("task lock could not be acquired")
ErrTaskLockNotAcquired is returned when a task lock could not be acquired
Functions ¶
This section is empty.
Types ¶
type Instance ¶
type Instance struct {
// contains filtered or unexported fields
}
Instance provides business logic for instance operations
func NewInstanceService ¶
func NewInstanceService(repo *repos.InstanceRepository, taskService *Task, projectService *Project) *Instance
NewInstanceService creates a new instance service instance
func (*Instance) CreateInstance ¶
func (s *Instance) CreateInstance(ctx context.Context, instances []types.InstanceRequest) ([]*models.Instance, error)
CreateInstance creates a new instance and a new task to track the instance creation in the DB. It returns the created instances and an error if one occurred.
func (*Instance) Get ¶ added in v0.0.8
func (s *Instance) Get(ctx context.Context, ownerID uint, instanceID uint) (*models.Instance, error)
Get retrieves an instance by ID
func (*Instance) GetInstance ¶
GetInstance retrieves an instance by ID
func (*Instance) ListInstances ¶
func (s *Instance) ListInstances(ctx context.Context, ownerID uint, opts *models.ListOptions) ([]models.Instance, error)
ListInstances retrieves a paginated list of instances
func (*Instance) MarkAsTerminated ¶ added in v0.0.2
MarkAsTerminated marks an instance as terminated
type Project ¶
type Project struct {
// contains filtered or unexported fields
}
Project handles project-related operations
func NewProjectService ¶
func NewProjectService(repo *repos.ProjectRepository) *Project
NewProjectService creates a new instance of ProjectService
func (*Project) GetByName ¶
func (s *Project) GetByName(ctx context.Context, ownerID uint, name string) (*models.Project, error)
GetByName retrieves a project by name
type Task ¶
type Task struct {
// contains filtered or unexported fields
}
Task handles task-related operations
func NewTaskService ¶
func NewTaskService(repo *repos.TaskRepository, projectService *Project) *Task
NewTaskService creates a new instance of TaskService
func (*Task) AcquireTaskLock ¶ added in v0.0.9
AcquireTaskLock attempts to lock a task for processing Returns nil if the lock was acquired, ErrTaskLockNotAcquired if the lock was not acquired, or another error if there was a problem with the database operation
func (*Task) CompleteTask ¶
func (s *Task) CompleteTask(ctx context.Context, ownerID uint, taskID uint, result json.RawMessage) error
CompleteTask marks a task as completed and sends webhook if configured
func (*Task) CreateBatch ¶ added in v0.0.2
CreateBatch creates a batch of tasks
func (*Task) GetSchedulableTasks ¶
func (s *Task) GetSchedulableTasks(ctx context.Context, priority models.TaskPriority, limit int) ([]models.Task, error)
GetSchedulableTasks retrieves tasks ready for the worker to process.
func (*Task) IncrementAttempts ¶ added in v0.0.9
IncrementAttempts atomically increments the attempts count for a task
func (*Task) ListByProject ¶
func (s *Task) ListByProject(ctx context.Context, ownerID uint, projectName string, opts *models.ListOptions) ([]models.Task, error)
ListByProject retrieves all tasks for a specific project with pagination
func (*Task) ListTasksByInstanceID ¶ added in v0.0.8
func (s *Task) ListTasksByInstanceID(ctx context.Context, ownerID uint, instanceID uint, actionFilter models.TaskAction, opts *models.ListOptions) ([]models.Task, error)
ListTasksByInstanceID retrieves all tasks for a specific instance, with an optional action filter.
func (*Task) RecoverStaleTasks ¶ added in v0.0.9
RecoverStaleTasks finds tasks that were in progress when the system crashed and resets them to pending status with incremented attempts
func (*Task) ReleaseTaskLock ¶ added in v0.0.9
ReleaseTaskLock releases a task lock
func (*Task) SetResult ¶
func (s *Task) SetResult(ctx context.Context, ownerID uint, taskID uint, result json.RawMessage) error
SetResult updates a task with results data
func (*Task) UpdateFailed ¶ added in v0.0.2
UpdateFailed updates a task as failed
func (*Task) UpdateStatus ¶
func (s *Task) UpdateStatus(ctx context.Context, ownerID uint, taskID uint, status models.TaskStatus) error
UpdateStatus updates the status of a task
type User ¶
type User struct {
// contains filtered or unexported fields
}
User provides business logic for user operations
func NewUserService ¶
func NewUserService(repo *repos.UserRepository) *User
NewUserService creates a new user service instance
func (User) CreateUser ¶
CreateUser creates a new user
func (User) DeleteUser ¶
DeleteUser deletes a user
func (User) GetAllUsers ¶
GetAllUsers retrieves all users
func (User) GetUserByID ¶
GetUserByID retrieves a user by id
type WorkerPool ¶ added in v0.0.9
type WorkerPool struct {
// contains filtered or unexported fields
}
WorkerPool is a struct that contains the worker pool's dependencies
func NewWorkerPool ¶ added in v0.0.9
func NewWorkerPool(instanceService *Instance, projectService *Project, taskService *Task, userService *User, backoff time.Duration) *WorkerPool
NewWorkerPool creates a new WorkerPool
func (*WorkerPool) LaunchWorkerPool ¶ added in v0.0.9
func (w *WorkerPool) LaunchWorkerPool(ctx context.Context, wg *sync.WaitGroup)
LaunchWorkerPool launches a task dispatcher and worker pool to process tasks
func (*WorkerPool) WithHighPriorityRatio ¶ added in v0.0.9
func (w *WorkerPool) WithHighPriorityRatio(ratio float64) *WorkerPool
WithHighPriorityRatio sets the ratio of workers assigned to high priority tasks
func (*WorkerPool) WithWorkerCount ¶ added in v0.0.9
func (w *WorkerPool) WithWorkerCount(count int) *WorkerPool
WithWorkerCount sets the number of workers in the pool