Files
ragflow/internal/dao/task.go
Jack c6eee09ed3 feat: migrate POST /api/v1/datasets/<dataset_id>/documents/stop to Go (#15597)
## Summary

Migrate the stop parse documents endpoint from Python to Go.

### Python endpoint
`POST /api/v1/datasets/<dataset_id>/documents/stop` —
`api/apps/restful_apis/document_api.py:1542-1641`

### Changes
| File | Change |
|------|--------|
| `internal/dao/task.go` | Add `GetByDocID` method |
| `internal/dao/task_test.go` | 3 DAO tests (new file) |
| `internal/service/document.go` | Add `StopParseDocuments` + refactor
shared helpers |
| `internal/service/document_test.go` | 8 service tests |
| `internal/handler/document.go` | Add handler + request struct +
interface |
| `internal/handler/document_test.go` | 5 handler tests |
| `internal/router/router.go` | Add `POST /:dataset_id/documents/stop`
route |

### How it works
1. Validates all document IDs belong to the dataset
2. For each document in RUNNING/CANCEL state (or with unfinished tasks):
- Sets Redis cancel signal `{task_id}-cancel` for each associated task
   - Updates `document.run` to CANCEL ("2")
3. Returns `{"success_count": N, "errors": [...]}`

### Test strategy
- **DAO/Service**: SQLite in-memory DB, zero mocks. Redis is nil-safe by
design.
- **Handler**: `fakeDocumentService` implementing `documentServiceIface`
interface.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-06-04 14:16:13 +08:00

73 lines
2.1 KiB
Go

//
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package dao
import (
"ragflow/internal/entity"
)
// TaskDAO task data access object
type TaskDAO struct{}
// NewTaskDAO create task DAO
func NewTaskDAO() *TaskDAO {
return &TaskDAO{}
}
// Create creates a new task
func (dao *TaskDAO) Create(task *entity.Task) error {
return DB.Create(task).Error
}
// GetByID gets task by ID
func (dao *TaskDAO) GetByID(id string) (*entity.Task, error) {
var task entity.Task
err := DB.Where("id = ?", id).First(&task).Error
if err != nil {
return nil, err
}
return &task, nil
}
// DeleteByDocIDs deletes tasks by document IDs (hard delete)
func (dao *TaskDAO) DeleteByDocIDs(docIDs []string) (int64, error) {
if len(docIDs) == 0 {
return 0, nil
}
result := DB.Unscoped().Where("doc_id IN ?", docIDs).Delete(&entity.Task{})
return result.RowsAffected, result.Error
}
// DeleteByTenantID deletes all tasks by tenant ID (hard delete via document join)
func (dao *TaskDAO) DeleteByTenantID(tenantID string) (int64, error) {
result := DB.Unscoped().Where("doc_id IN (SELECT id FROM document WHERE tenant_id = ?)", tenantID).Delete(&entity.Task{})
return result.RowsAffected, result.Error
}
// GetByDocID gets all tasks by document ID
func (dao *TaskDAO) GetByDocID(docID string) ([]*entity.Task, error) {
var tasks []*entity.Task
err := DB.Where("doc_id = ?", docID).Find(&tasks).Error
return tasks, err
}
func (dao *TaskDAO) GetAllTasks() ([]*entity.Task, error) {
var tasks []*entity.Task
err := DB.Find(&tasks).Error
return tasks, err
}