Actor picture

Rebuilder

mnmkng/rebuilder

Rebuild your actors easily with a simple regular expression. This actor will fetch all your existing actors and match their names. Those that pass will be rebuilt. Schedule this for maximum effectiveness. It can also rebuild itself!

No credit card required

Author's avatarOndra Urban
  • Modified
  • Users2
  • Runs980

Dockerfile

FROM apify/actor-node-basic:beta
# Copy source code
COPY . ./
#COPY src ./src
RUN npm --quiet set progress=false \
&& npm install --only=prod --no-optional \
&& echo "Installed NPM packages:" \
&& npm list || true \
&& echo "Node.js version:" \
&& node --version \
&& echo "NPM version:" \
&& npm --version

INPUT_SCHEMA.json

{
    "title": "Rebuilder Input",
    "type": "object",
    "description": "Rebuild your actors easily with a simple regular expression. This actor will fetch all your existing actors and match their names. Those that pass will be rebuilt. Schedule this for maximum effectiveness. It can also rebuild itself!",
    "schemaVersion": 1,
    "properties": {
        "regexString": {
            "title": "Regular Expression",
            "type": "string",
            "description": "Regular expression to match the name of the actors you want to rebuild. It's a string that will be used to construct a RegExp instance, so make sure to use proper escaping!",
            "editor": "textfield"
        },
        "version": {
            "title": "Version",
            "type": "string",
            "description": "Other versions than 0.0 of the actors can be rebuilt if a different version is specified. Unfortunately, using tags is not supported by Apify at the moment.",
            "editor": "textfield",
            "default": "0.0"
        },
        "buildTag": {
            "title": "Build tag",
            "type": "string",
            "description": "Tag to use for all the builds.",
            "editor": "textfield",
            "default": "latest"
        },
        "waitForFinish": {
            "title": "Wait for finish",
            "type": "boolean",
            "description": "Check this to tell the actor to wait for completion of all builds. This is useful when tied with a webhook that would then notify you of errors.",
            "default": false
        }
    },
    "required": ["regexString"]
}

README.md

# Actor - Rebuilder

Rebuild your actors easily with a simple regular expression. 
This actor will fetch all your existing actors and match their names.
Those that pass will be rebuilt. Schedule this for maximum effectiveness.
It can also rebuild itself! Use a webhook to get notified of errors.

main.js

const Apify = require('apify');

const { utils: { log, sleep }, client } = Apify;

Apify.main(async () => {
    log.info('Getting input.');
    const {
        regexString,
        waitForFinish,
        version,
        buildTag
    } = await Apify.getInput();

    log.info('Constructing regex.')
    const regex = new RegExp(regexString, 'i');

    log.info('Getting all user\'s actors.');
    const { items } = await client.acts.listActs();

    log.info('Filtering items that match our regex.');
    const actorsToRebuild = items.filter(({ name }) => regex.test(name));
    const actorCount = actorsToRebuild.length;
    if (actorCount > 50) {
        throw new Error(`Too many actors to rebuild (${actorCount}).`
         + 'Max is 50. Use a more specific regular expression.');
    }

    log.info(`Starting build of ${actorCount} actors.`);
    let failedBuildsCount = 0;
    const promises = actorsToRebuild.map(async ({ id }, idx) => {
        await sleep(idx * 500);
        return client.acts.buildAct({
            actId: id,
            version,
            useCache: false,
            tag: buildTag,
            waitForFinish: waitForFinish ? 120 : 0
        }).catch(err => {
            log.exception(err);
            failedBuildsCount++
        });
        
    });
    await Promise.all(promises);

    log.info(`All builds ${waitForFinish ? 'finished' : 'dispatched'}.`);
    if (failedBuildsCount) {
        throw new Error('Some of the builds were not successful. See log.');
    }
});

package.json

{
    "name": "rebuilder",
    "version": "0.0.1",
    "description": "Actors description",
    "main": "main.js",
    "dependencies": {
        "apify": "beta"
    },
    "scripts": {
        "start": "node main.js"
    },
    "author": "mnmkng"
}