* lib-scheduler-engines/src/main/java/com/fourelementscapital/scheduler/engines/PythonScript.java

* lib-scheduler-engines/src/main/java/com/fourelementscapital/scheduler/group/PythonScriptTask.java
      Added new: the new python engine based on cpython

  * lib-scheduler-queue/src/main/java/com/fourelementscapital/scheduler/peer/QueueFactory.java
      Added: the new factory for cpython

  * lib-scheduler-queue/src/main/java/com/fourelementscapital/scheduler/ScheduledTaskFactory.java
      Adjusted: accordingly to hook up the new engine
            Associated SQL statements needed in bbsync database:

              update scheduler_group set enginetype = 'pscript4pythonengine'
                where taskuid = 'rscript4rserveunix_python';

              insert into scheduler_taskpeers( taskuid, peername )
                values( 'rscript4rserveunix_python', '4ecapsvid13' );
This commit is contained in:
2021-12-16 15:31:45 +08:00
parent 15c5bd748d
commit adf29ba998
4 changed files with 279 additions and 41 deletions
@@ -0,0 +1,117 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.fourelementscapital.scheduler.engines;
import org.quartz.JobExecutionException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import com.fourelementscapital.scheduler.exception.SchedulerException;
/**
*
* @author bernarto
*/
public abstract class PythonScript extends ScheduledTask {
public PythonScript(String name, String uid) {
super(name, uid);
}
@Override
public void execute(StackFrame sframe) throws JobExecutionException,
SchedulerException,
Exception {
if (sframe == null || sframe.getData() == null ||
sframe.getData().get("rscript") == null ||
sframe.getData().get("id") == null) {
return;
}
String script = (String)sframe.getData().get("rscript");
Number nid = (Number)sframe.getData().get("id");
File py = File.createTempFile( nid.intValue() + "-" +
sframe.getTrigger_time(), ".py");
try ( FileWriter writer = new FileWriter(py) ) {
writer.write( script );
}
StringBuilder sb = new StringBuilder(1024);
Process process = runPythonScript( py.getName() );
int exitcode = 0;
try ( InputStreamReader isr =
new InputStreamReader(process.getInputStream()) ) {
try ( BufferedReader reader = new BufferedReader(isr) ) {
String line = reader.readLine();
while (line != null) {
sb.append(line).append("\n");
line = reader.readLine();
}
}
}
finally {
sframe.setConsole_message( sb.toString() );
exitcode = process.waitFor();
sframe.setTasklog(null);
py.delete();
}
if (exitcode != 0) {
throw new SyntaxError("Syntax errors in python script");
}
}
private Process runPythonScript(String scriptfile) throws IOException {
synchronized (this) {
getBuilder().command("python", scriptfile);
return getBuilder().start();
}
}
private ProcessBuilder getBuilder() {
ProcessBuilder local = null;
if ((local = _builder) == null) {
synchronized (this) {
if ((local = _builder) == null) {
_builder = new ProcessBuilder();
_builder.redirectErrorStream( true );
// _builder.environment().put("PYTHONPATH", "");
// _builder.environment().put("PYTHONHOME", "");
String tmpdir = System.getProperty("java.io.tmpdir");
if (tmpdir != null && tmpdir.length() > 0) {
_builder.directory( new File(tmpdir) );
}
}
}
}
return _builder;
}
private volatile ProcessBuilder _builder;
public class SyntaxError extends SchedulerException {
public SyntaxError(String msg) {
super(msg);
}
public int getErrorcode() {
return ERROR_SERVER_GENERAL_SCRIPT_ERROR;
}
}
}
@@ -0,0 +1,77 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.fourelementscapital.scheduler.group;
import com.fourelementscapital.db.vo.FlexiField;
import com.fourelementscapital.scheduler.engines.PythonScript;
import com.fourelementscapital.scheduler.engines.ScheduledTaskField;
import java.util.Vector;
/**
*
* @author bernarto
*/
public class PythonScriptTask extends PythonScript {
public static final String ENGINE_NAME = "pscript4pythonengine";
public PythonScriptTask(String name, String uid) {
super (name, uid);
try {
addFormFields( getMyFields() );
}
catch ( Exception e ) {
e.printStackTrace();
}
}
protected Vector<ScheduledTaskField> getMyFields(){
Vector<ScheduledTaskField> fields = new Vector<>();
//list
ScheduledTaskField f3 = new ScheduledTaskField();
f3.setShortname( "rscript_param" );
f3.setFieldlabel("");
f3.setFieldtype( FlexiField.TYPE_RSCRIPTEDITOR_PARAM );
f3.setPlacementform( "codeinject" );
fields.add( f3 );
ScheduledTaskField f4 = new ScheduledTaskField();
f4.setShortname( "rscript" );
f4.setFieldlabel( "Python Script" );
f4.setFieldtype( FlexiField.TYPE_RSCRIPTEDITOR );
fields.add(f4);
fields.addAll( getAdditionalRScriptField() );
return fields;
}
public Vector<ScheduledTaskField> getAdditionalRScriptField(){
Vector<ScheduledTaskField> fields = new Vector<>();
ScheduledTaskField f5=new ScheduledTaskField();
f5.setShortname( "trigger_commodity" );
f5.setFieldlabel( "Trigger Commodity" );
f5.setFieldtype( FlexiField.TYPE_TEXTBOX );
f5.setFineprint( "Building block which fixing time defines the "
+ "trigger time for Zero-day lag indicators e.g: CL,C,JNso");
f5.setPlacementform( "buildingblock" );
fields.add( f5 );
ScheduledTaskField f6 = new ScheduledTaskField();
f6.setShortname( "trigger_frequency" );
f6.setFieldlabel( "Trigger Frequency" );
f6.setFieldtype( FlexiField.TYPE_TEXTBOX );
f6.setFineprint( "Specific Intervals: 1/3 (starting at 1 and every 3 minutes)" );
f6.setPlacementform( "buildingblock" );
fields.add( f6 );
return fields;
}
}