wip #8 complete rewrite of worker
This commit is contained in:
parent
a6fa9a7149
commit
b08e96fe6b
37
worker.moon
37
worker.moon
@ -3,28 +3,31 @@ lock = require "resty.lock"
|
|||||||
import Tags, Tracks from require "models"
|
import Tags, Tracks from require "models"
|
||||||
import escape_literal from require "lapis.db"
|
import escape_literal from require "lapis.db"
|
||||||
|
|
||||||
add_job = (interval, name, fn, init) ->
|
next_id = (id=0) ->
|
||||||
run = (premature, arg) ->
|
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
|
||||||
|
|
||||||
|
update_tag_counts = (premature, tag_id) ->
|
||||||
if premature return
|
if premature return
|
||||||
|
|
||||||
thread_lock = lock\new "worker", { timeout: 0, exptime: interval }
|
thread_lock = lock\new "worker", { timeout: 0, exptime: 2 }
|
||||||
if thread_lock\lock "t#{name}"
|
if thread_lock\lock "update_tag_counts.thread"
|
||||||
|
time_lock = lock\new "worker", { exptime: 2 }
|
||||||
|
if time_lock\lock "update_tag_counts.time"
|
||||||
|
|
||||||
time_lock = lock\new "worker", { exptime: interval }
|
if tag_id
|
||||||
if time_lock\lock "t#{name}"
|
if tag = Tags\find id: tag_id
|
||||||
arg = fn(arg)
|
|
||||||
thread_lock\unlock!
|
|
||||||
ngx.timer.at(interval, run, arg)
|
|
||||||
ngx.timer.at(0, run, init)
|
|
||||||
|
|
||||||
update_tag_counts = (id=1) ->
|
|
||||||
if tag = Tags\find(:id)
|
|
||||||
tag_pattern = escape_literal tag.name
|
tag_pattern = escape_literal tag.name
|
||||||
tag_pattern = "'% #{tag_pattern\sub 2, -2} %'"
|
tag_pattern = "'% #{tag_pattern\sub 2, -2} %'"
|
||||||
count = Tracks\count "tags LIKE #{tag_pattern}"
|
count = Tracks\count "tags LIKE #{tag_pattern}"
|
||||||
tag\update(:count)
|
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
|
|
||||||
|
|
||||||
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