-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
90 lines (84 loc) · 2.95 KB
/
server.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
const cluster = require('node:cluster')
const { fork } = require('node:child_process')
const os = require('node:os')
const process = require('node:process')
const schedule = require('node-schedule')
const path = require('node:path')
const { chromium } = require('playwright')
const { totalBidList, user } = require('./utils/constants.js')
const { GetBidList } = require('./update/list.js')
const Cache = require('cache-storage')
const FileStorage = require('cache-storage/Storage/FileSyncStorage');
const cache = new Cache(new FileStorage('./storage'), 'namespace');
const storage_state = cache.load('Storage')
const numCPUs = 3;
const id = "bidding"
function messageHandler(list) {
totalBidList = totalBidList.reduce((pre, cur) => {
let target = pre.find(ee => ee.id == cur.id)
if (target) {
Object.assign(target, cur)
} else {
pre.push(cur)
}
return pre
}, list)
console.log(totalBidList)
}
async function getBidList() {
console.log("Starting bid list job...")
const browser = await chromium.launch({
headless: false
});
const context = await browser.newContext({
storageState: storage_state || {},
ignoreHTTPSErrors: true
});
const page = await context.newPage()
//进入采招网登录页面
if (!storage_state) {
await page.goto('https://sso.bidcenter.com.cn/login')
await page.locator('#txtusername').waitFor()
//输入账号密码
await page.fill('#txtusername', user.userName)
await page.fill('#txtpassword', user.password)
await page.screenshot({ path: 'screenShot/login.png' });
await page.locator('#login_login_btn').waitFor()
await page.click('#login_login_btn')
// await page.goto(`https://search.bidcenter.com.cn/search?keywords=${searchText}&type=${bidType}`)
await page.locator('#jq_btn_search').waitFor()
//进入列表页面
await page.click('#jq_btn_search')
let storage = await context.storageState()
cache.save('Storage', storage)
await page.screenshot({ path: 'screenShot/main_search.png' })
}
try {
await GetBidList(page)
for (let i = 0; i < numCPUs; i++) {
let worker = fork(`index.js`, {
cwd: path.resolve(__dirname, 'update')
});
worker.send({ index: i, page:context, totalBidList })
}
for (const id in cluster.workers) {
cluster.workers[id].on('message', messageHandler);
}
// await browser.close()
} catch (error) {
return Promise.reject(error)
}
}
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// 定时任务执行
(async () => {
await getBidList()
})()
schedule.scheduleJob(id, '30 * * * * *', async () => {
try {
} catch (error) {
console.error("定时任务执行失败", error)
}
})
}