Skip to content

Commit fe453eb

Browse files
lTimejnormal-wls
authored andcommitted
create_and_start_task新增回调功能 #7540
1 parent 67fffe4 commit fe453eb

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

gcloud/apigw/views/create_and_start_task.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
specific language governing permissions and limitations under the License.
1212
"""
1313

14+
import re
15+
1416
import jsonschema
1517
import ujson as json
1618
from apigw_manager.apigw.decorators import apigw_require
@@ -20,30 +22,22 @@
2022

2123
import env
2224
from gcloud import err_code
23-
from gcloud.apigw.decorators import (
24-
mark_request_whether_is_trust,
25-
project_inject,
26-
return_json_response,
27-
)
25+
from gcloud.apigw.decorators import mark_request_whether_is_trust, project_inject, return_json_response
2826
from gcloud.apigw.schemas import APIGW_CREATE_AND_START_TASK_PARAMS
2927
from gcloud.apigw.validators import CreateTaskValidator
3028
from gcloud.apigw.views.utils import logger
3129
from gcloud.common_template.models import CommonTemplate
3230
from gcloud.conf import settings
3331
from gcloud.constants import BUSINESS, COMMON, TaskCreateMethod
34-
from gcloud.contrib.operate_record.constants import (
35-
OperateSource,
36-
OperateType,
37-
RecordType,
38-
)
32+
from gcloud.contrib.operate_record.constants import OperateSource, OperateType, RecordType
3933
from gcloud.contrib.operate_record.decorators import record_operation
4034
from gcloud.core.models import EngineConfig
4135
from gcloud.iam_auth.intercept import iam_intercept
4236
from gcloud.iam_auth.view_interceptors.apigw import CreateTaskInterceptor
4337
from gcloud.taskflow3.celery.tasks import prepare_and_start_task
4438
from gcloud.taskflow3.domains.auto_retry import AutoRetryNodeStrategyCreator
4539
from gcloud.taskflow3.domains.queues import PrepareAndStartTaskQueueResolver
46-
from gcloud.taskflow3.models import TaskFlowInstance
40+
from gcloud.taskflow3.models import TaskCallBackRecord, TaskFlowInstance
4741
from gcloud.tasktmpl3.models import TaskTemplate
4842
from gcloud.utils.decorators import request_validate
4943
from gcloud.utils.throttle import check_task_operation_throttle
@@ -77,6 +71,15 @@ def create_and_start_task(request, template_id, project_id):
7771
)
7872
)
7973

74+
callback_url = params.pop("callback_url", None)
75+
CALLBACK_URL_PATTERN = r"^https?://\w.+$"
76+
if callback_url and not (isinstance(callback_url, str) and re.match(CALLBACK_URL_PATTERN, callback_url)):
77+
return {
78+
"result": False,
79+
"code": err_code.REQUEST_PARAM_INVALID.code,
80+
"message": f"callback_url format error, must match {CALLBACK_URL_PATTERN}",
81+
}
82+
8083
# 根据template_id获取template
8184
if template_source == BUSINESS:
8285
try:
@@ -152,6 +155,11 @@ def create_and_start_task(request, template_id, project_id):
152155
)
153156
except Exception as e:
154157
return {"result": False, "message": str(e), "code": err_code.UNKNOWN_ERROR.code}
158+
159+
# create callback url record
160+
if callback_url:
161+
TaskCallBackRecord.objects.create(task_id=task.id, url=callback_url)
162+
155163
# 开始执行task
156164
queue, routing_key = PrepareAndStartTaskQueueResolver(
157165
settings.API_TASK_QUEUE_NAME_V2

0 commit comments

Comments
 (0)