Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(app-sdk): Add background publisher API detail #229

Merged
merged 2 commits into from
Oct 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion docs_src/microservices/application/AdvancedTopics.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,61 @@ If in secure mode, the secrets are retrieved from the secret store based on the

If running in insecure mode, the secrets are retrieved from the *Writable.InsecureSecrets* configuration.

### Background Publishing

Application Services using the MessageBus trigger can request a background publisher using the AddBackgroundPublisher API in the SDK. This method takes an int representing the background channel's capacity as the only parameter and returns a reference to a BackgroundPublisher. This reference can then be used by background processes to publish to the configured MessageBus output. Example usage is as follows:

```go

func runJob (pub appsdk.BackgroundPublisher, done chan struct{}){
ticker := time.NewTicker(1 * time.Minute)
go func() {
for {
select {
case <-ticker.C:
msg := myDataService.GetMessage()
payload, err := json.Marshal(message)

if err != nil {
continue
}
pub.Publish(payload, uuid.New().String(), clients.ContentTypeJSON)
case <-j.done:
ticker.Stop()
return
}
}
}()
}

func main() {
edgexSdk := &appsdk.AppFunctionsSDK{ServiceKey: serviceKey}
if err := edgexSdk.Initialize(); err != nil {
edgexSdk.LoggingClient.Error(fmt.Sprintf("SDK initialization failed: %v", err))
os.Exit(-1)
}

//initialize background publisher with a channel capacity of 10
pub := sdk.AddBackgroundPublisher(10)

done := make(chan struct{})
defer close(done)

//pass publisher to your background job
runJob(pub, done)

edgexSdk.SetFunctionsPipeline(
All,
My,
Functions,
)

edgexSdk.MakeItRun()

os.Exit(0)
}
```

### Registry Client

**After initialization**, the configured registry client used by the SDK can be retrieved from the sdk instance at .RegistryClient. It is important to note that sdk.RegistryClient may be nil - either if the SDK is not yet initialized, or if the registry option (-r/--registry) is not specified on start. Once retrieved the client can be used to look up host information for other services, or perform other operations supported by the registry.Client type in [go-mod-registry](https://github.com/edgexfoundry/go-mod-registry). For example, to retrieve the URL for a given service:
Expand All @@ -445,4 +500,4 @@ func(sdk *appsdk.AppFunctionsSDK, serviceKey string) (string, error) {
```
!!! note Known Service Keys
Service keys for known EdgeX services can be found under clients in [go-mod-core-contracts](https://github.com/edgexfoundry/go-mod-core-contracts/blob/master/clients/constants.go#L58-L72)