How can we test Lifecycle events?

Hi,

we want to start using the new avi:forge:installed:app and avi:forge:upgraded:app lifecycle events, but we are not sure on how to test these. Our manifest contains:

  trigger:
    - key: lifecycle-trigger
      function: lifecycle-trigger-func
      events:
        - avi:forge:installed:app
          avi:forge:upgraded:app
  function:
    - key: lifecycle-trigger-func
      handler: lifecycle.postInstallOrUpdateHandler

and we want to check that the function is called. We thought in adding a value to storage (Storage API) when the function is invoked, and then read that value out from an admin page in the app.

This is our lifecycle file:

import { storage } from '@forge/api';

export const postInstallOrUpdateHandler = async (
  event: object,
  context: object
) => {
  console.log('event: ' + JSON.stringify(event));
  console.log('context: ' + JSON.stringify(context));

  await storage.set('TestEvent', 'hello');

  return true;
};

We uninstall and install the app manually before every test, but we get undefined when reading the TestEvent value from storage from the app. Also we cant see the console.log lines anywhere, I guess because the tunnel is not running during the install process.

What is the best way for us to check that the post-install and post-upgrade events are working?

Thanks,

2 Likes

Hi Eulogio,

I think you might just be missing a hyphen “-” before avi:forge:upgraded:app in your manifest file.

I copied your code into a sample app and had the same problem but (after much head-scratching) added the missing hyphen and it worked fine.

So your manifest should be:

  trigger:
    - key: lifecycle-trigger
      function: lifecycle-trigger-func
      events:
        - avi:forge:installed:app
        - avi:forge:upgraded:app
  function:
    - key: lifecycle-trigger-func
      handler: lifecycle.postInstallOrUpdateHandler
3 Likes

Thanks @AdamMoore - that worked!

Should this not raise a forge linting error?

Yeah fair point, I’ll see what we can do.

1 Like

I am new to the forge platform and trying to store some data for app post installation. But getting an authentication error while accessing storage inside postInstallOrUpdateHandler. am I missing anything?