Skip to content

Commit

Permalink
fix #178
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Nov 23, 2016
1 parent 7074483 commit 8aafa16
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@

import com.dangdang.ddframe.job.cloud.scheduler.config.CloudJobConfiguration;
import com.dangdang.ddframe.job.cloud.scheduler.config.CloudJobConfigurationGsonFactory;
import com.dangdang.ddframe.job.cloud.scheduler.config.ConfigurationService;
import com.dangdang.ddframe.job.cloud.scheduler.config.JobExecutionType;
import com.dangdang.ddframe.job.cloud.scheduler.lifecycle.LifecycleService;
import com.dangdang.ddframe.job.cloud.scheduler.producer.ProducerManager;
import com.dangdang.ddframe.job.cloud.scheduler.producer.ProducerManagerFactory;
import com.dangdang.ddframe.job.cloud.scheduler.state.ready.ReadyService;
import com.dangdang.ddframe.job.exception.JobSystemException;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.util.json.GsonFactory;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import org.apache.mesos.SchedulerDriver;

Expand All @@ -50,11 +55,17 @@ public final class CloudJobRestfulApi {

private final LifecycleService lifecycleService;

private final ConfigurationService configService;

private final ReadyService readyService;

public CloudJobRestfulApi() {
Preconditions.checkNotNull(schedulerDriver);
Preconditions.checkNotNull(regCenter);
producerManager = ProducerManagerFactory.getInstance(schedulerDriver, regCenter);
lifecycleService = new LifecycleService(schedulerDriver);
configService = new ConfigurationService(regCenter);
readyService = new ReadyService(regCenter);
}

/**
Expand Down Expand Up @@ -105,4 +116,20 @@ public void deregister(final String jobName) {
producerManager.deregister(jobName);
lifecycleService.killJob(jobName);
}

/**
* 触发一次作业.
*
* @param jobName 作业名称
*/
@POST
@Path("/trigger")
@Consumes(MediaType.APPLICATION_JSON)
public void trigger(final String jobName) {
Optional<CloudJobConfiguration> config = configService.load(jobName);
if (config.isPresent() && JobExecutionType.DAEMON == config.get().getJobExecutionType()) {
throw new JobSystemException("Daemon job '%s' cannot support trigger.", jobName);
}
readyService.addTransient(jobName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package com.dangdang.ddframe.job.cloud.scheduler.restful;

import com.dangdang.ddframe.job.cloud.scheduler.config.JobExecutionType;
import com.dangdang.ddframe.job.cloud.scheduler.fixture.CloudJsonConstants;
import com.dangdang.ddframe.job.cloud.scheduler.lifecycle.LifecycleService;
import com.dangdang.ddframe.job.cloud.scheduler.producer.ProducerManagerFactory;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.restful.RestfulServer;
Expand All @@ -30,7 +30,6 @@
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.unitils.util.ReflectionUtils;

Expand All @@ -49,9 +48,6 @@ public final class CloudJobRestfulApiTest {

private static CoordinatorRegistryCenter regCenter;

@Mock
private LifecycleService lifecycleService;

@BeforeClass
public static void setUpClass() throws Exception {
ReflectionUtils.setFieldValue(ProducerManagerFactory.class, ProducerManagerFactory.class.getDeclaredField("instance"), null);
Expand Down Expand Up @@ -97,6 +93,18 @@ public void assertDeregister() throws Exception {
verify(regCenter).get("/config/test_job");
}

@Test
public void assertTriggerWithDaemonJob() throws Exception {
when(regCenter.get("/config/test_job")).thenReturn(CloudJsonConstants.getJobJson(JobExecutionType.DAEMON));
assertThat(sentRequest("http://127.0.0.1:19000/job/trigger", "POST", "test_job"), is(500));
}

@Test
public void assertTriggerWithTransientJob() throws Exception {
when(regCenter.get("/config/test_job")).thenReturn(CloudJsonConstants.getJobJson());
assertThat(sentRequest("http://127.0.0.1:19000/job/trigger", "POST", "test_job"), is(204));
}

private static int sentRequest(final String url, final String method, final String content) throws Exception {
HttpClient httpClient = new HttpClient();
try {
Expand Down
4 changes: 4 additions & 0 deletions elastic-job-doc/content/post/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ weight=1

1. [ISSUE #177](https://github.com/dangdangdotcom/elastic-job/issues/177) 2.0.2版本Spring命名空间的job:script空指针

### 新功能

1. [ISSUE #178](https://github.com/dangdangdotcom/elastic-job/issues/178) 事件驱动触发作业

## 2.0.2

### 缺陷修正
Expand Down
16 changes: 16 additions & 0 deletions elastic-job-doc/content/post/user_guide/cloud/deploy_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,19 @@ url:`job/deregister`
```shell
curl -l -H "Content-type: application/json" -X DELETE -d 'foo_job' http://elastic_job_cloud_host:8899/job/deregister
```

### 触发一次作业

url:`job/trigger`

方法:`POST`

参数类型:`application/json`

参数:作业名称

说明:即事件驱动,通过调用`API`而非定时的触发作业。目前仅对`Transient`作业类型生效。

```shell
curl -l -H "Content-type: application/json" -X POST -d 'foo_job' http://elastic_job_cloud_host:8899/job/trigger
```

0 comments on commit 8aafa16

Please sign in to comment.