services

package
v0.0.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package services provides business logic implementation for the API

Package services provides business logic implementation for the API

Index

Constants

View Source
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
)
View Source
const DefaultBackoff = time.Second

DefaultBackoff is the default backoff time for the worker

Variables

View Source
var (
	ErrUserNotFound     = errors.New("user not found")
	ErrUserCreateFailed = errors.New("failed to create user")
)

User service errors

View Source
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

func (s *Instance) GetInstance(ctx context.Context, ownerID, id uint) (*models.Instance, error)

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

func (s *Instance) MarkAsTerminated(ctx context.Context, ownerID, instanceID uint) error

MarkAsTerminated marks an instance as terminated

func (*Instance) Terminate

func (s *Instance) Terminate(ctx context.Context, ownerID uint, projectName string, instanceIDs []uint) error

Terminate handles the termination of instances for a given project name and instance IDs.

func (*Instance) Update added in v0.0.8

func (s *Instance) Update(ctx context.Context, ownerID uint, instanceID uint, instance *models.Instance) error

Update updates an instance by ID

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) Create

func (s *Project) Create(ctx context.Context, project *models.Project) error

Create creates a new project

func (*Project) Delete

func (s *Project) Delete(ctx context.Context, ownerID uint, name string) error

Delete deletes a project by name

func (*Project) GetByName

func (s *Project) GetByName(ctx context.Context, ownerID uint, name string) (*models.Project, error)

GetByName retrieves a project by name

func (*Project) List

func (s *Project) List(ctx context.Context, ownerID uint, opts *models.ListOptions) ([]models.Project, error)

List retrieves all projects with pagination

func (*Project) ListInstances

func (s *Project) ListInstances(ctx context.Context, ownerID uint, projectName string, opts *models.ListOptions) ([]models.Instance, error)

ListInstances retrieves all instances for a specific project

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

func (s *Task) AcquireTaskLock(ctx context.Context, taskID uint) error

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) AddLogs

func (s *Task) AddLogs(ctx context.Context, ownerID uint, taskID uint, logs string) error

AddLogs appends logs to a task

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) Create

func (s *Task) Create(ctx context.Context, task *models.Task) error

Create creates a new task

func (*Task) CreateBatch added in v0.0.2

func (s *Task) CreateBatch(ctx context.Context, tasks []*models.Task) error

CreateBatch creates a batch of tasks

func (*Task) Get added in v0.0.8

func (s *Task) Get(ctx context.Context, ownerID uint, taskID uint) (*models.Task, error)

Get retrieves a task by ID

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

func (s *Task) IncrementAttempts(ctx context.Context, taskID uint) error

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

func (s *Task) RecoverStaleTasks(ctx context.Context) (int64, error)

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

func (s *Task) ReleaseTaskLock(ctx context.Context, taskID uint) error

ReleaseTaskLock releases a task lock

func (*Task) SetError

func (s *Task) SetError(ctx context.Context, ownerID uint, taskID uint, errMsg string) error

SetError updates a task with error information

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) Update

func (s *Task) Update(ctx context.Context, ownerID uint, task *models.Task) error

Update updates an existing task.

func (*Task) UpdateFailed added in v0.0.2

func (s *Task) UpdateFailed(ctx context.Context, task *models.Task, errMsg, logMsg string) error

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

func (s User) CreateUser(ctx context.Context, user *models.User) (uint, error)

CreateUser creates a new user

func (User) DeleteUser

func (s User) DeleteUser(ctx context.Context, userID uint) error

DeleteUser deletes a user

func (User) GetAllUsers

func (s User) GetAllUsers(ctx context.Context, opts *models.ListOptions) ([]models.User, error)

GetAllUsers retrieves all users

func (User) GetUserByID

func (s User) GetUserByID(ctx context.Context, userID uint) (*models.User, error)

GetUserByID retrieves a user by id

func (User) GetUserByUsername

func (s User) GetUserByUsername(ctx context.Context, username string) (*models.User, error)

GetUserByUsername retrieves a user by username

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL