Refactor: Remove background task management and related hooks

- Deleted `useBackgroundTasks.ts` hook and its associated logic for managing background tasks.
- Removed `backgroundTaskManager.ts` service and all related task definitions and registrations.
- Cleaned up `Podfile.lock` and `package.json` to remove unused dependencies related to background tasks.
- Updated iOS project files to eliminate references to removed background task components.
- Added new background fetch identifier in `Info.plist` for future use.
This commit is contained in:
richarjiang
2025-09-05 09:47:49 +08:00
parent cb89ee7bc2
commit acb3907344
60 changed files with 77 additions and 2230 deletions

View File

@@ -1,374 +0,0 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as BackgroundTask from 'expo-background-task';
import * as TaskManager from 'expo-task-manager';
// 后台任务名称常量
const BACKGROUND_TASK_NAME = 'health-background-task';
// 必须在全局作用域定义任务处理器
TaskManager.defineTask(BACKGROUND_TASK_NAME, async () => {
console.log('======= 后台任务被系统调用 =======');
const now = new Date();
console.log(`后台任务执行时间: ${now.toISOString()}`);
try {
// 获取后台任务管理器实例并执行所有注册的任务
const manager = BackgroundTaskManager.getInstance();
console.log('开始执行所有注册的任务...');
const results = await manager.executeAllTasks();
console.log('后台任务执行结果:', results);
// 返回成功状态
return BackgroundTask.BackgroundTaskResult.Success;
} catch (error) {
console.error('后台任务执行失败:', error);
return BackgroundTask.BackgroundTaskResult.Failed;
}
});
// 任务类型定义
export interface BackgroundTaskConfig {
id: string;
name: string;
handler: (data?: any) => Promise<void>;
options?: {
minimumInterval?: number; // 最小间隔时间(分钟)
};
}
// 任务状态
export interface TaskStatus {
id: string;
isRegistered: boolean;
lastExecution?: Date;
nextExecution?: Date;
executionCount: number;
lastError?: string;
}
// 后台任务管理器类
class BackgroundTaskManager {
private static instance: BackgroundTaskManager;
private tasks: Map<string, BackgroundTaskConfig> = new Map();
private taskStatuses: Map<string, TaskStatus> = new Map();
private isInitialized = false;
private systemTaskRegistered = false;
// 单例模式
public static getInstance(): BackgroundTaskManager {
if (!BackgroundTaskManager.instance) {
BackgroundTaskManager.instance = new BackgroundTaskManager();
}
return BackgroundTaskManager.instance;
}
// 初始化后台任务管理器
public async initialize(): Promise<void> {
if (this.isInitialized) {
return;
}
try {
this.isInitialized = true;
// 注册后台任务
await this.registerSystemBackgroundTask();
// 加载已保存的任务状态
await this.loadTaskStatuses();
console.log('后台任务管理器初始化成功');
} catch (error) {
console.error('后台任务管理器初始化失败:', error);
throw error;
}
}
// 注册系统后台任务
private async registerSystemBackgroundTask(): Promise<void> {
console.log('开始注册系统后台任务...');
try {
// 检查后台任务状态
const status = await BackgroundTask.getStatusAsync();
console.log('后台任务服务状态:', BackgroundTask.BackgroundTaskStatus[status]);
if (status === BackgroundTask.BackgroundTaskStatus.Available) {
// 检查任务是否已经注册
const isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_TASK_NAME);
console.log('系统任务是否已注册:', isRegistered);
if (!isRegistered) {
// 注册后台任务
await BackgroundTask.registerTaskAsync(BACKGROUND_TASK_NAME);
console.log('✅ 系统后台任务注册成功');
this.systemTaskRegistered = true;
} else {
console.log('✅ 系统后台任务已经注册,跳过重复注册');
this.systemTaskRegistered = true;
}
} else {
const statusText = Object.keys(BackgroundTask.BackgroundTaskStatus).find(
key => BackgroundTask.BackgroundTaskStatus[key as keyof typeof BackgroundTask.BackgroundTaskStatus] === status
);
console.warn('❌ 后台任务服务不可用,状态:', statusText || status);
console.warn('可能的原因:');
console.warn('- 设备省电模式开启');
console.warn('- 后台应用刷新被禁用');
console.warn('- 设备电量过低');
this.systemTaskRegistered = false;
}
} catch (error) {
console.error('❌ 注册系统后台任务失败:', error);
this.systemTaskRegistered = false;
throw error;
}
}
// 注册自定义任务
public async registerTask(task: BackgroundTaskConfig): Promise<void> {
try {
// 检查任务是否已存在
if (this.tasks.has(task.id)) {
console.warn(`任务 ${task.id} 已存在,将被覆盖`);
}
// 保存任务
this.tasks.set(task.id, task);
// 初始化任务状态
if (!this.taskStatuses.has(task.id)) {
this.taskStatuses.set(task.id, {
id: task.id,
isRegistered: true,
executionCount: 0,
});
}
// 保存任务状态
await this.saveTaskStatuses();
console.log(`任务 ${task.id} 注册成功`);
} catch (error) {
console.error(`注册任务 ${task.id} 失败:`, error);
throw error;
}
}
// 取消注册任务
public async unregisterTask(taskId: string): Promise<void> {
try {
// 移除任务
this.tasks.delete(taskId);
// 更新任务状态
const status = this.taskStatuses.get(taskId);
if (status) {
status.isRegistered = false;
await this.saveTaskStatuses();
}
console.log(`任务 ${taskId} 取消注册成功`);
} catch (error) {
console.error(`取消注册任务 ${taskId} 失败:`, error);
throw error;
}
}
// 手动执行任务
public async executeTask(taskId: string, data?: any): Promise<void> {
try {
const task = this.tasks.get(taskId);
if (!task) {
throw new Error(`任务 ${taskId} 不存在`);
}
console.log(`开始执行任务: ${taskId}`);
// 执行任务
await task.handler(data);
// 更新任务状态
const status = this.taskStatuses.get(taskId);
if (status) {
status.lastExecution = new Date();
status.executionCount += 1;
status.lastError = undefined;
await this.saveTaskStatuses();
}
console.log(`任务 ${taskId} 执行成功`);
} catch (error) {
console.error(`执行任务 ${taskId} 失败:`, error);
// 更新错误状态
const status = this.taskStatuses.get(taskId);
if (status) {
status.lastError = error instanceof Error ? error.message : String(error);
await this.saveTaskStatuses();
}
throw error;
}
}
// 执行所有任务
public async executeAllTasks(): Promise<{ [taskId: string]: 'success' | 'failed' }> {
const results: { [taskId: string]: 'success' | 'failed' } = {};
for (const [taskId, task] of Array.from(this.tasks.entries())) {
try {
await this.executeTask(taskId);
results[taskId] = 'success';
} catch (error) {
console.error(`执行任务 ${taskId} 失败:`, error);
results[taskId] = 'failed';
}
}
return results;
}
// 获取任务状态
public getTaskStatus(taskId: string): TaskStatus | undefined {
return this.taskStatuses.get(taskId);
}
// 获取所有任务状态
public getAllTaskStatuses(): TaskStatus[] {
return Array.from(this.taskStatuses.values());
}
// 获取已注册的任务列表
public getRegisteredTasks(): BackgroundTaskConfig[] {
return Array.from(this.tasks.values());
}
// 检查后台任务状态
public async getBackgroundTaskStatus(): Promise<BackgroundTask.BackgroundTaskStatus> {
return await BackgroundTask.getStatusAsync();
}
// 保存任务状态到本地存储
private async saveTaskStatuses(): Promise<void> {
try {
const statuses = Array.from(this.taskStatuses.values());
await AsyncStorage.setItem('@background_task_statuses', JSON.stringify(statuses));
} catch (error) {
console.error('保存任务状态失败:', error);
}
}
// 从本地存储加载任务状态
private async loadTaskStatuses(): Promise<void> {
try {
const statusesJson = await AsyncStorage.getItem('@background_task_statuses');
if (statusesJson) {
const statuses: TaskStatus[] = JSON.parse(statusesJson);
statuses.forEach(status => {
this.taskStatuses.set(status.id, status);
});
}
} catch (error) {
console.error('加载任务状态失败:', error);
}
}
// 清理过期的任务状态
public async cleanupTaskStatuses(): Promise<void> {
const now = new Date();
const thirtyDaysAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
for (const [taskId, status] of this.taskStatuses) {
if (status.lastExecution && status.lastExecution < thirtyDaysAgo && !status.isRegistered) {
this.taskStatuses.delete(taskId);
}
}
await this.saveTaskStatuses();
}
// 手动触发后台任务(仅开发环境)
public async triggerTaskForTesting(): Promise<void> {
if (!__DEV__) {
console.warn('⚠️ triggerTaskForTesting 仅在开发环境可用');
return;
}
try {
console.log('🧪 触发后台任务进行测试...');
await BackgroundTask.triggerTaskWorkerForTestingAsync();
console.log('✅ 后台任务测试触发成功');
} catch (error) {
console.error('❌ 触发后台任务测试失败:', error);
}
}
// 调试函数:显示后台任务状态
public async debugExecuteBackgroundTask(): Promise<void> {
console.log('===============================');
console.log('🔧 调试:后台任务状态检查');
console.log('===============================');
try {
// 获取后台任务状态
const status = await this.getBackgroundTaskStatus();
const statusText = Object.keys(BackgroundTask.BackgroundTaskStatus).find(
key => BackgroundTask.BackgroundTaskStatus[key as keyof typeof BackgroundTask.BackgroundTaskStatus] === status
);
console.log('📊 后台任务服务状态:', statusText || status);
// 检查系统任务是否已注册
const isSystemTaskRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_TASK_NAME);
console.log('🔄 系统后台任务是否已注册:', isSystemTaskRegistered);
console.log('🔄 管理器中的系统任务状态:', this.systemTaskRegistered);
// 显示自定义任务信息
console.log('📝 当前注册的自定义任务数量:', this.tasks.size);
this.tasks.forEach((task, id) => {
console.log(` - 任务ID: ${id}, 名称: ${task.name}`);
});
if (this.tasks.size === 0) {
console.warn('⚠️ 没有注册的自定义任务');
}
// 手动执行所有任务
console.log('🚀 手动执行所有注册的任务...');
const results = await this.executeAllTasks();
console.log('✅ 任务执行结果:', results);
// 显示详细的任务状态
console.log('📈 任务执行统计:');
const taskStatuses = this.getAllTaskStatuses();
taskStatuses.forEach(taskStatus => {
console.log(` 📊 任务 ${taskStatus.id}:`, {
注册状态: taskStatus.isRegistered ? '✅ 已注册' : '❌ 未注册',
执行次数: taskStatus.executionCount,
最后执行: taskStatus.lastExecution?.toLocaleString('zh-CN') || '从未执行',
最后错误: taskStatus.lastError || '无'
});
});
// 开发环境下触发测试
if (__DEV__) {
console.log('🧪 开发环境:触发后台任务测试...');
await this.triggerTaskForTesting();
}
console.log('===============================');
console.log('✅ 调试检查完成');
console.log('===============================');
} catch (error) {
console.error('❌ 调试执行失败:', error);
console.log('===============================');
}
}
}
// 导出单例实例
export const backgroundTaskManager = BackgroundTaskManager.getInstance();
// 导出类型
export type { BackgroundTaskConfig as BackgroundTaskType, TaskStatus as TaskStatusType };