wip #8 complete rewrite of worker
This commit is contained in:
parent
a6fa9a7149
commit
b08e96fe6b
45
worker.moon
45
worker.moon
@ -3,28 +3,31 @@ lock = require "resty.lock"
|
||||
import Tags, Tracks from require "models"
|
||||
import escape_literal from require "lapis.db"
|
||||
|
||||
add_job = (interval, name, fn, init) ->
|
||||
run = (premature, arg) ->
|
||||
if premature return
|
||||
next_id = (id=0) ->
|
||||
next_tag = Tags\select "WHERE id > ? ORDER BY id ASC LIMIT 1", id
|
||||
unless next_tag
|
||||
next_tag = Tags\select "WHERE id > '0' ORDER BY id ASC LIMIT 1"
|
||||
if next_tag
|
||||
return next_tag[1].id
|
||||
|
||||
thread_lock = lock\new "worker", { timeout: 0, exptime: interval }
|
||||
if thread_lock\lock "t#{name}"
|
||||
update_tag_counts = (premature, tag_id) ->
|
||||
if premature return
|
||||
|
||||
time_lock = lock\new "worker", { exptime: interval }
|
||||
if time_lock\lock "t#{name}"
|
||||
arg = fn(arg)
|
||||
thread_lock\unlock!
|
||||
ngx.timer.at(interval, run, arg)
|
||||
ngx.timer.at(0, run, init)
|
||||
thread_lock = lock\new "worker", { timeout: 0, exptime: 2 }
|
||||
if thread_lock\lock "update_tag_counts.thread"
|
||||
time_lock = lock\new "worker", { exptime: 2 }
|
||||
if time_lock\lock "update_tag_counts.time"
|
||||
|
||||
update_tag_counts = (id=1) ->
|
||||
if tag = Tags\find(:id)
|
||||
tag_pattern = escape_literal tag.name
|
||||
tag_pattern = "'% #{tag_pattern\sub 2, -2} %'"
|
||||
count = Tracks\count "tags LIKE #{tag_pattern}"
|
||||
tag\update(:count)
|
||||
next_tag = Tags\select "WHERE id > ? ORDER BY id ASC LIMIT 1", id
|
||||
if next_tag = next_tag[1]
|
||||
return next_tag.id
|
||||
if tag_id
|
||||
if tag = Tags\find id: tag_id
|
||||
tag_pattern = escape_literal tag.name
|
||||
tag_pattern = "'% #{tag_pattern\sub 2, -2} %'"
|
||||
count = Tracks\count "tags LIKE #{tag_pattern}"
|
||||
tag\update(:count)
|
||||
|
||||
add_job 60, "tags", update_tag_counts
|
||||
tag_id = next_id(tag_id) or tag_id
|
||||
thread_lock\unlock!
|
||||
|
||||
ngx.timer.at(2, update_tag_counts, tag_id)
|
||||
|
||||
ngx.timer.at(0, update_tag_counts)
|
||||
|
Reference in New Issue
Block a user