Send heartbeat messages to Simple Log Service from a consumer

Updated at:

If a consumer group does not receive a heartbeat within the timeout period, the consumer is removed. Call the ConsumerGroupHeartBeat API to send a heartbeat and keep the consumer active.

Prerequisites

Parameter description

def heart_beat(self, project, logstore, consumer_group, consumer, shards=None):

Request parameters

Parameter

Type

Required

Description

project

String

Yes

The name of the project.

logstore

String

Yes

The name of the logstore.

consumer_group

String

Yes

The name of the consumer group, which is unique in the logstore.

consumer

String

No

The name of the consumer.

shards

List

No

The shard IDs for which the consumer maintains a heartbeat. For example, you can specify shard 0 and shard 1.

Response parameters

For information about the response parameters, see ConsumerGroupHeartBeat.

Sample code

from aliyun.log import LogClient
import os

# In this example, the AccessKey ID and AccessKey Secret are obtained from environment variables.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')

# Specify a Simple Log Service endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"

client = LogClient(endpoint, access_key_id, access_key_secret)

# Project name
project = "project-1"

# Logstore name
logstore = "logstore-1"

# consumer group name
consumer_group = "consumer_group_test"

# List of shard IDs
shards = [0, 1]

# Consumer name
consumer = ''

try:
    # Create a consumer group for the specified project.
    response = client.heart_beat(project, logstore, consumer_group, consumer, shards=None)
    response.log_print()
except Exception as e:
    print(f"An error occurred while creating the consumer group: {e}")

Sample response

ListHeartBeat:
headers: {'Server': 'AliyunSLS', 'Content-Type': 'application/json', 'Content-Length': '5', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Fri, 25 Oct 2024 02:46:56 GMT', 'x-log-time': '1729824416', 'x-log-requestid': '671B06A0EE24B0B90621558E'}
shards: [0, 1]

References