-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRepo.js
72 lines (60 loc) · 2.19 KB
/
Repo.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
var fs = require('fs');
var _ = require('lodash');
var Database = require('better-sqlite3');
class RepoInterface {
addURLS(new_urls) { }
finishURL(url, data) { }
failURL(url, data) { }
getNextURL() { }
getAll() { }
}
class Repo extends RepoInterface {
constructor(filename) {
super()
if (filename)
this._db = new Database(filename)
else
this._db = new Database('memory', { memory: true })
this._db.prepare('create table if not EXISTs SESSIONS (URL TEXT PRIMARY KEY,START_TIME INTEGER,END_TIME INTEGER,STATUS TEXT,RESULT TEXT)').run()
this.selectAllstmt = this._db.prepare("SELECT * FROM SESSIONS");
this.insertstmt = this._db.prepare("INSERT OR IGNORE INTO SESSIONS (URL,STATUS) VALUES (@url,'q')");
this.selectstmt = this._db.prepare("SELECT URL FROM SESSIONS where status ='q' LIMIT 1");
this.updatestatus_start = this._db.prepare("UPDATE SESSIONS SET STATUS = @status, START_TIME = @start_time WHERE URL=@url");
this.updatestatus_end = this._db.prepare("UPDATE SESSIONS SET STATUS = @status, END_TIME = @end_time ,RESULT =@result WHERE URL=@url");
}
addURLS(new_urls) {
var db = this._db;
new_urls = _.uniq(_.concat(new_urls))
for(var i=0;i<new_urls.length;i++){
var data = { url : new_urls[i]}
//console.log("queue add ", new_urls[i])
this.insertstmt.run(data)
}
}
finishURL(url, data) {
console.log("queue finish ", url)
this.updatestatus_end.run({
status :'f',end_time:new Date().getTime() ,url:url,result:data
})
}
failURL(url, data) {
console.log("queue fail ", url)
this.updatestatus_end.run({
status :'e',end_time:new Date().getTime() ,url:url,result:data
})
}
getNextURL() {
var url = this.selectstmt.get();
if(url){
url = url.URL
this.updatestatus_start.run({
status :'p',start_time:new Date().getTime() ,url:url
})
}
return url
}
getAll() {
return this.selectAllstmt.all()
}
}
module.exports = Repo