-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
144 lines (141 loc) · 5.48 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const { openiap } = require("@openiap/nodeapi")
const fs = require('fs');
var client = new openiap();
const puppeteer = require('puppeteer');
async function ProcessWorkitem(workitem) {
console.log(`Processing workitem id ${workitem._id} retry #${workitem.retries}`);
if (workitem.payload == null) workitem.payload = {};
try {
if (typeof workitem.payload == "string") workitem.payload = JSON.parse(workitem.payload)
} catch (error) {
}
workitem.name = "Hello kitty " + (new Date()).toISOString(); // timestamp name, to show we updated the workitem
let browser = null;
try {
browser = await puppeteer.launch({
executablePath: 'google-chrome', // for some reason we NEED to set executablePath when running in docker.
headless: true, // set to false to see the browser in action
args: ['--no-sandbox', '--disable-setuid-sandbox'] // if running with root, you must also add ,
}); //
const page = await browser.newPage();
await page.goto('https://developer.chrome.com/');
// Set screen size
await page.setViewport({ width: 1080, height: 1024 });
var crypto = require('crypto');
filename = crypto.randomBytes(4).readUInt32LE(0) + '.png';
await page.goto("https://news.ycombinator.com/");
let urls = await page.evaluate(() => {
let results = [];
let items = document.querySelectorAll('.titleline a');
items.forEach((item) => {
results.push({
url: item.getAttribute('href'),
text: item.innerText,
});
});
return results;
})
workitem.payload.urls = urls;
await page.screenshot({ // Screenshot the website using defined options
path: filename, // Save the screenshot in current directory
fullPage: false // take a fullpage screenshot
});
} finally {
if (browser != null) {
await browser.close();
}
}
}
async function ProcessWorkitemWrapper(workitem) {
var currentfiles = fs.readdirSync(".");
try {
for (var i = 0; i < workitem.files.length; i++) {
const file = workitem.files[i];
await client.DownloadFile({ id: file._id });
}
await ProcessWorkitem(workitem);
workitem.state = "successful"
} catch (error) {
workitem.state = "retry"
workitem.errortype = "application" // business rule will never retry / application will retry as mamy times as defined on the workitem queue"
workitem.errormessage = error.message ? error.message : error
workitem.errorsource = error.stack.toString()
}
var _files = [];
workitem.files = [];
var files = fs.readdirSync(".");
for (var i = 0; i < files.length; i++) {
var filename = files[i]
if (currentfiles.indexOf(filename) == -1 && fs.lstatSync(filename).isFile()) {
console.log("Adding " + filename + " to workitem")
workitem.files.push({
filename,
compressed: false,
file: new Uint8Array(fs.readFileSync(filename))
})
_files.push(filename);
}
}
await client.UpdateWorkitem({ workitem })
for (var i = 0; i < files.length; i++) {
var filename = files[i]
if (currentfiles.indexOf(filename) == -1 && fs.lstatSync(filename).isFile()) {
console.log("Removing " + filename)
fs.unlinkSync(filename);
}
}
}
async function onConnected(client) {
var queue = process.env.queue;
var wiq = process.env.wiq;
if (wiq == null || wiq == "") wiq = "nodepuppeteertest"
if (queue == null || queue == "") queue = wiq;
const queuename = await client.RegisterQueue({ queuename: queue }, async (message) => {
try {
let workitem = null;
let counter = 0;
do {
workitem = await client.PopWorkitem({ wiq })
if (workitem != null) {
counter++;
await ProcessWorkitemWrapper(workitem);
}
} while (workitem != null)
if (counter > 0) {
console.log(`No more workitems in ${wiq} workitem queue`)
}
} catch (error) {
console.error(error)
}
})
console.log("Consuming queue " + queuename);
}
async function main() {
var wiq = process.env.wiq;
var queue = process.env.queue;
if (wiq == null || wiq == "") wiq = "nodepuppeteertest"
if (wiq == null || wiq == "") throw new Error("wiq environment variable is mandatory")
if (queue == null || queue == "") queue = wiq;
client.onConnected = onConnected;
await client.connect();
if (queue != null && queue != "") {
} else {
let counter = 1;
do {
let workitem = null;
do {
workitem = await client.PopWorkitem({ wiq })
if (workitem != null) {
counter++;
await ProcessWorkitemWrapper(workitem);
}
} while (workitem != null)
if (counter > 0) {
counter = 0;
console.log(`No more workitems in ${wiq} workitem queue`)
}
await new Promise(resolve => { setTimeout(resolve, 30000) }); // wait 30 seconds
} while (true)
}
}
main()