[O] Better logging
This commit is contained in:
@@ -5,11 +5,13 @@ import { ObjectId } from "mongodb"
|
||||
export async function saveResult(data: Omit<ResultDocument, "_id" | "createdAt">): Promise<string> {
|
||||
const doc = { ...data, createdAt: new Date() }
|
||||
const res = await dbs.results.insertOne(doc)
|
||||
console.log(`Saved result ${res.insertedId} for user ${data.userId}`)
|
||||
return res.insertedId.toString()
|
||||
}
|
||||
|
||||
export async function getResult(id: string): Promise<ResultDocument | null> {
|
||||
try {
|
||||
console.log(`Getting result ${id}`)
|
||||
return await dbs.results.findOne({ _id: new ObjectId(id) })
|
||||
} catch {
|
||||
return null
|
||||
|
||||
@@ -12,7 +12,12 @@ const API_URL = process.env.AUDIO_SEPARATOR_API
|
||||
export async function separateSong(inputPath: string, outputDir: string) {
|
||||
const vocalsPath = path.join(outputDir, 'vocals.opus')
|
||||
const instrumentalPath = path.join(outputDir, 'instrumental.opus')
|
||||
if (await fs.exists(vocalsPath) && await fs.exists(instrumentalPath)) return
|
||||
if (await fs.exists(vocalsPath) && await fs.exists(instrumentalPath)) {
|
||||
console.log(`Separation already done for ${inputPath}`)
|
||||
return
|
||||
}
|
||||
|
||||
console.log(`Starting separation for ${inputPath}`)
|
||||
|
||||
// Read file and create FormData
|
||||
const fileBuffer = await fs.readFile(inputPath)
|
||||
@@ -40,6 +45,7 @@ export async function separateSong(inputPath: string, outputDir: string) {
|
||||
|
||||
await downloadStem('vocals', vocalsPath)
|
||||
await downloadStem('instrumental', instrumentalPath)
|
||||
console.log(`Separation completed for ${inputPath}`)
|
||||
|
||||
// Clean up task on server
|
||||
try {
|
||||
@@ -49,8 +55,14 @@ export async function separateSong(inputPath: string, outputDir: string) {
|
||||
}
|
||||
break
|
||||
}
|
||||
if (status.status === 'error') throw error(500, status.error || '分离失败')
|
||||
if (status.status === 'not_found') throw error(500, '任务丢失')
|
||||
if (status.status === 'error') {
|
||||
console.error(`Separation failed for ${inputPath}: ${status.error}`)
|
||||
throw error(500, status.error || '分离失败')
|
||||
}
|
||||
if (status.status === 'not_found') {
|
||||
console.error(`Separation task lost for ${inputPath}`)
|
||||
throw error(500, '任务丢失')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+10
-2
@@ -50,6 +50,7 @@ function parsePlaylistRef(ref: string): number {
|
||||
|
||||
const getPlaylistRaw = cached(dbs.playlistsRaw,
|
||||
async (id: number) => {
|
||||
console.log(`Fetching playlist raw ${id}`)
|
||||
const pl = ((await ne.playlist_detail({ id })).body as any).playlist
|
||||
for (const track of pl.tracks)
|
||||
await dbs.songsRaw.replaceOne({ _id: track.id }, { _id: track.id, data: track }, { upsert: true })
|
||||
@@ -147,7 +148,11 @@ const songProcessingStatus = new Map<number, SongProcessState>()
|
||||
export const getSongStatus = (songId: number) => songProcessingStatus.get(songId) || { items: [], status: 'idle' }
|
||||
export const checkLyricsProcessed = async (songId: number) => !!await dbs.lyricsProcessed.findOne({ _id: songId as any })
|
||||
export const prepareSong = async (songId: number) => {
|
||||
if (songProcessingStatus.has(songId)) return
|
||||
console.log(`Preparing song ${songId}`)
|
||||
if (songProcessingStatus.has(songId)) {
|
||||
console.log(`Song ${songId} is already being processed`)
|
||||
return
|
||||
}
|
||||
|
||||
const state: SongProcessState = { items: [], status: 'running' }
|
||||
songProcessingStatus.set(songId, state)
|
||||
@@ -196,7 +201,9 @@ export const prepareSong = async (songId: number) => {
|
||||
try {
|
||||
await Promise.all([processLyrics(), processMusic()])
|
||||
state.status = 'done'
|
||||
console.log(`Song ${songId} preparation done`)
|
||||
} catch (e) {
|
||||
console.error(`Song ${songId} preparation failed`, e)
|
||||
addTask('error', eToString(e)).progress = -1
|
||||
state.status = 'error'
|
||||
}
|
||||
@@ -261,7 +268,6 @@ async function processImport(session: ImportSession, data: any) {
|
||||
data.tracks = (await Promise.all(session.songs.map(async item => {
|
||||
try {
|
||||
const lyrics = await getLyricsRaw(item.song.id)
|
||||
console.log(`Song ${item.song.id} lang ${lyrics.lang}`)
|
||||
if (lyrics.lang === 'jpn') {
|
||||
item.status = 'success'
|
||||
return item.song
|
||||
@@ -298,6 +304,7 @@ export const listRecPlaylists = async () => {
|
||||
const list = await dbs.playlists.find({
|
||||
_id: { $in: defaultPlaylists }
|
||||
} as any).map(it => it.data).toArray()
|
||||
console.log(`Listing recommended playlists: ${list.length} found`)
|
||||
return list.sort((a: any, b: any) => defaultPlaylists.indexOf(a.id) - defaultPlaylists.indexOf(b.id))
|
||||
}
|
||||
export const listMyPlaylists = async (user: UserDocument) => (await user.data.myPlaylists?.let(pl => dbs.playlists.find({
|
||||
@@ -305,6 +312,7 @@ export const listMyPlaylists = async (user: UserDocument) => (await user.data.my
|
||||
}).map(it => it.data).toArray())) ?? []
|
||||
|
||||
export const getPlaylist = async (playlistId: number | string) => {
|
||||
console.log(`Getting playlist ${playlistId}`)
|
||||
const plData = await dbs.playlists.findOne({ _id: +playlistId as any })
|
||||
if (!plData) throw error(404, 'Playlist not found')
|
||||
return plData.data
|
||||
|
||||
@@ -16,6 +16,7 @@ void users.createIndex({ syncCode: 1 }, { name: "users_sync_code_idx" })
|
||||
export async function createUser(registUA: string): Promise<string> {
|
||||
const ses = `${crypto.randomUUID()}-${Date.now().toString(36)}`
|
||||
await users.insertOne({ registUA, createdAt: new Date(), sessions: [ses], data: {} })
|
||||
console.log(`Created new user with session ${ses}`)
|
||||
return ses
|
||||
}
|
||||
|
||||
@@ -41,6 +42,7 @@ export async function createSyncCode(session: string): Promise<string> {
|
||||
// Sync code is 4 * 5 numbers
|
||||
const code = Array.from({ length: 4 }, () => Math.floor(Math.random() * 100000).toString().padStart(5, '0')).join('-')
|
||||
await users.updateOne({ _id: user._id }, { $set: { syncCode: code, syncCodeCreated: new Date() } })
|
||||
console.log(`Created sync code for user ${user._id}`)
|
||||
return code
|
||||
}
|
||||
|
||||
@@ -52,6 +54,7 @@ export async function createSyncCode(session: string): Promise<string> {
|
||||
export async function updateUserData(user: UserDocument, data: Partial<UserData>): Promise<void> {
|
||||
const newData = { ...(user.data || {}), ...data }
|
||||
await users.updateOne({ _id: user._id }, { $set: { data: newData } })
|
||||
console.log(`Updated user data for ${user._id}`)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,6 +83,7 @@ export async function loginWithSyncCode(code: string, newUA: string): Promise<st
|
||||
$unset: { syncCode: "", syncCodeCreated: "" }
|
||||
}
|
||||
)
|
||||
console.log(`User ${user._id} logged in with sync code`)
|
||||
|
||||
return ses
|
||||
}
|
||||
@@ -13,8 +13,6 @@
|
||||
|
||||
const t = getI18n().home
|
||||
|
||||
console.log(data.recPlaylists)
|
||||
|
||||
const loc = data.user.data.loc
|
||||
const href = loc?.isFinished && loc?.lastResultId ? `/results/${loc.lastResultId}` : `/song/${data.last?.id}`
|
||||
</script>
|
||||
|
||||
@@ -7,6 +7,7 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
|
||||
if (!code) throw error(400, 'Missing sync code')
|
||||
|
||||
const ua = request.headers.get('user-agent') || 'unknown'
|
||||
console.log(`Login attempt with sync code from UA: ${ua}`)
|
||||
const session = await loginWithSyncCode(code, ua)
|
||||
|
||||
// Set session cookie
|
||||
|
||||
@@ -7,7 +7,10 @@ export const POST: RequestHandler = async ({ request }) => {
|
||||
if (!id) throw error(400, 'Import ID is required')
|
||||
|
||||
const session = getSession(id)
|
||||
if (!session) throw error(404, 'Session not found')
|
||||
if (!session) {
|
||||
console.log(`API: Import session ${id} not found`)
|
||||
throw error(404, 'Session not found')
|
||||
}
|
||||
|
||||
return json(session);
|
||||
};
|
||||
|
||||
@@ -11,6 +11,7 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
|
||||
if (!user) throw error(401, 'Unauthorized');
|
||||
|
||||
try {
|
||||
console.log(`API: Starting import for link ${link} by user ${user._id}`)
|
||||
return json(await startImport(link, user._id))
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
|
||||
@@ -10,6 +10,7 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
|
||||
data.userId = user._id
|
||||
|
||||
// Validate data here if needed
|
||||
console.log(`API: Saving result for user ${user._id}`)
|
||||
const id = await saveResult(data)
|
||||
return json({ id })
|
||||
} catch (e) {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { prepareSong, getSongStatus } from '$lib/server/songs'
|
||||
|
||||
export async function POST({ params }) {
|
||||
const songId = +params.id
|
||||
console.log(`API: Requesting preparation for song ${songId}`)
|
||||
prepareSong(songId) // Start in background
|
||||
return json({ status: 'started' })
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ export const POST: RequestHandler = async ({ request, cookies }) => {
|
||||
|
||||
try {
|
||||
const data = await request.json()
|
||||
console.log(`API: Updating user data for ${user._id}`)
|
||||
await updateUserData(user, data)
|
||||
return json({ success: true })
|
||||
} catch (e) {
|
||||
|
||||
@@ -7,5 +7,6 @@ export const POST: RequestHandler = async ({ cookies }) => {
|
||||
if (!session) throw error(401, 'Unauthorized')
|
||||
|
||||
const code = await createSyncCode(session)
|
||||
console.log(`API: Created sync code for session ${session}`)
|
||||
return json({ code })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user