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 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
|
||||||
if premature return
|
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 }
|
update_tag_counts = (premature, tag_id) ->
|
||||||
if thread_lock\lock "t#{name}"
|
if premature return
|
||||||
|
|
||||||
time_lock = lock\new "worker", { exptime: interval }
|
thread_lock = lock\new "worker", { timeout: 0, exptime: 2 }
|
||||||
if time_lock\lock "t#{name}"
|
if thread_lock\lock "update_tag_counts.thread"
|
||||||
arg = fn(arg)
|
time_lock = lock\new "worker", { exptime: 2 }
|
||||||
thread_lock\unlock!
|
if time_lock\lock "update_tag_counts.time"
|
||||||
ngx.timer.at(interval, run, arg)
|
|
||||||
ngx.timer.at(0, run, init)
|
|
||||||
|
|
||||||
update_tag_counts = (id=1) ->
|
if tag_id
|
||||||
if tag = Tags\find(:id)
|
if tag = Tags\find id: tag_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