Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

3.6 KB
Raw Blame
//
//  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 (
	"context"

	"ragflow/internal/common"
	"ragflow/internal/entity"

	"gorm.io/gorm"
)

// 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(ctx context.Context, db *gorm.DB, task *entity.Task) error {
	return db.WithContext(ctx).Create(task).Error
}

// CreateMany creates multiple tasks in one batch.
func (dao *TaskDAO) CreateMany(ctx context.Context, db *gorm.DB, tasks []*entity.Task) error {
	if len(tasks) == 0 {
		return nil
	}
	return db.WithContext(ctx).Create(&tasks).Error
}

// GetByID gets task by ID
func (dao *TaskDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.Task, error) {
	var task entity.Task
	err := db.WithContext(ctx).Take(&task, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &task, nil
}

// UpdateProgress sets the progress and progress message of a task
func (dao *TaskDAO) UpdateProgress(ctx context.Context, db *gorm.DB, id string, progress float64, progressMsg string) error {
	return db.WithContext(ctx).Model(&entity.Task{}).Where("id = ?", id).Updates(map[string]any{
		"progress":     progress,
		"progress_msg": progressMsg,
	}).Error
}

// MarkActiveMemoryTaskFailed marks an unfinished memory task's UI projection
// as failed without overwriting a task that completed or failed concurrently.
func (dao *TaskDAO) MarkActiveMemoryTaskFailed(ctx context.Context, db *gorm.DB, id, progressMsg string) (bool, error) {
	result := db.WithContext(ctx).Model(&entity.Task{}).
		Where("id = ? AND task_type = ? AND progress >= 0 AND progress < 1", id, common.TaskTypeMemory).
		Updates(map[string]any{
			"progress":     -1,
			"progress_msg": progressMsg,
		})
	return result.RowsAffected == 1, result.Error
}

// DeleteIngestionTasksByDocIDs deletes ingestion tasks by document IDs (hard delete)
func (dao *TaskDAO) DeleteIngestionTasksByDocIDs(ctx context.Context, db *gorm.DB, docIDs []string) (int64, error) {
	if len(docIDs) == 0 {
		return 0, nil
	}
	result := db.WithContext(ctx).Unscoped().Where("document_id IN ?", docIDs).Delete(&entity.IngestionTask{})
	return result.RowsAffected, result.Error
}

// DeleteByDocIDs deletes tasks by document IDs (hard delete)
func (dao *TaskDAO) DeleteByDocIDs(ctx context.Context, db *gorm.DB, docIDs []string) (int64, error) {
	if len(docIDs) == 0 {
		return 0, nil
	}
	result := db.WithContext(ctx).Unscoped().Where("doc_id IN ?", docIDs).Delete(&entity.Task{})
	return result.RowsAffected, result.Error
}

// GetByDocID gets all tasks by document ID
func (dao *TaskDAO) GetByDocID(ctx context.Context, db *gorm.DB, docID string) ([]*entity.Task, error) {
	var tasks []*entity.Task
	err := db.WithContext(ctx).Where("doc_id = ?", docID).Order("from_page ASC, create_time ASC").Find(&tasks).Error
	return tasks, err
}

func (dao *TaskDAO) GetAllTasks(ctx context.Context, db *gorm.DB) ([]*entity.Task, error) {
	var tasks []*entity.Task
	err := db.WithContext(ctx).Find(&tasks).Error
	return tasks, err
}