this should be called worker

This commit is contained in:
Christine Dodrill 2016-02-06 15:14:55 -08:00
parent bdba4bb60c
commit 4abe9b8ba3
6 changed files with 25 additions and 22 deletions

View File

@ -49,7 +49,7 @@ proc fromDBRow*(r: Row): Tweet =
proc getTweetsFrom*(url: string, username: string): seq[Tweet] =
var res = newSeq[Tweet]()
let body = url.getContent()
let body = url.getContent(timeout=20_000)
for line in body.splitLines().items():
if line.len > 0:
@ -57,7 +57,7 @@ proc getTweetsFrom*(url: string, username: string): seq[Tweet] =
return res
proc updateTweetsOnce*() =
proc updateTweetsOnce*() {. gcsafe .} =
var db = open("./data/twtxt.db", nil, nil, nil)
var users = db.getAllRows sql"select * from users"
@ -65,6 +65,7 @@ proc updateTweetsOnce*() =
for user in users.items():
echo "updating " & $user
try:
let
username = user[1]
url = user[2]
@ -74,11 +75,4 @@ proc updateTweetsOnce*() =
try:
db.exec(sql"insert into tweets values(null, ?, ?, ?);", username, tweet.date.timeInfoToTime().toSeconds().int(), tweet.message)
except: discard
when isMainModule:
while true:
try:
updateTweetsOnce()
except: discard
sleep 300_000 # 5 minutes

View File

@ -1,2 +1,3 @@
--define:ssl
--threads:on
--threadAnalysis:off

View File

@ -95,8 +95,6 @@ routes:
db.exec(sql"insert into users values (null, ?, ?)", username, url)
updateTweetsOnce()
redirect "/users/" & username & "/0"
except: fail()

9
src/worker.nim Normal file
View File

@ -0,0 +1,9 @@
import os, threadpool, twtxt
when isMainModule:
while true:
try:
spawn updateTweetsOnce()
except: discard
sleep 300_000 # 5 minutes

3
src/worker.nims Normal file
View File

@ -0,0 +1,3 @@
--define:ssl
--threads:on
--threadAnalysis:off

View File

@ -5,7 +5,7 @@ author = "Christine Dodrill"
description = "a list of twtxt hacker feeds"
license = "MIT"
srcDir = "src"
bin = @["twtxtlist", "twtxt"]
bin = @["twtxtlist", "worker"]
# Dependencies
@ -18,6 +18,4 @@ task db, "SQLite database prompt":
task run, "Run app":
exec "nimble build"
exec "./twtxt &"
exec "./twtxtlist"
exec "killall twtxt"