github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/services/task.go (about) 1 /* 2 * Copyright 2023 Wang Min Xiang 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 package services 19 20 import ( 21 sc "context" 22 "github.com/aacfactory/errors" 23 "github.com/aacfactory/fns/commons/bytex" 24 "github.com/aacfactory/fns/commons/futures" 25 "github.com/aacfactory/fns/context" 26 "github.com/aacfactory/fns/services/tracings" 27 ) 28 29 type FnTask struct { 30 Fn Fn 31 Promise futures.Promise 32 } 33 34 func (task FnTask) Execute(ctx sc.Context) { 35 r := LoadRequest(context.Wrap(ctx)) 36 // tracing 37 trace, hasTrace := tracings.Load(r) 38 if hasTrace { 39 trace.Waited() 40 } 41 v, err := task.Fn.Handle(r) 42 if err != nil { 43 ep, fn := r.Fn() 44 codeErr := errors.Wrap(err).WithMeta("endpoint", bytex.ToString(ep)).WithMeta("fn", bytex.ToString(fn)) 45 if hasTrace { 46 trace.Finish("succeed", "false", "cause", codeErr.Name()) 47 } 48 task.Promise.Failed(codeErr) 49 } else { 50 if hasTrace { 51 trace.Finish("succeed", "true") 52 } 53 task.Promise.Succeed(NewResponse(v)) 54 } 55 }