Skip to content

Commit

Permalink
Cancel bulk 107 (#688)
Browse files Browse the repository at this point in the history
* _UniPush

* be: isInterruptState

* web Notification onclick

* 批量审批/批量修改可终止

* be: task cancel
  • Loading branch information
getrebuild authored Dec 5, 2023
1 parent 40d38f2 commit 3f7feaf
Show file tree
Hide file tree
Showing 25 changed files with 169 additions and 109 deletions.
2 changes: 1 addition & 1 deletion @rbv
Submodule @rbv updated from cb938a to d868eb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ protected Integer exec() {
"update `{0}` set `{1}` = ''%s'' where `{2}` = ''%s''", deptNew.toLiteral(), user.toLiteral());
int changed = 0;
for (Entity e : MetadataHelper.getEntities()) {
if (this.isInterrupt()) {
this.setInterrupted();
log.error("Task interrupted : " + user + " > " + deptNew);
break;
}
if (this.isInterruptState()) break;

if (!MetadataHelper.hasPrivilegesField(e)) {
this.addCompleted();
continue;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/rebuild/core/privileges/UserImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ protected Integer exec() {
this.setTotal(data.size());

for (Cell[] row : data) {
if (isInterruptState()) break;

String loginName = cellAsString(row, 0);
String password = cellAsString(row, 1);
if (StringUtils.isBlank(loginName) || StringUtils.isBlank(password)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ protected Integer exec() throws Exception {

boolean first = true;
for (Cell[] row : rows) {
if (isInterruptState()) break;

if (first) {
first = false;
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ protected Integer exec() {
final EntityService ies = Application.getEntityService(rule.getToEntity().getEntityCode());

for (final Cell[] row : rows) {
if (isInterrupt()) {
this.setInterrupted();
break;
}
if (isInterruptState()) break;

final Cell firstCell = row == null || row.length == 0 ? null : row[0];
if (firstCell == null || firstCell.getRowNo() == 0) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public Integer exec() {
ID firstAssigned = null;
NotificationOnce.begin();
for (ID id : records) {
if (isInterruptState()) break;

if (Application.getPrivilegesManager().allowAssign(context.getOpUser(), id)) {
int a = ges.assign(id, context.getToUser(), context.getCascades());
if (a > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public Integer exec() {
final ID[] willUpdates = prepareRecords();
this.setTotal(willUpdates.length);

if (isInterruptState()) return getSucceeded();

JSONArray updateContents = ((JSONObject) context.getExtraParams().get("customData"))
.getJSONArray("updateContents");

Expand Down Expand Up @@ -73,6 +75,8 @@ public Integer exec() {

GeneralEntityServiceContextHolder.setRepeatedCheckMode(GeneralEntityServiceContextHolder.RCM_CHECK_ALL);
for (ID id : willUpdates) {
if (isInterruptState()) break;

if (Application.getPrivilegesManager().allowUpdate(context.getOpUser(), id)) {
// 更新记录
formJson.getJSONObject(JsonRecordCreator.META_FIELD).put("id", id.toLiteral());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public Integer exec() {

String lastError = null;
for (ID id : records) {
if (isInterruptState()) break;

if (Application.getPrivilegesManager().allowDelete(context.getOpUser(), id)) {
try {
int d = ges.delete(id, context.getCascades());
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/rebuild/core/service/general/BulkShare.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public Integer exec() {
ID firstShared = null;
NotificationOnce.begin();
for (ID id : records) {
if (isInterruptState()) break;

if (Application.getPrivilegesManager().allowShare(context.getOpUser(), id)) {
int a = ges.share(id, context.getToUser(), context.getCascades(), shareRights);
if (a > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public Integer exec() {

// 这里的取消实际是删除了共享表记录
for (ID id : records) {
if (isInterruptState()) break;

int a = ges.unshare(realTarget, id);
if (a > 0) this.addSucceeded();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ protected Integer exec() {

this.setTotal(records.size() + this.getTotal() + 1);
for (Record o : records) {
if (this.isInterrupt()) {
this.setInterrupted();
break;
}
if (this.isInterruptState()) break;

try {
String quickCodeNew = generateQuickCode(o);
Expand All @@ -97,7 +94,7 @@ protected Integer exec() {
}
}

if (records.size() < PAGE_SIZE || this.isInterrupted()) break;
if (records.size() < PAGE_SIZE || this.isInterruptState()) break;
}

this.setTotal(this.getTotal() - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ public Integer exec() {

setTotal(array.length);
for (Object[] o : array) {
if (this.isInterrupt()) {
this.setInterrupted();
break;
}
if (this.isInterruptState()) break;

try {
String series = SeriesGeneratorFactory.generate(
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/rebuild/core/support/CommandArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class CommandArgs {

public static final String _ForceTour = "_ForceTour";
public static final String _HeavyStopWatcher = "_HeavyStopWatcher";
public static final String _UniPush = "_UniPush";

/**
* @param name
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/com/rebuild/core/support/task/HeavyTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
@Slf4j
public abstract class HeavyTask<T> extends SetUser implements Runnable {

volatile private boolean interrupt = false;
volatile private boolean interruptState = false;

/**
Expand Down Expand Up @@ -164,19 +163,24 @@ public JSON getExecResults() {

// 中断处理。是否允许中断由子类决定(实现)

public void interrupt() {
this.interrupt = true;
}

protected boolean isInterrupt() {
return interrupt;
}

protected void setInterrupted() {
/**
* 设置中断
*/
public void setInterruptState() {
this.interruptState = true;
}

public boolean isInterrupted() {
/**
* 是否中断(含手动中断与VM中断)
*
* @return
*/
public boolean isInterruptState() {
if (this.interruptState) return true;
if (Thread.currentThread().isInterrupted()) {
log.warn("Current thread is interrupted (by VM) : {}", Thread.currentThread());
setInterruptState();
}
return interruptState;
}

Expand Down Expand Up @@ -217,6 +221,6 @@ protected void completedAfter() {

@Override
public String toString() {
return "HeavyTask#" + getClass().getSimpleName();
return "HeavyTask#" + getClass();
}
}
16 changes: 9 additions & 7 deletions src/main/java/com/rebuild/core/support/task/TaskExecutors.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import cn.devezhao.commons.CodecUtils;
import cn.devezhao.commons.ThreadPool;
import cn.devezhao.persist4j.engine.ID;
import com.rebuild.core.RebuildException;
import com.rebuild.core.support.distributed.DistributedJobLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
Expand Down Expand Up @@ -74,14 +73,17 @@ public static String submit(HeavyTask<?> task, ID execUser) {
public static boolean cancel(String taskid) {
HeavyTask<?> task = TASKS.get(taskid);
if (task == null) {
throw new RebuildException("No Task found : " + taskid);
log.warn("No task found : {}", taskid);
return false;
}
task.interrupt();

task.setInterruptState();

boolean interrupted = false;
for (int i = 1; i <= 3; i++) {
for (int i = 1; i <= 4; i++) {
ThreadPool.waitFor(i * 500);
if (task.isInterrupted()) {

if (task.isInterruptState()) {
interrupted = true;
break;
}
Expand Down Expand Up @@ -149,11 +151,11 @@ public void executeJob() {
long leftTime = (System.currentTimeMillis() - task.getCompletedTime().getTime()) / 1000;
if (leftTime > 60 * 120) {
TASKS.remove(e.getKey());
log.info("HeavyTask clean up : " + e.getKey());
log.info("HeavyTask clean-up : {}", e.getKey());
}
completed++;
}
log.info("{} task(s) in the queue. {} are completed (will clean up later)", TASKS.size(), completed);
log.info("{} task(s) in the queue. {} are completed (will clean-up later)", TASKS.size(), completed);
}

Queue<Runnable> queue = ((ThreadPoolExecutor) SINGLE_QUEUE).getQueue();
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/com/rebuild/web/commons/HeavyTaskController.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ public JSONAware taskCancel(HttpServletRequest request) {
return RespBody.errorl("无法终止,因为任务已经完成");
}

task.interrupt();
for (int i = 0; i < 10; i++) {
if (task.isInterrupted()) {
task.setInterruptState();

for (int i = 1; i <= 4; i++) {
ThreadPool.waitFor(i * 500);

if (task.isInterruptState()) {
return formatTaskState(task);
}
ThreadPool.waitFor(200);
}

return RespBody.errorl("无法终止任务");
}

Expand All @@ -82,10 +83,10 @@ private JSON formatTaskState(HeavyTask<?> task) {
state.put("completed", task.getCompleted());
state.put("succeeded", task.getSucceeded());
state.put("isCompleted", task.isCompleted());
state.put("isInterrupted", task.isInterrupted());
state.put("isInterrupted", task.isInterruptState());
state.put("elapsedTime", task.getElapsedTime());
state.put("hasError", task.getErrorMessage());
if (task.isCompleted() || task.isInterrupted()) state.put("execResults", task.getExecResults());
if (task.isCompleted() || task.isInterruptState()) state.put("execResults", task.getExecResults());
return state;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/web/assets/js/admin/data-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ $(document).ready(() => {
}

window.onbeforeunload = function () {
if (import_inprogress === true) return false
if (import_inprogress === true) return 'SHOW-CLOSE-CONFIRM'
}

$('.J_step3-trace').on('click', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/web/assets/js/charts/chart-design.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ $(document).ready(() => {
window.onbeforeunload = function () {
const cfg = build_config()
if ((!cfg && !wpc.chartId) || $same(cfg, wpc.chartConfig)) return undefined
return 'CLOSE CONFIRM'
return 'SHOW-CLOSE-CONFIRM'
}
})

Expand Down
Loading

0 comments on commit 3f7feaf

Please sign in to comment.