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