Skip to content

Commit

Permalink
[Feature] Add project name/workflow definition name/instance id/task …
Browse files Browse the repository at this point in the history
…name to the built-in variables
  • Loading branch information
haibingtown committed May 15, 2023
1 parent a3b2cc0 commit 9cd3118
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 24 deletions.
16 changes: 11 additions & 5 deletions docs/docs/en/guide/parameter/built-in.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

## Basic Built-in Parameter

| Variable | Declaration Method | Meaning |
|--------------------|-------------------------|---------------------------------------------------------------------------------------------|
| system.biz.date | `${system.biz.date}` | The day before the schedule time of the daily scheduling instance, the format is `yyyyMMdd` |
| system.biz.curdate | `${system.biz.curdate}` | The schedule time of the daily scheduling instance, the format is `yyyyMMdd` |
| system.datetime | `${system.datetime}` | The schedule time of the daily scheduling instance, the format is `yyyyMMddHHmmss` |
| Variable | Declaration Method | Meaning |
|---------------------------------|--------------------------------------|---------------------------------------------------------------------------------------------|
| system.biz.date | `${system.biz.date}` | The day before the schedule time of the daily scheduling instance, the format is `yyyyMMdd` |
| system.biz.curdate | `${system.biz.curdate}` | The schedule time of the daily scheduling instance, the format is `yyyyMMdd` |
| system.datetime | `${system.datetime}` | The schedule time of the daily scheduling instance, the format is `yyyyMMddHHmmss` |
| system.task.execute.path | `${system.task.execute.path}` | the absolute path of current executing task |
| system.task.instance.id | `${ssystem.task.instance.id}` | the instance id of current task |
| system.task.definition.name | `${system.task.definition.name}` | the definition name of current task |
| system.workflow.instance.id | `${system.workflow.instance.id}` | the instance id of the workflow to which current task belongs |
| system.workflow.definition.name | `${system.workflow.definition.name}` | the definition name of the workflow to which current task belongs |
| system.project.name | `${system.project.name}` | the name of the project to which current task belongs |

## Extended Built-in Parameter

Expand Down
31 changes: 28 additions & 3 deletions docs/docs/zh/guide/parameter/built-in.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,34 @@
<td>日常调度实例定时的定时时间,格式为 yyyyMMdd</td>
</tr>
<tr>
<td>system.datetime</td>
<td>${system.datetime}</td>
<td>日常调度实例定时的定时时间,格式为 yyyyMMddHHmmss</td>
<td>system.task.execute.path</td>
<td>${system.task.execute.path}</td>
<td>当前任务执行的绝对路径</td>
</tr>
<tr>
<td>system.task.instance.id</td>
<td>${system.task.instance.id}</td>
<td>当前任务实例的ID</td>
</tr>
<tr>
<td>system.task.definition.name</td>
<td>${system.task.definition.name}</td>
<td>当前任务所属任务定义的名称</td>
</tr>
<tr>
<td>system.workflow.instance.id</td>
<td>${system.workflow.instance.id}</td>
<td>当前任务所属工作流实例ID</td>
</tr>
<tr>
<td>system.workflow.definition.name</td>
<td>${system.workflow.definition.name}</td>
<td>当前任务所属工作流定义的名称</td>
</tr>
<tr>
<td>system.project.name</td>
<td>${system.project.name}</td>
<td>当前任务所在项目的名称</td>
</tr>
</table>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

package org.apache.dolphinscheduler.service.expand;

import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.PARAMETER_TASK_EXECUTE_PATH;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.PARAMETER_TASK_INSTANCE_ID;

import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.common.constants.DateConstants;
import org.apache.dolphinscheduler.common.enums.CommandType;
Expand Down Expand Up @@ -49,6 +46,14 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.PARAMETER_TASK_EXECUTE_PATH;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.PARAMETER_TASK_INSTANCE_ID;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.PARAMETER_TASK_DEFINITION_NAME;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.PARAMETER_WORKFLOW_INSTANCE_ID;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.PARAMETER_WORKFLOW_DEFINITION_NAME;
import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.PARAMETER_PROJECT_NAME;


