Skip to content

Commit

Permalink
add tasks sync-ly
Browse files Browse the repository at this point in the history
  • Loading branch information
layerssss committed Sep 24, 2014
1 parent 89f0558 commit b32d0ab
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 92 deletions.
27 changes: 19 additions & 8 deletions app.iced
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ app.use (req, res, next)->
app.use express.methodOverride()

client.startCron()
queue = client.queue
autorefresh = ->
client.queue.tasks.updateTasklist()
queue.append
name: "刷新任务列表"
func: queue.tasks.updateTasklist
setTimeout autorefresh, 60000 * (1 + Math.random() * 3)
autorefresh()

Expand All @@ -41,35 +44,43 @@ app.all '*', (req, res, n)->
return n 403 if process.env.ONLYFROM && -1 == process.env.ONLYFROM.indexOf ip
n null
app.post '/refresh', (req, res, n)->
client.queue.tasks.updateTasklist()

queue.append
name: "刷新任务列表"
func: queue.tasks.updateTasklist
res.redirect 'back'

app.post '/', (req, res, n)->
if req.files && req.files.bt && req.files.bt.path && req.files.bt.length
bt = req.files.bt
await fs.rename bt.path, "#{bt.path}.torrent", defer e
return cb e if e
client.queue.tasks.addBtTask bt.name, "#{bt.path}.torrent"
return n e if e
await queue.tasks.addBtTask bt.name, "#{bt.path}.torrent", defer e
return n e if e
else
client.queue.tasks.addTask req.body.url
await queue.tasks.addTask req.body.url, defer e
return n e if e
res.redirect '/'

app.get '/login', (req, res)->
res.locals.vcode = null
res.render 'login'
app.post '/login', (req, res, n)->
await client.queue.tasks.login req.body.username, req.body.password, req.body.vcode, defer e
await queue.tasks.login req.body.username, req.body.password, req.body.vcode, defer e
return n e if e
res.redirect '/'
app.get '/logout', (req, res, n)->
await client.queue.tasks.logout defer e
await queue.tasks.logout defer e
return n e if e
res.redirect '/'

app.delete '/tasks/:id', (req, res, n)->
if client.stats.retrieving?.task.id
client.stats.retrieving.kill()
client.queue.tasks.deleteTask req.params.id
queue.append
name: "删除任务 #{task.id}"
func: (fcb)->
queue.tasks.deleteTask req.params.id, fcb
res.redirect '/'


Expand Down
167 changes: 84 additions & 83 deletions client.iced
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ exports.startCron = ->
if queue.length
stats.task = queue.shift()
log.unshift "#{stats.task.name} 启动"
console.log log[log.length - 1]
await stats.task.func defer e
log.unshift "#{stats.task.name} 完成"
console.log log[log.length - 1]
if e
log.unshift e.message
console.error e.message
Expand All @@ -73,70 +75,71 @@ getPythonBin = (cb)->


queue.tasks =
retrieve: (task)->
queue.append
name: "取回 #{task.id}"
func: (cb)->
await getPythonBin defer e, pyothon_bin
return cb e if e
stats.retrieving = spawn pyothon_bin, [cli, 'download', '--continue', '--no-hash', task.id], stdio: 'pipe', cwd: workingDirectory
errBuffer = []
stats.retrieving.task = task
new lazy(stats.retrieving.stderr).lines.forEach (line)->
line ?= []
line = line.toString 'utf8'
errBuffer.push line
line = line.match /\s+(\d?\d%)\s+([^ ]{1,10})\s+([^ ]{1,10})\r?\n?$/
[dummy, stats.progress, stats.speed, stats.time] = line if line

await stats.retrieving.on 'exit', defer e
if e
stats.error[task.id] = errBuffer.join ''
stats.retrieving = null
queue.tasks.updateTasklist()
queue.tasks.deleteTask(task.id)
cb()
retrieve: (task, cb)->
await getPythonBin defer e, pyothon_bin
return cb e if e
stats.retrieving = spawn pyothon_bin, [cli, 'download', '--continue', '--no-hash', task.id], stdio: 'pipe', cwd: workingDirectory
errBuffer = []
stats.retrieving.task = task
new lazy(stats.retrieving.stderr).lines.forEach (line)->
line ?= []
line = line.toString 'utf8'
errBuffer.push line
line = line.match /\s+(\d?\d%)\s+([^ ]{1,10})\s+([^ ]{1,10})\r?\n?$/
[dummy, stats.progress, stats.speed, stats.time] = line if line

await stats.retrieving.on 'exit', defer e
if e
stats.error[task.id] = errBuffer.join ''
stats.retrieving = null
queue.append
name: "刷新任务列表"
func: queue.tasks.updateTasklist
queue.append
name: "删除任务 #{task.id}"
func: (fcb)->
queue.tasks.deleteTask task.id, fcb
cb()


