Skip to content

Commit

Permalink
修改job接口
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jan 18, 2025
1 parent 04a73dd commit 6e2ffcb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 25 deletions.
65 changes: 48 additions & 17 deletions nop-job/nop-job-api/src/main/java/io/nop/job/api/IJobScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
*/
package io.nop.job.api;

import io.nop.api.core.annotations.biz.BizMutation;
import io.nop.api.core.annotations.biz.BizObjName;
import io.nop.api.core.annotations.biz.BizQuery;
import io.nop.api.core.annotations.core.Name;
import io.nop.api.core.exceptions.NopException;
import io.nop.job.api.spec.JobSpec;

import jakarta.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -19,17 +23,24 @@
import static io.nop.job.api.JobApiErrors.ARG_JOB_NAME;
import static io.nop.job.api.JobApiErrors.ERR_JOB_UNKNOWN_JOB;

@BizObjName("JobScheduler")
public interface IJobScheduler {

@BizQuery
List<String> getAllJobNames();

JobDetail getJobDetail(String jobName);
@BizQuery
@Nullable
JobDetail getJobDetail(@Name("jobName") String jobName);

/**
* 得到指定任务的状态信息
*
* @param ignoreUnknown 当jobName对应的任务不存在时是否抛出异常
*/
default List<JobDetail> getJobDetails(Set<String> jobNames, boolean ignoreUnknown) {
@BizQuery
default List<JobDetail> getJobDetails(@Name("jobNames") Set<String> jobNames,
@Name("ignoreUnknown") boolean ignoreUnknown) {
List<JobDetail> ret = new ArrayList<>(jobNames.size());
for (String jobName : jobNames) {
JobDetail detail = getJobDetail(jobName);
Expand All @@ -46,9 +57,15 @@ default List<JobDetail> getJobDetails(Set<String> jobNames, boolean ignoreUnknow
/**
* 加入任务,并自动启动trigger
*/
void addJob(JobSpec spec, boolean allowUpdate, boolean startTrigger);

default void addJobs(Collection<JobSpec> specs, boolean allowUpdate, boolean startTrigger) {
@BizMutation
void addJob(@Name("jobSpec") JobSpec spec,
@Name("allowUpdate") boolean allowUpdate,
@Name("startTrigger") boolean startTrigger);

@BizMutation
default void addJobs(@Name("specs") Collection<JobSpec> specs,
@Name("allowUpdate") boolean allowUpdate,
@Name("startTrigger") boolean startTrigger) {
for (JobSpec spec : specs) {
addJob(spec, allowUpdate, startTrigger);
}
Expand All @@ -57,9 +74,11 @@ default void addJobs(Collection<JobSpec> specs, boolean allowUpdate, boolean sta
/**
* 删除任务。如果任务当前处于运行状态,则会先取消任务
*/
boolean removeJob(String jobName);
@BizMutation
boolean removeJob(@Name("jobName") String jobName);

default boolean removeJobs(Collection<String> jobNames) {
@BizMutation
default boolean removeJobs(@Name("jobNames") Collection<String> jobNames) {
if (jobNames == null)
return false;

Expand All @@ -71,6 +90,7 @@ default boolean removeJobs(Collection<String> jobNames) {
return b;
}

@BizMutation
default boolean clearJobs() {
return removeJobs(getAllJobNames());
}
Expand All @@ -79,14 +99,17 @@ default boolean clearJobs() {
* 获取job当前状态。如果没有找到已注册的job,则返回null
*/
@Nullable
TriggerStatus getTriggerStatus(String jobName);
@BizQuery
TriggerStatus getTriggerStatus(@Name("jobName") String jobName);

/**
* 启动已经注册的任务
*/
boolean startJob(String jobName);
@BizQuery
boolean startJob(@Name("jobName") String jobName);

default boolean startJobs(Collection<String> jobNames) {
@BizQuery
default boolean startJobs(@Name("jobNames") Collection<String> jobNames) {
if (jobNames == null)
return false;

Expand All @@ -98,16 +121,19 @@ default boolean startJobs(Collection<String> jobNames) {
return b;
}

@BizQuery
default boolean startAllJobs() {
return startJobs(getAllJobNames());
}

/**
* 暂停任务。但是任务仍然保存在调度器中,并没有被删除
*/
boolean pauseJob(String jobName);
@BizQuery
boolean pauseJob(@Name("jobName") String jobName);

default boolean pauseJobs(Collection<String> jobNames) {
@BizQuery
default boolean pauseJobs(@Name("jobNames") Collection<String> jobNames) {
if (jobNames == null)
return false;

Expand All @@ -119,16 +145,19 @@ default boolean pauseJobs(Collection<String> jobNames) {
return b;
}

@BizMutation
default boolean pauseAllJobs() {
return pauseJobs(getAllJobNames());
}

/**
* 取消任务。任务取消后会进入取消状态,不会被自动调度
*/
boolean cancelJob(String jobName);
@BizMutation
boolean cancelJob(@Name("jobName") String jobName);

default boolean cancelJobs(Collection<String> jobNames) {
@BizMutation
default boolean cancelJobs(@Name("jobNames") Collection<String> jobNames) {
if (jobNames == null)
return false;

Expand All @@ -140,21 +169,23 @@ default boolean cancelJobs(Collection<String> jobNames) {
return b;
}

@BizMutation
default boolean cancelAllJobs() {
return cancelJobs(getAllJobNames());
}

/**
* 手动触发一次任务。如果任务正在执行,则返回false。如果任务没有处于调度状态,则临时调度一次。 任何时刻同一个jobName对应的任务只会有一个实例在执行。
*/
boolean fireNow(String jobName);
@BizMutation
boolean fireNow(@Name("jobName") String jobName);

/**
* 从数据库中装载持久化任务。
*
* @param epoch 每次leader选举都产生一个新的epoch号
*/
void activate(long epoch);
void activate(@Name("epoch") long epoch);

/**
* deactivate之后不允许再接收外部指令。负载均衡场景下只有一个主任务调度器允许运行,如果发生主从切换,从服务器要执行deactivate操作。 处于deactivate状态的调度器不会再修改数据库。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
import io.nop.api.core.annotations.data.DataBean;

import java.time.MonthDay;
import java.util.List;

@DataBean
public class AnnualCalendarSpec extends CalendarSpec {
private static final long serialVersionUID = 1L;

private MonthDay[] excludes;
private List<MonthDay> excludes;

public MonthDay[] getExcludes() {
public List<MonthDay> getExcludes() {
return excludes;
}

public void setExcludes(MonthDay[] excludes) {
public void setExcludes(List<MonthDay> excludes) {
this.excludes = excludes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.io.Serializable;
import java.time.LocalDate;
import java.time.MonthDay;
import java.util.Arrays;
import java.util.List;

public class AnnualCalendar extends BaseCalendar implements ICalendar, Serializable {

Expand All @@ -22,15 +22,15 @@ public class AnnualCalendar extends BaseCalendar implements ICalendar, Serializa
/**
* excludeDays需要按时间顺序排好序
*/
private MonthDay[] excludeDays;
private List<MonthDay> excludeDays;

public AnnualCalendar(ICalendar baseCalendar) {
super(baseCalendar);
}

public void setExcludeDays(MonthDay[] excludeDays) {
public void setExcludeDays(List<MonthDay> excludeDays) {
this.excludeDays = excludeDays;
Arrays.sort(excludeDays);
excludeDays.sort(MonthDay::compareTo);
}

private boolean isExcludedDay(LocalDate day) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static ICalendar buildCalendar(List<CalendarSpec> calendars) {
for (CalendarSpec calInfo : calendars) {
if (calInfo instanceof AnnualCalendarSpec) {
AnnualCalendarSpec spec = (AnnualCalendarSpec) calInfo;
if (ArrayHelper.isEmpty(spec.getExcludes()))
if (CollectionHelper.isEmpty(spec.getExcludes()))
continue;

AnnualCalendar annual = new AnnualCalendar(cal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ private boolean changeLeader(NopSysClusterLeader leader, IEntityDao<NopSysCluste
@Override
public void restartElection() {
IEntityDao<NopSysClusterLeader> dao = dao();
IEstimatedClock clock = dao.getDbEstimatedClock();

for (int i = 0; i < 10; i++) {
NopSysClusterLeader leader = getEntity(dao);
Expand All @@ -227,6 +228,7 @@ public void restartElection() {

// 增大epoch将导致当前的leader发现epoch已改变,需要重新获取leader
leader.setLeaderEpoch(leader.getLeaderEpoch() + 1);
leader.setExpireAt(clock.getMinCurrentTime());
leader.orm_disableVersionCheckError(true);
dao.updateEntityDirectly(leader);

Expand Down

0 comments on commit 6e2ffcb

Please sign in to comment.