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

Return HTTP OK Response after user logic execution #46

Merged
merged 1 commit into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
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
34 changes: 34 additions & 0 deletions samples/listener/onMessage_HeavyProcessing.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. 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.

import ballerinax/slack.'listener as slack;

slack:ListenerConfiguration configuration = {
port: 9090,
verificationToken: "VERIFICATION_TOKEN"
};

listener slack:Listener slackListener = new (configuration);

service /slack on slackListener {
remote function onMessage(slack:MessageEvent eventInfo) returns error? {
_ = @strand { thread: "any" } start userLogic(eventInfo);
}
}

function userLogic(slack:MessageEvent eventInfo) returns error? {
// Write your logic here
}
23 changes: 23 additions & 0 deletions slack/modules/listener/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,28 @@ service /slack on slackListener {
}
}
```
> **NOTE:**
If the user's logic inside any remote method of the connector listener throws an error, connector internal logic will
covert that error into a HTTP 500 error response and respond to the webhook (so that event may get redelivered),
otherwise it will respond with HTTP 200 OK. Due to this architecture, if the user logic in listener remote operations
includes heavy processing, the user may face HTTP timeout issues for webhook responses. In such cases, it is advised to
process events asynchronously as shown below.

```ballerina
import ballerinax/slack.'listener as slack;
slack:ListenerConfiguration configuration = {
port: 9090,
verificationToken: "VERIFICATION_TOKEN"
};
listener slack:Listener slackListener = new (configuration);
service /slack on slackListener {
remote function onMessage(slack:MessageEvent eventInfo) returns error? {
_ = @strand { thread: "any" } start userLogic(eventInfo);
}
}
function userLogic(slack:MessageEvent eventInfo) returns error? {
// Write your logic here
}
```

## Please check the [Samples directory](https://github.com/ballerina-platform/module-ballerinax-slack/tree/master/samples) for more examples.
2 changes: 1 addition & 1 deletion slack/modules/listener/http_service.bal
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ service class HttpService {
} else if (eventOrVerification == EVENT_CALLBACK) {
http:Response response = new;
response.statusCode = http:STATUS_OK;
check caller->respond(response);

json eventTypeJson = check payload.event.'type;
string eventType = eventTypeJson.toString();
Expand Down Expand Up @@ -118,6 +117,7 @@ service class HttpService {
TeamJoinEvent slackEvent = check payload.cloneWithType(TeamJoinEvent);
check self.handleTeamEvents(eventType, slackEvent);
}
check caller->respond(response);
} else {
return error("Unidentified Request Type");
}
Expand Down