Shared CloudCode Tasks

Overview

A shared CloudCode task can be created to manage files that need to be shared with other CloudCode tasks. This can be used to share configuration or common functions across multiple CloudCode tasks within the same app.

Version Compatibility

Compatible with version 1.4.2 and later of CloudCode.

A "Shared" task template has been added to assist in creating a task which can be accessed from all other tasks.

Simply select this template when creating a new CloudCode task. A shared task is required to be called "shared" and have the following package.json:

{
    "name": "shared",
    "cloudcode": {
        "shared": true,
        "enabled": true
    }
}

The shared task cannot be deployed or executed directly but files in the task can be included from other CloudCode tasks.

Example

Given a shared task with the following files:

module.exports = {
    // put method and values to share here
    secret: 'foo',
    add: function add(a, b) {
        return a + b;
    }
};
shared/config.json
{
    "secret": "bar"
}
shared/text.yml
secret: "baz"

The files in the shared task can then be loaded from other tasks, for example:

const fs = require('fs');
const path = require('path');
const shared = require('../shared/index');
const sharedConfig = require('../shared/config.json');
const sharedText = fs.readFileSync(path.join(__dirname, '../shared/text.yml'), 'UTF-8');

export async function run() {
    console.log('shared:', shared);
    console.log('sharedConfig:', sharedConfig);
    console.log('sharedText:', sharedText);
    console.log('shared.add(1,2):', shared.add(1,2));
}

The output of the task will be as follows:

14:27:25.468 [INTERNAL:INFO] Created invocation 'T20180921122725LA5kMEtGoFVajZaKkKuTG6'...
14:27:25.474 [QUEUE:INFO] Moved from state 'CREATED' to 'QUEUED'
14:27:25.510 [QUEUE:INFO] Moved from state 'QUEUED' to 'STARTED'
14:27:27.786 [TASK:INFO] Start 'EDITOR_TEST' request. Process trace ID: 'P20180921122727PBwTsW97AVYn8ks5ZfyEZY'
14:27:27.791 [TASK:INFO] shared: { secret: 'foo' }
14:27:27.791 [TASK:INFO] sharedConfig: { secret: 'bar' }
14:27:27.791 [TASK:INFO] sharedText: secret: "baz"
             
14:27:27.792 [TASK:INFO] Request complete. Response code: 204. Memory used: 48MB.
14:27:29.101 [QUEUE:INFO] Moved from state 'STARTED' to 'COMPLETED'

Last updated