Resurrect run on Out of memory avatar

Resurrect run on Out of memory

Try for free

No credit card required

View all Actors
Resurrect run on Out of memory

Resurrect run on Out of memory

lukaskrivka/resurrect-on-out-of-memory
Try for free

No credit card required

Simple helper actor to resurrect your runs when they run out of memory.

.editorconfig

1root = true
2
3[*]
4indent_style = space
5indent_size = 4
6charset = utf-8
7trim_trailing_whitespace = true
8insert_final_newline = true
9end_of_line = lf

.eslintrc

1{
2    "extends": "@apify"
3}

.gitignore

1# This file tells Git which files shouldn't be added to source control
2
3.idea
4node_modules
5
6apify_storage

Dockerfile

1# First, specify the base Docker image. You can read more about
2# the available images at https://sdk.apify.com/docs/guides/docker-images
3# You can also use any other image from Docker Hub.
4FROM apify/actor-node:16
5
6# Second, copy just package.json and package-lock.json since it should be
7# the only file that affects "npm install" in the next step, to speed up the build
8COPY package*.json ./
9
10# Install NPM packages, skip optional and development dependencies to
11# keep the image small. Avoid logging too much and print the dependency
12# tree for debugging
13RUN npm --quiet set progress=false \
14 && npm install --only=prod --no-optional \
15 && echo "Installed NPM packages:" \
16 && (npm list --only=prod --no-optional --all || true) \
17 && echo "Node.js version:" \
18 && node --version \
19 && echo "NPM version:" \
20 && npm --version
21
22# Next, copy the remaining files and directories with the source code.
23# Since we do this after NPM install, quick build will be really fast
24# for most source file changes.
25COPY . ./
26
27# Optionally, specify how to launch the source code of your actor.
28# By default, Apify's base Docker images define the CMD instruction
29# that runs the Node.js source code using the command specified
30# in the "scripts.start" section of the package.json file.
31# In short, the instruction looks something like this:
32#
33# CMD npm start

apify.json

1{
2	"name": "resurrect-on-out-of-memory",
3	"version": "0.0",
4	"buildTag": "latest",
5	"env": null,
6	"template": "project_empty"
7}

package.json

1{
2	"name": "resurrect-on-out-of-memory",
3	"version": "0.0.1",
4	"description": "This is a boilerplate of an Apify actor.",
5	"dependencies": {
6		"apify": "^2.2.2"
7	},
8	"devDependencies": {
9		"@apify/eslint-config": "^0.1.3",
10		"eslint": "^7.0.0"
11	},
12	"scripts": {
13		"start": "node src/main.js",
14		"lint": "./node_modules/.bin/eslint ./src --ext .js,.jsx",
15		"lint:fix": "./node_modules/.bin/eslint ./src --ext .js,.jsx --fix",
16		"test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1"
17	},
18	"author": "It's not you it's me",
19	"license": "ISC"
20}

src/main.js

1const Apify = require('apify');
2
3const { log } = Apify.utils;
4
5Apify.main(async () => {
6    // We should only be called via webhook
7    const input = await Apify.getInput();
8
9    const { email } = input;
10
11    const client = Apify.newClient();
12
13    const { resource } = input;
14    const { id } = resource;
15
16    // We check if the run was out of memory (137 status)
17    const { exitCode, status } = await client.run(id).get();
18
19    if (status !== 'FAILED') {
20        log.warning(`Run ${id} finished with status ${status}, not FAILED. We have nothing to resurrect. `
21            + `Did you configure the webhook correctly only for FAILED status?`);
22    }
23    const isOutOfMemory = exitCode === 137;
24
25    if (isOutOfMemory) {
26        log.info(`Run ${id} was out of memory (status 137), we will resurrect it.`);
27        await client.run(id).resurrect();
28        log.info(`Run resurrected.`);
29        if (email) {
30            log.info(`Sending email report and finishing`);
31            const emailInput = {
32                to: email,
33                subject: `Apify - Run ${id} was out of memory, resurrected`,
34                text: `Apify - Run ${id} was out of memory, resurrected`,
35            };
36            await client.actor('apify/send-mail').start(emailInput);
37        }
38    } else {
39        log.info(`Run ${id} was not out of memory, it failed with exit code ${exitCode}. We will not resurrect it.`);
40    }
41});
Developer
Maintained by Community
Actor metrics
  • 1 monthly user
  • 2 stars
  • 100.0% runs succeeded
  • Created in Apr 2022
  • Modified over 2 years ago
Categories