github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/runtime/runtime.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 runtime
    19  
    20  import (
    21  	"github.com/aacfactory/fns/barriers"
    22  	"github.com/aacfactory/fns/commons/switchs"
    23  	"github.com/aacfactory/fns/commons/versions"
    24  	"github.com/aacfactory/fns/context"
    25  	fLog "github.com/aacfactory/fns/logs"
    26  	"github.com/aacfactory/fns/services"
    27  	"github.com/aacfactory/fns/shareds"
    28  	"github.com/aacfactory/logs"
    29  	"github.com/aacfactory/workers"
    30  )
    31  
    32  func New(id string, name string, version versions.Version, status *switchs.Switch, log logs.Logger, worker workers.Workers, endpoints services.Endpoints, barrier barriers.Barrier, shared shareds.Shared) *Runtime {
    33  	return &Runtime{
    34  		appId:      []byte(id),
    35  		appName:    name,
    36  		appVersion: version,
    37  		status:     status,
    38  		log:        log,
    39  		worker:     worker,
    40  		endpoints:  endpoints,
    41  		barrier:    barrier,
    42  		shared:     shared,
    43  	}
    44  }
    45  
    46  type Runtime struct {
    47  	appId      []byte
    48  	appName    string
    49  	appVersion versions.Version
    50  	status     *switchs.Switch
    51  	log        logs.Logger
    52  	worker     workers.Workers
    53  	endpoints  services.Endpoints
    54  	barrier    barriers.Barrier
    55  	shared     shareds.Shared
    56  }
    57  
    58  func (rt *Runtime) AppId() []byte {
    59  	return rt.appId
    60  }
    61  
    62  func (rt *Runtime) AppName() string {
    63  	return rt.appName
    64  }
    65  
    66  func (rt *Runtime) AppVersion() versions.Version {
    67  	return rt.appVersion
    68  }
    69  
    70  func (rt *Runtime) Running() (running bool, serving bool) {
    71  	return rt.status.IsOn()
    72  }
    73  
    74  func (rt *Runtime) RootLog() logs.Logger {
    75  	return rt.log
    76  }
    77  
    78  func (rt *Runtime) Workers() workers.Workers {
    79  	return rt.worker
    80  }
    81  
    82  func (rt *Runtime) Endpoints() services.Endpoints {
    83  	return rt.endpoints
    84  }
    85  
    86  func (rt *Runtime) Barrier() barriers.Barrier {
    87  	return rt.barrier
    88  }
    89  
    90  func (rt *Runtime) Shared() shareds.Shared {
    91  	return rt.shared
    92  }
    93  
    94  func (rt *Runtime) TryExecute(ctx context.Context, task workers.Task) bool {
    95  	name := ""
    96  	named, ok := task.(workers.NamedTask)
    97  	if ok {
    98  		name = named.Name()
    99  	} else {
   100  		name = "[task]"
   101  	}
   102  	fLog.With(ctx, rt.log.With("task", name))
   103  	With(ctx, rt)
   104  	return rt.worker.Dispatch(ctx, task)
   105  }
   106  
   107  func (rt *Runtime) Execute(ctx context.Context, task workers.Task) {
   108  	name := ""
   109  	named, ok := task.(workers.NamedTask)
   110  	if ok {
   111  		name = named.Name()
   112  	} else {
   113  		name = "[task]"
   114  	}
   115  	fLog.With(ctx, rt.log.With("task", name))
   116  	With(ctx, rt)
   117  	rt.worker.MustDispatch(ctx, task)
   118  	return
   119  }