Access task configuration from an event listener

Matt Curtis April 21, 2013

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
Przemek Bruski
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 21, 2013

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 ;-)

Matt Curtis April 21, 2013

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.

Przemek Bruski
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 21, 2013

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

Matt Curtis April 21, 2013

That's awesome, thanks!

Matt Curtis April 22, 2013

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?

Matt Curtis April 24, 2013

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...

TAGS
AUG Leaders

Atlassian Community Events