Is there a way to check if an instance of a post-function is already running?

j.hoffmann February 19, 2021

I would like to prevent simultaneous execution of a particular post-function.

To put it simply:

  • User #1 presses a particular workflow button -> a post-function is executed.
  • While the post-function is still running user #2 presses the same workflow button -> Post-Function detects, that user #1 already executed the transition (to itself) and that the script is still running -> error message is returned.

Edit:

Extending the question: 
Would detecting this still be possible if I started a Thread inside the Post-Function, so that the main thread of the Post-Function finishes, while the Thread is still running in the background?

1 answer

1 accepted

0 votes
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 19, 2021

You can't actually perform two post-functions at the same time.  Imagine the situation with two users looking at the same issue.

User 1 clicks "start progress", then user 2 hits the same a nanosecond later.  The transition process that the first one is performing has moved the issue into a state (even if it's a status looped back to itself) where the second call the transition might be invalid, so it won't actually do anything.  User 2 will get a message about the the state of the issue having being changed, so the transition they've requested is no longer valid.  As the first transition blocks any others, you can't get into a state where the same post-function is running twice

Obviously, that only applies for the one issue.  If your two users are looking at different issues, yes, the same post-function could run at the same time, but it will be running against two different issues, so they won't affect each other.

j.hoffmann February 22, 2021

That makes a lot of sense. Thanks.

I assume if I chose to create a new thread from inside that post-function to do some task (so the user doesn't have to wait), then I wouldn't be able to prevent simultaneous executions of those threaded jobs, correct, unless maybe I have a "boolean" field "allow execution" on the context issue, set it to false in the main post-function, then set it to true again once the threaded task is finished?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 22, 2021

Yes, that's the only way to do it, but you'll need to shift the logic a bit.  You can't do this detection stuff in the post function, you'll need to do it in the conditions on the transition, basically "don't allow the transition to start if the flagged-as-running field is set"

I also want to question what you're doing.  A post-function is only really suitable for processes that are single purpose, short and usually directly related to the issue being transitioned.

A long running process probably should be being run by something built to handle long running processes, and as you seem to want to prevent more than one of them running, we can be pretty sure it's not related to Jira.  It sounds like your transition should not be running the process itself, but passing a trigger to a build server so that can handle all the work properly.

j.hoffmann February 22, 2021

That's good advice. Thanks.

I wrote a script to export data to Google Sheets in a very specific format, while also doing some calculations before sending the data. I also couldn't do it via scheduled job, since one requirement was for it to be executed on demand. It works well, but I just wanted to make sure, that there can only ever be a single export script running at the same time, as anything else would just be bogging down the instance with redundant JQL searches etc.

We have an extremely customized instance here, and I agree, that  passing a trigger to a build server would be best. It's a work-in-progress.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events