# Bug: Precomputation Cache has multiple entries when switching environment

**URL:** <https://community.developer.atlassian.com/t/bug-precomputation-cache-has-multiple-entries-when-switching-environment/81574>\
**Category:** Forge\
**Tags:** rest-api-v3, jql-function, forge\
**Created:** [July 4, 2024, 10:05am UTC](https://community.developer.atlassian.com/t/bug-precomputation-cache-has-multiple-entries-when-switching-environment/81574 "2024-07-04T10:05:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsaran](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vsaran](https://community.developer.atlassian.com/u/vsaran)\
**Post date:** [July 4, 2024, 10:05am UTC](https://community.developer.atlassian.com/t/bug-precomputation-cache-has-multiple-entries-when-switching-environment/81574/1 "2024-07-04T10:05:13Z")

</div>

Hey folks,

I’m writing a plugin function for JQL. I have been using the [subtaskOf example](https://bitbucket.org/atlassian/forge-jql-function-subtaskof/src/master/) that is around.

I got it working in my dev environment, so then I installed it as staging on the proper work jira instance.  
However, then I noticed something odd, and tried to connect to it with forge tunnel - which notified me it can only tunnel to dev.  
So I removed the staging install and then installed the development version on the same Jira cloud instance.

However, now, there is a lingering precompute which is causing issues.  
So there’s a bug or anomaly with the precompute system when switching an app’s environment.  
See the screenshot attached for the exact details:

 ![image](https://global.discourse-cdn.com/atlassiandeveloper/original/3X/8/1/81ac86c44f5c16845fabf267ea071af6a546e4a3.png)

My precomputationApi.js is very similar to the provided example:

```js
import api, { route } from "@forge/api";

// https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-jql-functions--apps-/

export const fetchPrecomputations = async (orderBy) => {
  return await api
    .asApp()
    .requestJira(route`/rest/api/3/jql/function/computation?orderBy=${orderBy || "UPDATED"}`, {
      headers: {
        Accept: "application/json",
      },
    })
    .then((response) => response.json());
};

// `precomputations` is a list that looks like this: [{ id: "id", "value": "jql" }]
export const updatePrecomputations = async (precomputations, component, operator) => {
  const response = await api
    .asApp()
    .requestJira(route`/rest/api/3/jql/function/computation`, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        "values": precomputations
      }),
    });

  console.log(`Update precomputation ${precomputations.map(p => p.id)} (For (${operator}) Component: '${component}') result: ${response.statusText}`);

  if (!response.ok)
    throw Error(`${response.status}:${response.statusText}`)
};

```

---

<div class="post-metadata">

**Author:** ![vsaran](https://avatars.discourse-cdn.com/v4/letter/v/439d5e/32.png) [@vsaran](https://community.developer.atlassian.com/u/vsaran)\
**Post date:** [July 4, 2024, 10:19am UTC](https://community.developer.atlassian.com/t/bug-precomputation-cache-has-multiple-entries-when-switching-environment/81574/2 "2024-07-04T10:19:06Z")

</div>

The bug appears to be that the GET and the POST have a difference of opinion. I suspect the GET should NOT return the first id seen in the screenshot above.

Updating the function to this is a workaround:

```js
async function refreshMostStalePrecomputation() {

  var precomputations = await fetchPrecomputations();
  
  const oldestPrecomp = precomputations.values[0]
  const operator = oldestPrecomp.operator;
  const component = oldestPrecomp.arguments[0];

  // There's a bug for multiple ids mapped to the same operator/component
  // This is a workaround
  var idsToUpdate = []
  for (const precomp of precomputations.values) {
    if (precomp.operator == operator && precomp.arguments[0] == component) {
      idsToUpdate.push(precomp.id);
    }
  }
  console.log(`Refreshing precomputation(s) ${idsToUpdate.toString()} (For (${operator}) Component: '${component}')`)

  const functionResult = await epicHasComponentImpl(component, operator);

  if (functionResult.jql) {
    var errors = []
    for (const id of idsToUpdate) {
      const updatedPrecomputation = {
        id: id,
        value: functionResult.jql
      };

      try {
        await updatePrecomputations([updatedPrecomputation], component, operator);
      }
      catch (ex) {
        errors.push(ex.message);
      }
    }
    if (errors.length == idsToUpdate.length)
    {
      console.error(`Failed to update precomputation, POST(s) failed with ${errors.toString()}`)
    }
  } else {
    console.error(`Failed to update precomputation(s) ${idsToUpdate.toString()} for ${component}: ${functionResult.error}`);
  }
}

```

---

<div class="post-metadata">

**Author:** ![ljarzabek](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/ljarzabek/32/49732_2.png) [@ljarzabek](https://community.developer.atlassian.com/u/ljarzabek)\
**Post date:** [July 4, 2024, 12:05pm UTC](https://community.developer.atlassian.com/t/bug-precomputation-cache-has-multiple-entries-when-switching-environment/81574/3 "2024-07-04T12:05:50Z")

</div>

Hi @vsaran, thank you for reaching out!

We will take a look and come back to you. Sorry for the inconvenience.

---

<div class="post-metadata">

**Author:** ![ljarzabek](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/ljarzabek/32/49732_2.png) [@ljarzabek](https://community.developer.atlassian.com/u/ljarzabek)\
**Post date:** [July 19, 2024, 7:28am UTC](https://community.developer.atlassian.com/t/bug-precomputation-cache-has-multiple-entries-when-switching-environment/81574/4 "2024-07-19T07:28:38Z")

</div>

Hi @vsaran, thank you for your patience.  
We have rolled out the fix, so you no longer should experience this problem.
