|
@@ -0,0 +1,30 @@
|
|
1
|
+lock = require "resty.lock"
|
|
2
|
+
|
|
3
|
+import Tags, Tracks from require "models"
|
|
4
|
+import escape_literal from require "lapis.db"
|
|
5
|
+
|
|
6
|
+add_job = (interval, name, fn, init) ->
|
|
7
|
+ run = (premature, arg) ->
|
|
8
|
+ if premature return
|
|
9
|
+
|
|
10
|
+ thread_lock = lock\new "worker". { timeout: 0, exptime: interval }
|
|
11
|
+ if thread_lock\lock "t#{name}"
|
|
12
|
+
|
|
13
|
+ time_lock = lock\new "worker", { exptime: interval }
|
|
14
|
+ if time_lock\lock "t#{name}"
|
|
15
|
+ arg = fn(arg)
|
|
16
|
+ thread_lock\unlock!
|
|
17
|
+ ngx.timer.at(interval, run, arg)
|
|
18
|
+ ngx.timer.at(0, run, init)
|
|
19
|
+
|
|
20
|
+update_tag_counts = (id=1) ->
|
|
21
|
+ if tag = Tags\find(:id)
|
|
22
|
+ tag_pattern = escape_literal tag.name
|
|
23
|
+ tag_pattern = "'% #{tag_pattern\sub 2, -2} %'"
|
|
24
|
+ count = Tracks\count "tags LIKE #{tag_pattern}"
|
|
25
|
+ tag\update(:count)
|
|
26
|
+ next_tag = Tags\select "WHERE id > ? ORDER BY id ASC LIMIT 1", id
|
|
27
|
+ if next_tag = next_tag[1]
|
|
28
|
+ return next_tag.id
|
|
29
|
+
|
|
30
|
+add_job 60, "tags", update_tag_counts
|