StateManager

JFrog Platform Administration Documentation

Content Type
Administration / Platform

This interface allows you to set or modify a Worker state at runtime, which persists between Worker runs. For example:

export default async (context: PlatformContext): Promise<any> => {
  context.state.has('foo'); // false
  context.state.set('foo', 'bar');
  context.state.has('foo'); // true

  return context.state.getAll(); // { "foo": "bar" }
};

The state is stored as a string: to safely retrieve a number, use the following syntax:

const numberStr = context.state.get('number');
const number = Number.parseInt(numberStr) || 0;

This code will try to get the property ‘number’ from the state and convert it to a number. If it doesn't exist or cannot be parsed, the code returns 0.

StateManager Limitations

  • The state tool does not support concurrent executions of the same Worker.

  • In case of execution failure, the state will not be saved.

  • A Worker state can hold a maximum of 10 items by default: Self-Managed users can change this value in Workers YAML Configuration.Worker YAML Configuration

  • A state item key-value pair cannot exceed 40 characters for the key and 250 characters for the value.

interface StateManager {
    /**
     * Retrieves a value from the state by its key
     * @param {string} key - The key of the value to retrieve
     */
    get(key: string): string;
    /**
     * Sets a value in the state by its key
     * @param {string} key - The key of the value to set
     * @param {any} value - The value to set. If the value is not a string, it will be converted to a JSON string.
     */
    set(key: string, value: any): void;
    /**
     * Checks if a value exists in the state by its key
     * @param {string} key - The key of the value to check
     * @returns {boolean} - True if the value exists, false otherwise
     */
    has(key: string): boolean;
    /**
     * Deletes a value from the state by its key.
     * If it does not exist, it does nothing.
     * @param {string} key - The key of the value to delete
     */
    delete(key: string): void;
    /**
     * Clears the entire state
     */
    clear(): void;
    /**
     * Returns a copy of the entire state
     */
    getAll(): Record<string, string>;
}