# Accessing activities last attempt within activity runtime

**URL:** <https://community.temporal.io/t/accessing-activities-last-attempt-within-activity-runtime/5359>\
**Category:** Community Support\
**Tags:** go-sdk, activity\
**Created:** [July 17, 2022, 3:55pm UTC](https://community.temporal.io/t/accessing-activities-last-attempt-within-activity-runtime/5359 "2022-07-17T15:55:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nitesh237](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/nitesh237/32/1656_2.png) [@nitesh237](https://community.temporal.io/u/nitesh237)\
**Post date:** [July 17, 2022, 3:55pm UTC](https://community.temporal.io/t/accessing-activities-last-attempt-within-activity-runtime/5359/1 "2022-07-17T15:55:05Z")

</div>

Is there a way to access max attempt count for an activity execution ?  
Only attempt is accessible from the activity info.

```auto
ActivityInfo struct {
		TaskToken []byte
		WorkflowType *WorkflowType
		WorkflowNamespace string
		WorkflowExecution WorkflowExecution
		ActivityID string
		ActivityType ActivityType
		TaskQueue string
		HeartbeatTimeout time.Duration // Maximum time between heartbeats. 0 means no heartbeat needed.
		ScheduledTime time.Time // Time of activity scheduled by a workflow
		StartedTime time.Time // Time of activity start
		Deadline time.Time // Time of activity timeout
		Attempt int32 // Attempt starts from 1, and increased by 1 for every retry if retry policy is specified.
	}

```

Now, for few of our use-cases we have to perform some business logic in activities based on whether it is a `LastAttempt` for the activity or not. Now, since, the sdk doesn’t expose max attempt for the activity, the only option that remains available is to use `DescribeWorkflow` API and extract last attempt from `PendingAcitvityInfo` present in the response.

```auto
type PendingActivityInfo struct {
	ActivityId string `protobuf:"bytes,1,opt,name=activity_id,json=activityId,proto3" json:"activity_id,omitempty"`
	ActivityType *v1.ActivityType `protobuf:"bytes,2,opt,name=activity_type,json=activityType,proto3" json:"activity_type,omitempty"`
	State v11.PendingActivityState `protobuf:"varint,3,opt,name=state,proto3,enum=temporal.api.enums.v1.PendingActivityState" json:"state,omitempty"`
	HeartbeatDetails *v1.Payloads `protobuf:"bytes,4,opt,name=heartbeat_details,json=heartbeatDetails,proto3" json:"heartbeat_details,omitempty"`
	LastHeartbeatTime *time.Time `protobuf:"bytes,5,opt,name=last_heartbeat_time,json=lastHeartbeatTime,proto3,stdtime" json:"last_heartbeat_time,omitempty"`
	LastStartedTime *time.Time `protobuf:"bytes,6,opt,name=last_started_time,json=lastStartedTime,proto3,stdtime" json:"last_started_time,omitempty"`
	Attempt int32 `protobuf:"varint,7,opt,name=attempt,proto3" json:"attempt,omitempty"`
	MaximumAttempts int32 `protobuf:"varint,8,opt,name=maximum_attempts,json=maximumAttempts,proto3" json:"maximum_attempts,omitempty"`
	ScheduledTime *time.Time `protobuf:"bytes,9,opt,name=scheduled_time,json=scheduledTime,proto3,stdtime" json:"scheduled_time,omitempty"`
	ExpirationTime *time.Time `protobuf:"bytes,10,opt,name=expiration_time,json=expirationTime,proto3,stdtime" json:"expiration_time,omitempty"`
	LastFailure *v13.Failure `protobuf:"bytes,11,opt,name=last_failure,json=lastFailure,proto3" json:"last_failure,omitempty"`
	LastWorkerIdentity string `protobuf:"bytes,12,opt,name=last_worker_identity,json=lastWorkerIdentity,proto3" json:"last_worker_identity,omitempty"`
}

```

However, I am not sure if this is the best way.

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [July 17, 2022, 9:26pm UTC](https://community.temporal.io/t/accessing-activities-last-attempt-within-activity-runtime/5359/2 "2022-07-17T21:26:29Z")

</div>

> Now, for few of our use-cases we have to perform some business logic in activities based on whether it is a `LastAttempt` for the activity or not

Imo would be maybe better to separate this logic into catching the ActivityFailure when retries are exhausted and call activities that would “compensate” this failure.

---

<div class="post-metadata">

**Author:** ![nitesh237](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/nitesh237/32/1656_2.png) [@nitesh237](https://community.temporal.io/u/nitesh237)\
**Post date:** [July 18, 2022, 4:23am UTC](https://community.temporal.io/t/accessing-activities-last-attempt-within-activity-runtime/5359/3 "2022-07-18T04:23:21Z")

</div>

Agree. That would be better @tihomir . However, we have some legacy code built on our internal orchestrator. Hence, to maintain backward compatibility for few older use cases, we need to handle lastAttempt.

---

<div class="post-metadata">

**Author:** ![nitesh237](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/nitesh237/32/1656_2.png) [@nitesh237](https://community.temporal.io/u/nitesh237)\
**Post date:** [July 18, 2022, 8:56pm UTC](https://community.temporal.io/t/accessing-activities-last-attempt-within-activity-runtime/5359/4 "2022-07-18T20:56:03Z")

</div>

@tihomir any better recommendation here ? Also, assuming it should be ok to use `DescribeWorkflow` within activity execution ?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [July 18, 2022, 9:04pm UTC](https://community.temporal.io/t/accessing-activities-last-attempt-within-activity-runtime/5359/5 "2022-07-18T21:04:02Z")

</div>

> Agree. That would be better @tihomir . However, we have some legacy code built on our internal orchestrator. Hence, to maintain backward compatibility for few older use cases, we need to handle lastAttempt.

You can still execute the same activity from the workflow with a different argument like “lastAttempt=true”. If you cannot change the signature you can pass this argument using a ContextPropagator.

---

<div class="post-metadata">

**Author:** ![nitesh237](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/nitesh237/32/1656_2.png) [@nitesh237](https://community.temporal.io/u/nitesh237)\
**Post date:** [July 19, 2022, 7:41am UTC](https://community.temporal.io/t/accessing-activities-last-attempt-within-activity-runtime/5359/6 "2022-07-19T07:41:34Z")

</div>

I guess this might work as a temporary hack… Thanks @maxim.
