ruanwenjun commented on code in PR #17819:
URL:
https://github.com/apache/dolphinscheduler/pull/17819#discussion_r2650122024
##########
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java:
##########
@@ -192,9 +192,10 @@ public void sendServerStoppedAlert(String host, String
serverType) {
* workflow time out alert
*
* @param workflowInstance workflowInstance
- * @param projectUser projectUser
+ * @param projectUser projectUser
+ * @param modifyBy modifyBy
*/
- public void sendWorkflowTimeoutAlert(WorkflowInstance workflowInstance,
ProjectUser projectUser) {
+ public void sendWorkflowTimeoutAlert(WorkflowInstance workflowInstance,
ProjectUser projectUser, String modifyBy) {
Review Comment:
Please don't change this, this PR should only fix the alert bug.
##########
dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/engine/workflow/lifecycle/handler/WorkflowTimeoutLifecycleEventHandler.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.dolphinscheduler.server.master.engine.workflow.lifecycle.handler;
+
+import org.apache.dolphinscheduler.dao.entity.WorkflowInstance;
+import org.apache.dolphinscheduler.server.master.engine.ILifecycleEventType;
+import
org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.WorkflowLifecycleEventType;
+import
org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.event.WorkflowTimeoutLifecycleEvent;
+import
org.apache.dolphinscheduler.server.master.engine.workflow.runnable.IWorkflowExecutionRunnable;
+import
org.apache.dolphinscheduler.server.master.engine.workflow.statemachine.IWorkflowStateAction;
+import org.apache.dolphinscheduler.service.alert.WorkflowAlertManager;
+
+import lombok.extern.slf4j.Slf4j;
+
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@Component
+public class WorkflowTimeoutLifecycleEventHandler
+ extends
+
AbstractWorkflowLifecycleEventHandler<WorkflowTimeoutLifecycleEvent> {
+
+ private final WorkflowAlertManager workflowAlertManager;
+
+ public WorkflowTimeoutLifecycleEventHandler(final WorkflowAlertManager
workflowAlertManager) {
+ this.workflowAlertManager = workflowAlertManager;
+ }
+
+ @Override
+ public void handle(final IWorkflowStateAction workflowStateAction,
+ final IWorkflowExecutionRunnable
workflowExecutionRunnable,
+ final WorkflowTimeoutLifecycleEvent
workflowTimeoutEvent) {
+ final WorkflowInstance workflowInstance =
workflowExecutionRunnable.getWorkflowInstance();
+ final String workflowName = workflowExecutionRunnable.getName();
+
+ // Check if workflow is still active (not finished)
+ if (workflowInstance.getState().isFinalState()) {
+ log.info("The workflow {} has been finished with state: {}, skip
timeout alert.",
+ workflowName,
+ workflowInstance.getState().name());
+ return;
+ }
+
+ log.info("The workflow {} has timeout, try to send a timeout alert.",
workflowName);
+ doWorkflowTimeoutAlert(workflowInstance);
+ }
+
+ private void doWorkflowTimeoutAlert(final WorkflowInstance
workflowInstance) {
+ // ProjectUser will be built in WorkflowAlertManager
+ workflowAlertManager.sendWorkflowTimeoutAlert(workflowInstance, null);
Review Comment:
Should fix like #17818, otherwise will throw NPE
##########
dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/engine/workflow/lifecycle/event/WorkflowTimeoutLifecycleEvent.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.dolphinscheduler.server.master.engine.workflow.lifecycle.event;
+
+import static com.google.common.base.Preconditions.checkState;
+
+import org.apache.dolphinscheduler.dao.entity.WorkflowInstance;
+import org.apache.dolphinscheduler.server.master.engine.ILifecycleEventType;
+import
org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.AbstractWorkflowLifecycleLifecycleEvent;
+import
org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.WorkflowLifecycleEventType;
+import
org.apache.dolphinscheduler.server.master.engine.workflow.runnable.IWorkflowExecutionRunnable;
+
+import java.util.concurrent.TimeUnit;
+
+public class WorkflowTimeoutLifecycleEvent extends
AbstractWorkflowLifecycleLifecycleEvent {
+
+ private final IWorkflowExecutionRunnable workflowExecutionRunnable;
+
+ protected WorkflowTimeoutLifecycleEvent(final IWorkflowExecutionRunnable
workflowExecutionRunnable,
+ final long timeout) {
+ super(timeout);
+ this.workflowExecutionRunnable = workflowExecutionRunnable;
+ }
+
+ @Override
+ public IWorkflowExecutionRunnable getWorkflowExecutionRunnable() {
+ return workflowExecutionRunnable;
+ }
+
+ public static WorkflowTimeoutLifecycleEvent of(final
IWorkflowExecutionRunnable workflowExecutionRunnable) {
+ final WorkflowInstance workflowInstance =
workflowExecutionRunnable.getWorkflowInstance();
+ checkState(workflowInstance != null, "The workflow instance must be
initialized before timeout monitoring.");
+
+ final int timeout = workflowInstance.getTimeout();
+ checkState(timeout >= 0, "The workflow timeout: %s must >=0 minutes",
timeout);
+
+ // Calculate remaining time until timeout: timeout - elapsed time
+ long delayTime = TimeUnit.MINUTES.toMillis(timeout)
+ - (System.currentTimeMillis() -
workflowInstance.getStartTime().getTime());
+ // Ensure delayTime is not negative (trigger immediately if already
timeout)
Review Comment:
I am not clear in which case the `delayTime` might be negative, since
`System.currentTimeMillis() - workflowInstance.getStartTime().getTime()` should
always > 0.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]