 05273fa8d2
			
		
	
	
	05273fa8d2
	
	
	
		
			
			This PR depends on https://codeberg.org/forgejo/forgejo/pulls/7510 This PR renames UpdateRunJob to UpdateRunJobWithoutNotification and UpdateRun to UpdateRunWithoutNotification and implements wrapper functions that also call the new ActionRunNowDone notification when needed. This PR can be reviewed commit-by-commit. # Things to Test - [x] GetRunBefore - [ ] integration test for sendActionRunNowDoneNotificationIfNeeded, UpdateRun and UpdateRunJob ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [ ] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Co-authored-by: nobody <nobody@example.com> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7491 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: christopher-besch <mail@chris-besch.com> Co-committed-by: christopher-besch <mail@chris-besch.com>
		
			
				
	
	
		
			101 lines
		
	
	
	
		
			3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			101 lines
		
	
	
	
		
			3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2022 The Gitea Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package actions
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"fmt"
 | |
| 	"time"
 | |
| 
 | |
| 	actions_model "forgejo.org/models/actions"
 | |
| 	"forgejo.org/models/db"
 | |
| 	"forgejo.org/modules/actions"
 | |
| 	"forgejo.org/modules/log"
 | |
| 	"forgejo.org/modules/setting"
 | |
| 	"forgejo.org/modules/timeutil"
 | |
| )
 | |
| 
 | |
| // StopZombieTasks stops the task which have running status, but haven't been updated for a long time
 | |
| func StopZombieTasks(ctx context.Context) error {
 | |
| 	return stopTasks(ctx, actions_model.FindTaskOptions{
 | |
| 		Status:        []actions_model.Status{actions_model.StatusRunning},
 | |
| 		UpdatedBefore: timeutil.TimeStamp(time.Now().Add(-setting.Actions.ZombieTaskTimeout).Unix()),
 | |
| 	})
 | |
| }
 | |
| 
 | |
| // StopEndlessTasks stops the tasks which have running status and continuous updates, but don't end for a long time
 | |
| func StopEndlessTasks(ctx context.Context) error {
 | |
| 	return stopTasks(ctx, actions_model.FindTaskOptions{
 | |
| 		Status:        []actions_model.Status{actions_model.StatusRunning},
 | |
| 		StartedBefore: timeutil.TimeStamp(time.Now().Add(-setting.Actions.EndlessTaskTimeout).Unix()),
 | |
| 	})
 | |
| }
 | |
| 
 | |
| func stopTasks(ctx context.Context, opts actions_model.FindTaskOptions) error {
 | |
| 	tasks, err := db.Find[actions_model.ActionTask](ctx, opts)
 | |
| 	if err != nil {
 | |
| 		return fmt.Errorf("find tasks: %w", err)
 | |
| 	}
 | |
| 
 | |
| 	jobs := make([]*actions_model.ActionRunJob, 0, len(tasks))
 | |
| 	for _, task := range tasks {
 | |
| 		if err := db.WithTx(ctx, func(ctx context.Context) error {
 | |
| 			if err := StopTask(ctx, task.ID, actions_model.StatusFailure); err != nil {
 | |
| 				return err
 | |
| 			}
 | |
| 			if err := task.LoadJob(ctx); err != nil {
 | |
| 				return err
 | |
| 			}
 | |
| 			jobs = append(jobs, task.Job)
 | |
| 			return nil
 | |
| 		}); err != nil {
 | |
| 			log.Warn("Cannot stop task %v: %v", task.ID, err)
 | |
| 			continue
 | |
| 		}
 | |
| 
 | |
| 		remove, err := actions.TransferLogs(ctx, task.LogFilename)
 | |
| 		if err != nil {
 | |
| 			log.Warn("Cannot transfer logs of task %v: %v", task.ID, err)
 | |
| 			continue
 | |
| 		}
 | |
| 		task.LogInStorage = true
 | |
| 		if err := actions_model.UpdateTask(ctx, task, "log_in_storage"); err != nil {
 | |
| 			log.Warn("Cannot update task %v: %v", task.ID, err)
 | |
| 			continue
 | |
| 		}
 | |
| 		remove()
 | |
| 	}
 | |
| 
 | |
| 	CreateCommitStatus(ctx, jobs...)
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // CancelAbandonedJobs cancels the jobs which have waiting status, but haven't been picked by a runner for a long time
 | |
| func CancelAbandonedJobs(ctx context.Context) error {
 | |
| 	jobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{
 | |
| 		Statuses:      []actions_model.Status{actions_model.StatusWaiting, actions_model.StatusBlocked},
 | |
| 		UpdatedBefore: timeutil.TimeStamp(time.Now().Add(-setting.Actions.AbandonedJobTimeout).Unix()),
 | |
| 	})
 | |
| 	if err != nil {
 | |
| 		log.Warn("find abandoned tasks: %v", err)
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	now := timeutil.TimeStampNow()
 | |
| 	for _, job := range jobs {
 | |
| 		job.Status = actions_model.StatusCancelled
 | |
| 		job.Stopped = now
 | |
| 		if err := db.WithTx(ctx, func(ctx context.Context) error {
 | |
| 			_, err := UpdateRunJob(ctx, job, nil, "status", "stopped")
 | |
| 			return err
 | |
| 		}); err != nil {
 | |
| 			log.Warn("cancel abandoned job %v: %v", job.ID, err)
 | |
| 			// go on
 | |
| 		}
 | |
| 		CreateCommitStatus(ctx, job)
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 |