Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 53 additions & 24 deletions ui/src/composables/use-migrate-task-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,55 @@ export function useMigrateTaskRunner(taskGroups: Ref<MigrateTaskGroup[]>) {
const failedTasks = computed(() => allTasks.value.filter((task) => task.status === 'failed'))
const hasFailedTasks = computed(() => failedTasks.value.length > 0)

function taskKey(task: MigrateTaskItem<any>) {
return `${task.type}:${task.id}`
}

async function runTasksInDependencyOrder(tasks: MigrateTaskItem<any>[]) {
const tasksToRun = tasks.filter((task) => task.status !== 'running')
const remaining = new Set(tasksToRun)
const taskByKey = new Map(allTasks.value.map((task) => [taskKey(task), task]))

tasksToRun.forEach((task) => {
task.status = 'pending'
task.error = undefined
})

while (remaining.size > 0) {
const readyTasks = Array.from(remaining).filter((task) =>
(task.dependsOn || []).every((dependencyKey) => {
const dependency = taskByKey.get(dependencyKey)
return !dependency || dependency.status === 'success'
})
)

if (readyTasks.length === 0) {
const hasRunningDependency = Array.from(remaining).some((task) =>
(task.dependsOn || []).some(
(dependencyKey) => taskByKey.get(dependencyKey)?.status === 'running'
)
)
if (hasRunningDependency) {
await taskQueue.drained()
continue
}

remaining.forEach((task) => {
const unresolvedDependencies = (task.dependsOn || []).filter((dependencyKey) => {
const dependency = taskByKey.get(dependencyKey)
return dependency && dependency.status !== 'success'
})
task.status = 'failed'
task.error = `依赖任务未成功:${unresolvedDependencies.join('、')}`
})
break
}

await Promise.all(readyTasks.map((task) => taskQueue.push(task)))
readyTasks.forEach((task) => remaining.delete(task))
}
}

watch(
taskGroups,
(groups) => {
Expand All @@ -49,10 +98,7 @@ export function useMigrateTaskRunner(taskGroups: Ref<MigrateTaskGroup[]>) {
return
}

task.status = 'pending'
task.error = undefined
importLoading.value = true
taskQueue.push(task)
void retryTask(task)
}
})
})
Expand Down Expand Up @@ -94,19 +140,7 @@ export function useMigrateTaskRunner(taskGroups: Ref<MigrateTaskGroup[]>) {
isImportStarted.value = true
beforeUnloadGuard.enable()

const tasksToRun = [...pendingTasks.value, ...failedTasks.value]
tasksToRun.forEach((task) => {
if (task.status !== 'running') {
task.status = 'pending'
task.error = undefined
}

taskQueue.push(task).catch(() => {
// error is handled in asyncWorker
})
})

await taskQueue.drained()
await runTasksInDependencyOrder([...pendingTasks.value, ...failedTasks.value])

importLoading.value = false
beforeUnloadGuard.disable()
Expand All @@ -128,11 +162,7 @@ export function useMigrateTaskRunner(taskGroups: Ref<MigrateTaskGroup[]>) {
}

importLoading.value = true
failedTasks.value.forEach((task) => {
task.retry()
})

await taskQueue.drained()
await runTasksInDependencyOrder([...failedTasks.value])

importLoading.value = false

Expand All @@ -148,9 +178,8 @@ export function useMigrateTaskRunner(taskGroups: Ref<MigrateTaskGroup[]>) {
}

async function retryTask(task: MigrateTaskItem) {
task.retry()
importLoading.value = true
await taskQueue.drained()
await runTasksInDependencyOrder([task])
importLoading.value = false
}

Expand Down
11 changes: 9 additions & 2 deletions ui/src/composables/use-migrate-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ function createTaskItem<T>(
type: string,
label: string,
item: T,
run: () => Promise<TaskResponse>
run: () => Promise<TaskResponse>,
dependsOn?: string[]
): MigrateTaskItem<T> {
return {
id,
type,
label,
item,
...(dependsOn?.length ? { dependsOn } : {}),
status: 'pending',
run,
retry: () => {}
Expand Down Expand Up @@ -244,6 +246,10 @@ export function useMigrateTask(
)
)
} else {
const dependsOn = [`comment:${comment.spec.commentName}`]
if (comment.spec.quoteReply) {
dependsOn.push(`reply:${comment.spec.quoteReply}`)
}
commentTasks.push(
createTaskItem(
comment.metadata?.name || 'unknown',
Expand All @@ -253,7 +259,8 @@ export function useMigrateTask(
() =>
coreApiClient.content.reply.createReply({
reply: comment as Reply
})
}),
dependsOn
)
)
}
Expand Down
17 changes: 9 additions & 8 deletions ui/src/modules/wordpress/use-wordpress-data-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ export function useWordPressDataParser(file: File): useWordPressDataParserReturn
const createReply = (
reply: Comment,
refType: 'Post' | 'SinglePage',
replyTarget: { commentName: string; quoteReply: string },
replyTarget: { commentName: string; quoteReply?: string },
authorById: Map<number, Author>
): MigrateReply => {
const fallbackAuthor = authorById.get(reply['wp:comment_user_id'])
Expand Down Expand Up @@ -368,7 +368,7 @@ export function useWordPressDataParser(file: File): useWordPressDataParserReturn
creationTime: new Date(reply['wp:comment_date']).toISOString(),
hidden: false,
commentName: replyTarget.commentName,
quoteReply: replyTarget.quoteReply
...(replyTarget.quoteReply ? { quoteReply: replyTarget.quoteReply } : {})
},
status: {},
ownerRef:
Expand Down Expand Up @@ -398,13 +398,13 @@ export function useWordPressDataParser(file: File): useWordPressDataParserReturn
function resolveWordPressReplyTarget(
comment: Comment,
commentById: Map<string, Comment>
): { commentName: string; quoteReply: string } | undefined {
): { commentName: string; quoteReply?: string } | undefined {
if (comment['wp:comment_parent'] === 0) {
return undefined
}

const quoteReply = String(comment['wp:comment_parent'])
const directParent = commentById.get(quoteReply)
const directParentName = String(comment['wp:comment_parent'])
const directParent = commentById.get(directParentName)
if (!directParent) {
return undefined
}
Expand All @@ -415,7 +415,7 @@ export function useWordPressDataParser(file: File): useWordPressDataParserReturn
while (rootComment['wp:comment_parent'] !== 0) {
const currentId = String(rootComment['wp:comment_id'])
if (visited.has(currentId)) {
break
return undefined
}

visited.add(currentId)
Expand All @@ -427,9 +427,10 @@ export function useWordPressDataParser(file: File): useWordPressDataParserReturn
rootComment = nextParent
}

const commentName = String(rootComment['wp:comment_id'])
return {
commentName: String(rootComment['wp:comment_id']),
quoteReply
commentName,
...(directParentName !== commentName ? { quoteReply: directParentName } : {})
}
}

Expand Down
1 change: 1 addition & 0 deletions ui/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export interface MigrateTaskItem<T = any> {
type: string
label: string
item: T
dependsOn?: string[]
status: MigrateTaskState
error?: string
run: () => Promise<AxiosResponse<any, any>>
Expand Down
Loading