It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Access task configuration from an event listener

Hi,

I've got a custom task module with a UI form that saves user selected information in the taskContext.getConfigurationMap().

When that task is included in a stage/job I would like my event listener to read that user selected value and send out an appropriate notification but I can't work out where to find it.

In the task's execute method the value can be read with:

String userSelectedNotification = taskContext.getConfigurationMap().get("selectedNotification");

I'm not too worried about which event I have to listen for ( JobCompletedEvent, StageCompletedEvent and others all look like good candidates) I just need that information to be available.

I'm sure it must be possible but I can't work out which of the maps I should be reading from. If it helps I can persist the value in a different map (there seem to be a few config maps) within the task's execute() method.

Thanks in advance! :)

[Using Bamboo 4.4.3]

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted

Hmm, you could try injecting eventPublisher and doing com.atlassian.event.api.EventPublisher#register on a listener instance. Annotate some methods with @EventListener and they should receive the events.

This is a bit hacky though and I am not sure if it will work (especially on remote agents). But there's some chance it will work. We didn't really mean for the tasks to receive events ;-)

Thanks for the quick reply Przemek!

You lost me there a little bit though (can't get my head around a task listening on an event!) :)

I don't think I need my task to receive an event (unless you're saying that's the workaround).

I have a string that was set by my TaskConfigurator which is available during my task's execution (through the taskContext). At the end of the job or stage that the task was a part of I would like a notification to be sent out where the listener uses that string to determine which NotificationType to send out.

So I was hoping there was some map/context/config/result object that could be written to by the task and read by the listener receiving the stage completed event so the listener knows how that task was configured.

Aaaah. So you need to access the task configuration from your listener, right?

The listener should have the PlanResultKey (or PlanKey). Use it with com.atlassian.bamboo.plan.cache.CachedPlanManagerImpl#getPlanByKey to get the Plan reference.

Then, use plan.getBuildDefinition().getTaskDefinitions() to get definitons of all tasks for that job. Find your task. You can access the configuration using com.atlassian.bamboo.task.TaskDefinition#getConfiguration

That's awesome, thanks!

Hit another road block with this...

StageCompletedEvent can't read the task configuration (presumably because tasks belong to a job) but JobCompletedEvent can.

JobCompletedEvent can't get the notificationRules (I get the net.sf.hibernate.LazyInitializationException: Exception initializing proxy - the owning Session was closed error message when calling NotificationManager#getNotificationRules) even with Brydie's fix using a transactionTemplate I get that exception.

Not sure why it works for StageCompleted and not the other event, I guess there are session problems because there could be multiple parallel jobs running.

So I need a listener that has access to both the notification rules and the task config. Perhaps a different event or I listen for the job event and persist the value in a way that StageComplete will receive?

Hi Przemek, I've moved that last comment into its own question here. Any chance you could take a look when you get some time please :) I've run out of ideas and still can't get it to work...

Community showcase
Posted in Off-topic

Do you still play the same games you played when you were a kid?

Months ago, I was walking in a huge grocery store and I went to the toys aisle and found a jigsaw puzzle that was more than 50% off, and I thought "why not? It will be fun and it can be a beautiful f...

192 views 20 8
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you