updateTasklist: ->
queue.prepend
name: '刷新任务列表'
func: (cb)->
await getPythonBin defer e, pyothon_bin
return cb e if e
await exec "#{pyothon_bin} #{cli} config encoding utf-8", cwd: workingDirectory, defer e
return cb e if e
await exec "#{pyothon_bin} #{cli} list --no-colors", cwd: workingDirectory, defer e, out, err
if e && err.match /user is not logged in|Verification code required/
stats.requireLogin = true
return cb e
return cb e if e
_tasks = []
if out.match regexMG
for task in out.match regexMG
task = task.match regexQ
_tasks.push
id: task[1]
filename: task[2]
status: statusMap[task[3]]
statusLabel: statusMapLabel[task[3]]

stats.tasks = _tasks

for task in _tasks
if task.status=='success' && !stats.error[task.id]?
queue.tasks.retrieve task
cb()
deleteTask: (id)->
queue.prepend
name: "删除任务 #{id}"
func: (cb)->
await getPythonBin defer e, pyothon_bin
return cb e if e
await exec "#{pyothon_bin} #{cli} delete #{id}", cwd: workingDirectory, defer e, out, err
return cb e if e
queue.tasks.updateTasklist()
cb null
updateTasklist: (cb)->
await getPythonBin defer e, pyothon_bin
return cb e if e
await exec "#{pyothon_bin} #{cli} config encoding utf-8", cwd: workingDirectory, defer e
return cb e if e
await exec "#{pyothon_bin} #{cli} list --no-colors", cwd: workingDirectory, defer e, out, err
if e && err.match /user is not logged in|Verification code required/
stats.requireLogin = true
return cb e
return cb e if e
_tasks = []
if out.match regexMG
for task in out.match regexMG
task = task.match regexQ
_tasks.push
id: task[1]
filename: task[2]
status: statusMap[task[3]]
statusLabel: statusMapLabel[task[3]]

stats.tasks = _tasks

for task in _tasks
if task.status=='success' && !stats.error[task.id]?
queue.append
name: "取回 #{task.id}"
func: (fcb)->
queue.tasks.retrieve task, fcb
cb()
deleteTask: (id, cb)->
await getPythonBin defer e, pyothon_bin
return cb e if e
await exec "#{pyothon_bin} #{cli} delete #{id}", cwd: workingDirectory, defer e, out, err
return cb e if e
queue.append
name: "刷新任务列表"
func: queue.tasks.updateTasklist
cb null

login: (username, password, vcode, cb)->
await getPythonBin defer e, pyothon_bin
Expand Down Expand Up @@ -178,7 +181,9 @@ queue.tasks =
return cb null
if @login_result == 0
stats.requireLogin = false
queue.tasks.updateTasklist()
queue.append
name: "刷新任务列表"
func: queue.tasks.updateTasklist
cb null
else
console.error @login_output
Expand All @@ -195,25 +200,21 @@ queue.tasks =
stats.requireLogin = true
cb null

addBtTask: (filename, torrent)->
queue.append
name: "添加bt任务 #{filename}"
func: (cb)->
await getPythonBin defer e, pyothon_bin
return cb e if e
await exec "#{pyothon_bin} #{cli} add #{torrent}", cwd: workingDirectory, defer e, out, err
return cb e if e
queue.tasks.updateTasklist()
cb null
addTask: (url)->
queue.append
name: "添加任务 #{url}"
func: (cb)->
await getPythonBin defer e, pyothon_bin
return cb e if e
await exec "#{pyothon_bin} #{cli} add \"#{url}\"", cwd: workingDirectory, defer e, out, err
return cb e if e
queue.tasks.updateTasklist()
cb null
addBtTask: (filename, torrent, cb)->
await getPythonBin defer e, pyothon_bin
return cb e if e
await exec "#{pyothon_bin} #{cli} add #{torrent}", cwd: workingDirectory, defer e, out, err
return cb e if e
await queue.tasks.updateTasklist defer e
return cb e if e
cb null
addTask: (url, cb)->
await getPythonBin defer e, pyothon_bin
return cb e if e
await exec "#{pyothon_bin} #{cli} add \"#{url}\"", cwd: workingDirectory, defer e, out, err
return cb e if e
await queue.tasks.updateTasklist defer e
return cb e if e
cb null


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lixian-portal",
"version": "0.1.5",
"version": "0.1.6",
"description": "lixian-portal =============",
"dependencies": {
"iced-coffee-script": ">1.4.0",
Expand Down

0 comments on commit b32d0ab

Please sign in to comment.