@Component
public class CuringGlobalParams implements CuringParamsService {

Expand Down Expand Up @@ -123,7 +128,7 @@ public String curingGlobalParams(Integer processInstanceId, Map<String, String>
}

/**
* the global parameters and local parameters used in the worker will be prepared here.
* the global parameters and local parameters used in the worker will be prepared here, and build-in parameters.
*
* @param taskInstance
* @param parameters
Expand All @@ -137,8 +142,6 @@ public Map<String, Property> paramParsingPreparation(@NonNull TaskInstance taskI
// assign value to definedParams here
Map<String, String> globalParamsMap = setGlobalParamsMap(processInstance);
Map<String, Property> globalParams = ParamUtils.getUserDefParamsMap(globalParamsMap);
CommandType commandType = processInstance.getCmdTypeIfComplement();
Date scheduleTime = processInstance.getScheduleTime();

// combining local and global parameters
Map<String, Property> localParams = parameters.getInputLocalParametersMap();
Expand All @@ -155,16 +158,13 @@ public Map<String, Property> paramParsingPreparation(@NonNull TaskInstance taskI
// of the process instance complement
Map<String, String> cmdParam = JSONUtils.toMap(processInstance.getCommandParam());
String timeZone = cmdParam.get(Constants.SCHEDULE_TIMEZONE);
Map<String, String> params = BusinessTimeUtils.getBusinessTime(commandType, scheduleTime, timeZone);

if (MapUtils.isNotEmpty(globalParamsMap)) {
params.putAll(globalParamsMap);
}
// build-in params
Map<String, String> params = setBuildInParamsMap(taskInstance, timeZone);

if (StringUtils.isNotBlank(taskInstance.getExecutePath())) {
params.put(PARAMETER_TASK_EXECUTE_PATH, taskInstance.getExecutePath());
if (MapUtils.isNotEmpty(params)) {
globalParams.putAll(ParamUtils.getUserDefParamsMap(params));
}
params.put(PARAMETER_TASK_INSTANCE_ID, Integer.toString(taskInstance.getId()));

if (MapUtils.isNotEmpty(varParams)) {
globalParams.putAll(varParams);
Expand Down Expand Up @@ -206,6 +206,28 @@ public Map<String, Property> paramParsingPreparation(@NonNull TaskInstance taskI
return globalParams;
}

/**
* build all built-in parameters
* @param taskInstance
* @param timeZone
*/
private Map<String, String> setBuildInParamsMap(@NonNull TaskInstance taskInstance,
@NonNull String timeZone) {
CommandType commandType = taskInstance.getProcessInstance().getCmdTypeIfComplement();
Date scheduleTime = taskInstance.getProcessInstance().getScheduleTime();

Map<String, String> params = BusinessTimeUtils.getBusinessTime(commandType, scheduleTime, timeZone);

if (StringUtils.isNotBlank(taskInstance.getExecutePath())) {
params.put(PARAMETER_TASK_EXECUTE_PATH, taskInstance.getExecutePath());
}
params.put(PARAMETER_TASK_INSTANCE_ID, Integer.toString(taskInstance.getId()));
params.put(PARAMETER_TASK_DEFINITION_NAME, taskInstance.getTaskDefine().getName());
params.put(PARAMETER_WORKFLOW_INSTANCE_ID, Integer.toString(taskInstance.getProcessInstance().getId()));
params.put(PARAMETER_WORKFLOW_DEFINITION_NAME, taskInstance.getProcessInstance().getProcessDefinition().getName());
params.put(PARAMETER_PROJECT_NAME, taskInstance.getProcessInstance().getProcessDefinition().getProjectName());
return params;
}
private Map<String, String> setGlobalParamsMap(ProcessInstance processInstance) {
Map<String, String> globalParamsMap = new HashMap<>(16);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.regex.Pattern;

import com.google.common.collect.Sets;
import org.apache.dolphinscheduler.common.constants.DateConstants;

public class TaskConstants {

Expand Down Expand Up @@ -165,17 +166,17 @@ private TaskConstants() {
/**
* system date(yyyyMMddHHmmss)
*/
public static final String PARAMETER_DATETIME = "system.datetime";
public static final String PARAMETER_DATETIME = DateConstants.PARAMETER_DATETIME;

/**
* system date(yyyymmdd) today
*/
public static final String PARAMETER_CURRENT_DATE = "system.biz.curdate";
public static final String PARAMETER_CURRENT_DATE = DateConstants.PARAMETER_CURRENT_DATE;

/**
* system date(yyyymmdd) yesterday
*/
public static final String PARAMETER_BUSINESS_DATE = "system.biz.date";
public static final String PARAMETER_BUSINESS_DATE = DateConstants.PARAMETER_BUSINESS_DATE;

/**
* the absolute path of current executing task
Expand All @@ -187,6 +188,25 @@ private TaskConstants() {
*/
public static final String PARAMETER_TASK_INSTANCE_ID = "system.task.instance.id";

/**
* the definition name of current task
*/
public static final String PARAMETER_TASK_DEFINITION_NAME = "system.task.definition.name";

/**
* the instance id of the workflow to which current task belongs
*/
public static final String PARAMETER_WORKFLOW_INSTANCE_ID = "system.workflow.instance.id";

/**
* the definition name of the workflow to which current task belongs
*/
public static final String PARAMETER_WORKFLOW_DEFINITION_NAME = "system.workflow.definition.name";

/**
* the name of the project to which current task belongs
*/
public static final String PARAMETER_PROJECT_NAME = "system.project.name";
/**
* month_begin
*/
Expand Down

0 comments on commit 9cd3118

Please sign in to comment.