Skip to content

Commit

Permalink
feat: task for running in backend (#629)
Browse files Browse the repository at this point in the history
* feat: display task page in console.

* feat: 给插件暴露提交任务和返回任务的内部接口

* docs: update CHANGELOG.MD
  • Loading branch information
ChiveHao committed Jul 19, 2024
1 parent 474178b commit 465f404
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- 默认主题的js问题修复
- 默认主题的条目详情页添加URL选择的剧集参数
- 默认主题的条目详情页播放逻辑
- 后台任务相关接口优化

## 依赖

Expand Down
10 changes: 10 additions & 0 deletions api/src/main/java/run/ikaros/api/core/task/TaskOperate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package run.ikaros.api.core.task;

import reactor.core.publisher.Mono;
import run.ikaros.api.plugin.AllowPluginOperate;

public interface TaskOperate extends AllowPluginOperate {
Mono<Void> submit(String name, Runnable runnable);

Mono<Void> cancel(String name);
}
5 changes: 5 additions & 0 deletions console/src/locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,11 @@ module:
sidebar: Users
authorities:
sidebar: Role Authorities
tasks:
title: Tasks
sidebar: Tasks
details:
title: Task Details
login:
operations:
submit:
Expand Down
5 changes: 5 additions & 0 deletions console/src/locales/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,11 @@ module:
sidebar: 角色
authorities:
sidebar: 角色权限
tasks:
title: 任务
sidebar: 任务
details:
title: 任务详情
login:
operations:
submit:
Expand Down
4 changes: 2 additions & 2 deletions console/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Subject from './content/subject/module';
import Plugin from './system/plugin/module';
import Setting from './system/setting/module';
import About from './system/about/module';
// import Tasks from './system/task/module';
import Tasks from './system/task/module';
import Users from './system/user/module';
import Roles from './system/role/module';

Expand All @@ -17,7 +17,7 @@ const coreModules = [
Plugin,
Setting,
About,
// Tasks,
Tasks,
Users,
Roles,
];
Expand Down
50 changes: 15 additions & 35 deletions console/src/modules/system/about/About.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ onMounted(fetchActuatorInfo);
</script>
<template>
<!-- Basic -->
<el-descriptions
class="margin-top"
title="Basic"
:column="3"
size="large"
border
>
<el-descriptions class="margin-top" title="Basic" :column="3" size="large" border>
<!-- git -->
<el-descriptions-item>
<template #label> Git Branch</template>
Expand Down Expand Up @@ -73,13 +67,7 @@ onMounted(fetchActuatorInfo);
<br/>

<!-- Jave -->
<el-descriptions
class="margin-top"
title="Java"
:column="3"
size="large"
border
>
<el-descriptions class="margin-top" title="Java" :column="3" size="large" border>
<!-- version -->
<el-descriptions-item>
<template #label> Java Version</template>
Expand Down Expand Up @@ -122,39 +110,31 @@ onMounted(fetchActuatorInfo);
<br/>

<!-- Introduce -->
<el-descriptions
class="margin-top"
title="Ikaros Project"
:column="1"
size="large"
direction="vertical"
border
>
<el-descriptions class="margin-top" title="Ikaros Project" :column="1" size="large" direction="vertical" border>
<el-descriptions-item>
<template #label> Offical</template>
<a href="https://ikaros.run" target="_blank">https://ikaros.run</a>
</el-descriptions-item>
<el-descriptions-item>
<template #label> GitHub</template>
<a href="https://github.com/ikaros-dev/ikaros" target="_blank"
>https://github.com/ikaros-dev/ikaros</a
>
<a href="https://github.com/ikaros-dev/ikaros" target="_blank">https://github.com/ikaros-dev/ikaros</a>
</el-descriptions-item>
<el-descriptions-item>
<template #label> Open API Documentation</template>
<a href="/webjars/swagger-ui/index.html" target="_blank">
/webjars/swagger-ui/index.html
</a>
</el-descriptions-item>
<el-descriptions-item>
<template #label> Contributors</template>
<el-image
style="width: 100%; height: 100%"
src="https://contrib.nn.ci/api?repo=ikaros-dev/ikaros&repo=ikaros-dev/docs&repo=ikaros-dev/app"
fit="fill"
/>
<el-image style="width: 100%; height: 100%"
src="https://contrib.nn.ci/api?repo=ikaros-dev/ikaros&repo=ikaros-dev/docs&repo=ikaros-dev/app"
fit="fill"/>
</el-descriptions-item>
<el-descriptions-item>
<template #label> Status</template>
<el-image
style="width: 100%; height: 100%"
src="https://repobeats.axiom.co/api/embed/f7285853048ff09f313f6483901e2af0e638f666.svg"
fit="fill"
/>
<el-image style="width: 100%; height: 100%"
src="https://repobeats.axiom.co/api/embed/f7285853048ff09f313f6483901e2af0e638f666.svg" fit="fill"/>
</el-descriptions-item>
</el-descriptions>
</template>
Expand Down
14 changes: 7 additions & 7 deletions console/src/modules/system/task/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { definePlugin } from '@runikaros/shared';
import {definePlugin} from '@runikaros/shared';
import Tasks from './Tasks.vue';
import { Ship } from '@element-plus/icons-vue';
import { markRaw } from 'vue';
import {MostlyCloudy} from '@element-plus/icons-vue';
import {markRaw} from 'vue';
import TaskDetails from './TaskDetails.vue';

export default definePlugin({
Expand All @@ -15,11 +15,11 @@ export default definePlugin({
name: 'Tasks',
component: Tasks,
meta: {
title: 'core.tasks.title',
title: 'module.tasks.title',
menu: {
name: 'core.sidebar.menu.items.tasks',
name: 'module.tasks.sidebar',
group: 'system',
icon: markRaw(Ship),
icon: markRaw(MostlyCloudy),
priority: 0,
},
},
Expand All @@ -32,7 +32,7 @@ export default definePlugin({
name: 'TaskDetails',
component: TaskDetails,
meta: {
title: 'core.tasks.details.title',
title: 'module.tasks.details.title',
hidden: true,
},
},
Expand Down
21 changes: 21 additions & 0 deletions server/src/main/java/run/ikaros/server/core/task/PluginTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package run.ikaros.server.core.task;

import run.ikaros.server.store.entity.TaskEntity;
import run.ikaros.server.store.repository.TaskRepository;

public class PluginTask extends Task {
public PluginTask(TaskEntity entity,
TaskRepository repository) {
super(entity, repository);
}

@Override
protected String getTaskEntityName() {
return this.getClass().getName() + "-";
}

@Override
protected void doRun() throws Exception {

}
}
53 changes: 53 additions & 0 deletions server/src/main/java/run/ikaros/server/core/task/TaskOperator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package run.ikaros.server.core.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import reactor.core.publisher.Mono;
import run.ikaros.api.core.task.TaskOperate;
import run.ikaros.server.store.entity.TaskEntity;
import run.ikaros.server.store.repository.TaskRepository;

@Slf4j
@Component
public class TaskOperator implements TaskOperate {
private final TaskService taskService;
private final TaskRepository taskRepository;

public TaskOperator(TaskService taskService,
TaskRepository taskRepository) {
this.taskService = taskService;
this.taskRepository = taskRepository;
}

@Override
public Mono<Void> submit(String name, Runnable runnable) {
Assert.hasText(name, "name must not be empty");
Assert.notNull(runnable, "runnable must not be null");

TaskEntity taskEntity = new TaskEntity();
taskEntity.setName(name);
taskService.setDefaultFieldValue(taskEntity);

PluginTask pluginTask = new PluginTask(taskEntity, taskRepository) {
@Override
protected String getTaskEntityName() {
return name;
}

@Override
protected void doRun() throws Exception {
runnable.run();
}
};

return taskService.submit(pluginTask);
}

@Override
public Mono<Void> cancel(String name) {
Assert.hasText(name, "name must not be empty");
return taskService.cancel(name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public interface TaskService {
Mono<PagingWrap<TaskEntity>> listEntitiesByCondition(FindTaskCondition findTaskCondition);

Mono<Long> getProcess(Long id);

void setDefaultFieldValue(TaskEntity entity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public Mono<Void> submit(Task task) {
.filter(taskEntity -> TaskStatus.RUNNING.equals(taskEntity.getStatus())
|| TaskStatus.CREATE.equals(taskEntity.getStatus()))
.collectList()
.filter(taskEntities -> taskEntities != null && taskEntities.size() > 0)
.filter(taskEntities -> taskEntities != null && !taskEntities.isEmpty())
.flatMap(
taskEntities -> Mono.error(new RuntimeException("Submission failed, task exists.")))
.switchIfEmpty(taskRepository.save(entity)
Expand All @@ -113,7 +113,10 @@ public Mono<Void> submit(Task task) {
).then();
}

private static void setDefaultFieldValue(TaskEntity entity) {
/**
* Set Default filed for task entity.
*/
public void setDefaultFieldValue(TaskEntity entity) {
if (entity.getStatus() == null) {
entity.setStatus(TaskStatus.CREATE);
}
Expand Down

0 comments on commit 465f404

Please sign in to comment.