Rebuilder avatar
Rebuilder
Try for free

No credit card required

View all Actors
Rebuilder

Rebuilder

mnmkng/rebuilder
Try for free

No credit card required

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!

Dockerfile

1FROM apify/actor-node-basic:beta
2# Copy source code
3COPY . ./
4#COPY src ./src
5RUN npm --quiet set progress=false \
6&& npm install --only=prod --no-optional \
7&& echo "Installed NPM packages:" \
8&& npm list || true \
9&& echo "Node.js version:" \
10&& node --version \
11&& echo "NPM version:" \
12&& npm --version

INPUT_SCHEMA.json

1{
2    "title": "Rebuilder Input",
3    "type": "object",
4    "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!",
5    "schemaVersion": 1,
6    "properties": {
7        "regexString": {
8            "title": "Regular Expression",
9            "type": "string",
10            "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!",
11            "editor": "textfield"
12        },
13        "version": {
14            "title": "Version",
15            "type": "string",
16            "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.",
17            "editor": "textfield",
18            "default": "0.0"
19        },
20        "buildTag": {
21            "title": "Build tag",
22            "type": "string",
23            "description": "Tag to use for all the builds.",
24            "editor": "textfield",
25            "default": "latest"
26        },
27        "waitForFinish": {
28            "title": "Wait for finish",
29            "type": "boolean",
30            "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.",
31            "default": false
32        }
33    },
34    "required": ["regexString"]
35}

main.js

1const Apify = require('apify');
2
3const { utils: { log, sleep }, client } = Apify;
4
5Apify.main(async () => {
6    log.info('Getting input.');
7    const {
8        regexString,
9        waitForFinish,
10        version,
11        buildTag
12    } = await Apify.getInput();
13
14    log.info('Constructing regex.')
15    const regex = new RegExp(regexString, 'i');
16
17    log.info('Getting all user\'s actors.');
18    const { items } = await client.acts.listActs();
19
20    log.info('Filtering items that match our regex.');
21    const actorsToRebuild = items.filter(({ name }) => regex.test(name));
22    const actorCount = actorsToRebuild.length;
23    if (actorCount > 50) {
24        throw new Error(`Too many actors to rebuild (${actorCount}).`
25         + 'Max is 50. Use a more specific regular expression.');
26    }
27
28    log.info(`Starting build of ${actorCount} actors.`);
29    let failedBuildsCount = 0;
30    const promises = actorsToRebuild.map(async ({ id }, idx) => {
31        await sleep(idx * 500);
32        return client.acts.buildAct({
33            actId: id,
34            version,
35            useCache: false,
36            tag: buildTag,
37            waitForFinish: waitForFinish ? 120 : 0
38        }).catch(err => {
39            log.exception(err);
40            failedBuildsCount++
41        });
42        
43    });
44    await Promise.all(promises);
45
46    log.info(`All builds ${waitForFinish ? 'finished' : 'dispatched'}.`);
47    if (failedBuildsCount) {
48        throw new Error('Some of the builds were not successful. See log.');
49    }
50});

package.json

1{
2    "name": "rebuilder",
3    "version": "0.0.1",
4    "description": "Actors description",
5    "main": "main.js",
6    "dependencies": {
7        "apify": "beta"
8    },
9    "scripts": {
10        "start": "node main.js"
11    },
12    "author": "mnmkng"
13}
Developer
Maintained by Community
Actor metrics
  • 1 monthly user
  • Created in Apr 2019
  • Modified over 1 year ago
Categories