Skip to content

Commit 2f5e245

Browse files
committed
refactor(tasks): add Clear method
1 parent 5e326b5 commit 2f5e245

File tree

7 files changed

+44
-10
lines changed

7 files changed

+44
-10
lines changed

db_lib/AnsibleApp.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ func (t *AnsibleApp) Log(msg string) {
6969
t.Logger.Log(msg)
7070
}
7171

72+
func (t *AnsibleApp) Clear() {
73+
}
74+
7275
func (t *AnsibleApp) InstallRequirements(environmentVars []string, tplParams any, params any) error {
7376
if err := t.installCollectionsRequirements(); err != nil {
7477
return err

db_lib/LocalApp.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ type LocalApp interface {
4040
SetLogger(logger task_logger.Logger) task_logger.Logger
4141
InstallRequirements(environmentVars []string, tplParams any, params any) error
4242
Run(args LocalAppRunningArgs) error
43+
Clear()
4344
}

db_lib/ShellApp.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ func (t *ShellApp) SetLogger(logger task_logger.Logger) task_logger.Logger {
7474
return logger
7575
}
7676

77+
func (t *ShellApp) Clear() {
78+
}
79+
7780
func (t *ShellApp) InstallRequirements(environmentVars []string, tplParams any, params any) error {
7881
return nil
7982
}

db_lib/TerraformApp.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ func (t *TerraformApp) selectWorkspace(workspace string, environmentVars []strin
194194
return nil
195195
}
196196

197+
func (t *TerraformApp) Clear() {
198+
}
199+
197200
func (t *TerraformApp) InstallRequirements(environmentVars []string, tplParams any, params any) (err error) {
198201

199202
tpl := tplParams.(*db.TerraformTemplateParams)
@@ -207,7 +210,7 @@ func (t *TerraformApp) InstallRequirements(environmentVars []string, tplParams a
207210
backendFile := path.Join(t.GetFullPath(), backendFilename)
208211

209212
if tpl.OverrideBackend {
210-
err = os.WriteFile(backendFile, []byte("terraform { backend \"http\" {} }"), 0644)
213+
err = os.WriteFile(backendFile, []byte("terraform {\n backend \"http\" {\n }\n}\n"), 0644)
211214
if err != nil {
212215
return
213216
}
@@ -217,15 +220,15 @@ func (t *TerraformApp) InstallRequirements(environmentVars []string, tplParams a
217220
return
218221
}
219222

220-
if tpl.OverrideBackend {
221-
err = os.Remove(backendFile)
222-
if os.IsNotExist(err) {
223-
err = nil
224-
}
225-
if err != nil {
226-
return
227-
}
228-
}
223+
//if tpl.OverrideBackend {
224+
// err = os.Remove(backendFile)
225+
// if os.IsNotExist(err) {
226+
// err = nil
227+
// }
228+
// if err != nil {
229+
// return
230+
// }
231+
//}
229232

230233
workspace := "default"
231234

services/tasks/LocalJob.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ func (t *LocalJob) Run(username string, incomingVersion *string, alias string) (
526526
defer func() {
527527
t.destroyKeys()
528528
t.destroyInventoryFile()
529+
t.App.Clear()
529530
}()
530531

531532
t.SetStatus(task_logger.TaskRunningStatus) // It is required for local mode. Don't delete

services/tasks/TaskPool.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,26 @@ func getNextBuildVersion(startVersion string, currentVersion string) string {
508508
return prefix + strconv.Itoa(newVer) + suffix
509509
}
510510

511+
// AddTask creates and queues a new task for execution in the task pool.
512+
//
513+
// Parameters:
514+
// - taskObj: The task object with initial configuration
515+
// - userID: Optional ID of the user initiating the task
516+
// - username: Username of the user initiating the task
517+
// - projectID: ID of the project this task belongs to
518+
// - needAlias: Whether to generate a unique alias for the task
519+
//
520+
// The method:
521+
// - Sets initial task properties (created time, waiting status, etc.)
522+
// - Validates the task against its template
523+
// - For build templates, calculates the next version number
524+
// - Creates the task record in the database
525+
// - Sets up appropriate job handler (local or remote)
526+
// - Queues the task for execution
527+
//
528+
// Returns:
529+
// - The newly created task with all properties set
530+
// - An error if task creation or validation fails
511531
func (p *TaskPool) AddTask(
512532
taskObj db.Task,
513533
userID *int,
@@ -558,6 +578,7 @@ func (p *TaskPool) AddTask(
558578
}
559579

560580
if needAlias {
581+
// A unique, randomly-generated identifier that persists throughout the task's lifecycle.
561582
taskRunner.Alias = random.String(32)
562583
}
563584

services/tasks/TaskRunner.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ type TaskRunner struct {
4848
statusListeners []task_logger.StatusListener
4949
logListeners []task_logger.LogListener
5050

51+
// Alias uses if task require an alias for run.
52+
// For example, terraform task require an alias for run.
5153
Alias string
5254

5355
logWG sync.WaitGroup

0 commit comments

Comments
 (0)