mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
add cron job api's
This commit is contained in:
parent
3e2ea3d4df
commit
73f3b2b7d8
4 changed files with 157 additions and 0 deletions
|
|
@ -30,12 +30,14 @@ import org.apache.zeppelin.interpreter.InterpreterSetting;
|
|||
import org.apache.zeppelin.notebook.Note;
|
||||
import org.apache.zeppelin.notebook.Notebook;
|
||||
import org.apache.zeppelin.notebook.Paragraph;
|
||||
import org.apache.zeppelin.rest.message.CronRequest;
|
||||
import org.apache.zeppelin.rest.message.InterpreterSettingListForNoteBind;
|
||||
import org.apache.zeppelin.rest.message.NewInterpreterSettingRequest;
|
||||
import org.apache.zeppelin.rest.message.NewNotebookRequest;
|
||||
import org.apache.zeppelin.server.JsonResponse;
|
||||
import org.apache.zeppelin.server.ZeppelinServer;
|
||||
import org.apache.zeppelin.socket.NotebookServer;
|
||||
import org.quartz.CronExpression;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
|
@ -300,4 +302,81 @@ public class NotebookRestApi {
|
|||
p.abort();
|
||||
return new JsonResponse(Status.ACCEPTED).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register cron job REST API
|
||||
* @param message - JSON with cron expressions.
|
||||
* @return JSON with status.ACCEPTED
|
||||
* @throws IOException, IllegalArgumentException
|
||||
*/
|
||||
@POST
|
||||
@Path("cron/{notebookId}")
|
||||
public Response registerCronJob(@PathParam("notebookId") String notebookId, String message) throws
|
||||
IOException, IllegalArgumentException {
|
||||
logger.info("Register cron job note={} request cron msg={}", notebookId, message);
|
||||
|
||||
CronRequest request = gson.fromJson(message,
|
||||
CronRequest.class);
|
||||
|
||||
Note note = notebook.getNote(notebookId);
|
||||
if (note == null) {
|
||||
return new JsonResponse(Status.NOT_FOUND, "note not found.").build();
|
||||
}
|
||||
|
||||
if (!CronExpression.isValidExpression(request.getCronString())) {
|
||||
return new JsonResponse(Status.BAD_REQUEST, "wrong cron expressions.").build();
|
||||
}
|
||||
|
||||
Map<String, Object> config = note.getConfig();
|
||||
config.put("cron", request.getCronString());
|
||||
note.setConfig(config);
|
||||
notebook.refreshCron(note.id());
|
||||
|
||||
return new JsonResponse(Status.ACCEPTED).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove cron job REST API
|
||||
* @param
|
||||
* @return JSON with status.ACCEPTED
|
||||
* @throws IOException, IllegalArgumentException
|
||||
*/
|
||||
@DELETE
|
||||
@Path("cron/{notebookId}")
|
||||
public Response removeCronJob(@PathParam("notebookId") String notebookId) throws
|
||||
IOException, IllegalArgumentException {
|
||||
logger.info("Remove cron job note {}", notebookId);
|
||||
|
||||
Note note = notebook.getNote(notebookId);
|
||||
if (note == null) {
|
||||
return new JsonResponse(Status.NOT_FOUND, "note not found.").build();
|
||||
}
|
||||
|
||||
Map<String, Object> config = note.getConfig();
|
||||
config.put("cron", null);
|
||||
note.setConfig(config);
|
||||
notebook.refreshCron(note.id());
|
||||
|
||||
return new JsonResponse(Status.ACCEPTED).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cron job REST API
|
||||
* @param
|
||||
* @return JSON with status.ACCEPTED
|
||||
* @throws IOException, IllegalArgumentException
|
||||
*/
|
||||
@GET
|
||||
@Path("cron/{notebookId}")
|
||||
public Response getCronJob(@PathParam("notebookId") String notebookId) throws
|
||||
IOException, IllegalArgumentException {
|
||||
logger.info("Get cron job note {}", notebookId);
|
||||
|
||||
Note note = notebook.getNote(notebookId);
|
||||
if (note == null) {
|
||||
return new JsonResponse(Status.NOT_FOUND, "note not found.").build();
|
||||
}
|
||||
|
||||
return new JsonResponse(Status.ACCEPTED, note.getConfig().get("cron")).build();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.zeppelin.rest.message;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.zeppelin.interpreter.InterpreterOption;
|
||||
|
||||
/**
|
||||
* CronRequest rest api request message
|
||||
*
|
||||
*/
|
||||
public class CronRequest {
|
||||
String cron;
|
||||
|
||||
public CronRequest (){
|
||||
|
||||
}
|
||||
|
||||
public String getCronString() {
|
||||
return cron;
|
||||
}
|
||||
}
|
||||
|
|
@ -395,6 +395,10 @@ public abstract class AbstractTestRestApi {
|
|||
|
||||
protected Matcher<? super HttpMethodBase> isAccepted() { return responsesWith(202); }
|
||||
|
||||
protected Matcher<? super HttpMethodBase> isBadRequest() { return responsesWith(400); }
|
||||
|
||||
protected Matcher<? super HttpMethodBase> isNotFound() { return responsesWith(404); }
|
||||
|
||||
protected Matcher<? super HttpMethodBase> isNotAllowed() {
|
||||
return responsesWith(405);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -339,5 +339,41 @@ public class ZeppelinRestApiTest extends AbstractTestRestApi {
|
|||
//cleanup
|
||||
ZeppelinServer.notebook.removeNote(note.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCronJobs() throws InterruptedException, IOException{
|
||||
// create a note and a paragraph
|
||||
Note note = ZeppelinServer.notebook.createNote();
|
||||
|
||||
note.setName("note for run test");
|
||||
Paragraph paragraph = note.addParagraph();
|
||||
paragraph.setText("%md This is test paragraph.");
|
||||
|
||||
String jsonRequest = "{\"cron\":\"* * * * * ?\" }";
|
||||
// right cron expression but not exist note.
|
||||
PostMethod postCron = httpPost("/notebook/cron/notexistnote", jsonRequest);
|
||||
assertThat("", postCron, isNotFound());
|
||||
postCron.releaseConnection();
|
||||
Thread.sleep(1000);
|
||||
|
||||
// right cron expression.
|
||||
postCron = httpPost("/notebook/cron/" + note.getId(), jsonRequest);
|
||||
assertThat("", postCron, isAccepted());
|
||||
postCron.releaseConnection();
|
||||
Thread.sleep(1000);
|
||||
|
||||
// wrong cron expression.
|
||||
jsonRequest = "{\"cron\":\"a * * * * ?\" }";
|
||||
postCron = httpPost("/notebook/cron/" + note.getId(), jsonRequest);
|
||||
assertThat("", postCron, isBadRequest());
|
||||
postCron.releaseConnection();
|
||||
Thread.sleep(1000);
|
||||
|
||||
// remove cron job.
|
||||
DeleteMethod deleteCron = httpDelete("/notebook/cron/" + note.getId());
|
||||
assertThat("", deleteCron, isAccepted());
|
||||
deleteCron.releaseConnection();
|
||||
ZeppelinServer.notebook.removeNote(note.getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue