mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-02-26 14:14:44 +08:00
feat: refine cron job
This commit is contained in:
@@ -1,13 +1,45 @@
|
||||
/* eslint-disable no-console */
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { db } from '@/lib/db';
|
||||
import { fetchVideoDetail } from '@/lib/fetchVideoDetail';
|
||||
import { SearchResult } from '@/lib/types';
|
||||
|
||||
const STORAGE_TYPE = process.env.NEXT_PUBLIC_STORAGE_TYPE ?? 'localstorage';
|
||||
export const runtime = 'edge';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
console.log(request.url);
|
||||
try {
|
||||
console.log('Cron job triggered:', new Date().toISOString());
|
||||
|
||||
refreshRecordAndFavorites();
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Cron job executed successfully',
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Cron job failed:', error);
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: 'Cron job failed',
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
timestamp: new Date().toISOString(),
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshRecordAndFavorites() {
|
||||
if (STORAGE_TYPE === 'localstorage') {
|
||||
if (
|
||||
process.env.NEXT_PUBLIC_STORAGE_TYPE ||
|
||||
'localstorage' === 'localstorage'
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -155,5 +187,3 @@ async function refreshRecordAndFavorites() {
|
||||
console.error('刷新播放记录/收藏任务启动失败', err);
|
||||
}
|
||||
}
|
||||
|
||||
export default refreshRecordAndFavorites;
|
||||
@@ -1,6 +1,5 @@
|
||||
import type { Metadata, Viewport } from 'next';
|
||||
import { Inter } from 'next/font/google';
|
||||
import '../lib/cron';
|
||||
|
||||
import './globals.css';
|
||||
import 'sweetalert2/dist/sweetalert2.min.css';
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
import cron from 'node-cron';
|
||||
|
||||
import refreshRecordAndFavorites from '@/lib/refreshRecordAndFavorites';
|
||||
|
||||
/*
|
||||
* 初始化定时任务:每个小时的 02 分执行一次。
|
||||
* 若需要添加更多任务,可在此文件中继续编写。
|
||||
*/
|
||||
|
||||
declare global {
|
||||
// 避免在开发热重载或多次导入时重复初始化
|
||||
// eslint-disable-next-line no-var
|
||||
var __moonTVCronInitialized: boolean | undefined;
|
||||
}
|
||||
|
||||
if (!global.__moonTVCronInitialized) {
|
||||
cron.schedule(
|
||||
'2 * * * *',
|
||||
async () => {
|
||||
refreshRecordAndFavorites();
|
||||
},
|
||||
{
|
||||
timezone: 'Asia/Shanghai',
|
||||
}
|
||||
);
|
||||
|
||||
global.__moonTVCronInitialized = true;
|
||||
}
|
||||
|
||||
export {}; // 仅用于确保这是一个模块
|
||||
@@ -1,12 +1,8 @@
|
||||
/* eslint-disable no-console, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
|
||||
|
||||
import { getRequestContext } from '@cloudflare/next-on-pages';
|
||||
|
||||
import { AdminConfig } from './admin.types';
|
||||
import { Favorite, IStorage, PlayRecord } from './types';
|
||||
|
||||
export const runtime = 'edge';
|
||||
|
||||
// 搜索历史最大条数
|
||||
const SEARCH_HISTORY_LIMIT = 20;
|
||||
|
||||
@@ -120,17 +116,6 @@ const INIT_SQL = `
|
||||
|
||||
// 获取全局D1数据库实例
|
||||
function getD1Database(): D1Database {
|
||||
try {
|
||||
// 在 Cloudflare Pages 环境中,通过 getRequestContext 访问 D1 数据库
|
||||
const { env } = getRequestContext();
|
||||
if (env && (env as any).DB) {
|
||||
return (env as any).DB as D1Database;
|
||||
}
|
||||
} catch (error) {
|
||||
// 如果 getRequestContext 失败,继续尝试其他方式
|
||||
console.warn('Failed to get request context:', error);
|
||||
}
|
||||
|
||||
// 在 next-on-pages 环境中,D1 数据库可能通过 process.env 暴露
|
||||
if (typeof process !== 'undefined' && (process.env as any).DB) {
|
||||
return (process.env as any).DB as D1Database;
|
||||
@@ -145,10 +130,6 @@ export class D1Storage implements IStorage {
|
||||
private db: D1Database | null = null;
|
||||
private initialized = false;
|
||||
|
||||
constructor() {
|
||||
// 不在构造函数中初始化数据库,延迟到实际使用时
|
||||
}
|
||||
|
||||
private async getDatabase(): Promise<D1Database> {
|
||||
if (!this.db) {
|
||||
this.db = getD1Database();
|
||||
|
||||
Reference in New Issue
Block a user