This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.coffee
88 lines (73 loc) · 2.23 KB
/
example.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
Hermes = require "./index"
cfg = require("../config/load_config")()
URL = require "url"
amqp = require "amqp"
async = require "async"
port = cfg.frontend_server.port
port = undefined if port is 80
baseUrl = URL.format
hostname: cfg.frontend_server.host
port: port
pathname: cfg.frontend_server.path
protocol: cfg.frontend_server.protocol
scraper = new Hermes.Scraper baseUrl,
excludeFilters: [/^#*\/notifications/]
memoryDebug: true
memoryDebugInterval: 60000
pageOptions:
showBrowserLog: false
buildOnDemand = (fragment, cb) ->
scraper.scrape "#!#{fragment}", (err, html) =>
cb err, html
s = new Hermes.Server
buildOnDemandFn: buildOnDemand
s.listen cfg.seo_server.port
rerender = (queue, message) ->
switch message.type
when "user" then fragment = "/#{message.short_name}"
when "category" then fragment = "/categories/#{message.short_name}"
when "story" then fragment = "/story/#{message.story_id}"
console.log "Received a rerender message"
console.log message
console.log "Rendering fragment #{fragment}"
scraper.scrape "#!#{fragment}", (err, html) =>
console.error err if err
queue.shift()
# listen for rebuild instructions on rabbitmq as well
connection = amqp.createConnection
host: cfg.amqp.host
port: cfg.amqp.port
login: cfg.amqp.login
password: cfg.amqp.password
vhost: cfg.amqp.vhost
exchange = "amq.topic"
queue = null
# TODO clean up error handling, this implementation eats errors ;)
# If something breaks, it's jonas' fault
async.series
"wait for connection": (done) ->
connection.on "ready", -> done()
"wait for exchange": (done) ->
exchange = connection.exchange cfg.amqp.hermes_exchange,
type: 'topic'
durable: true
autoDelete: false
exchange.on "open", -> done()
"wait for queue": (done) ->
queue = connection.queue "seo_renderer",
autoDelete: false,
durable: true
queue.on "open", -> done()
"add binding": (done) ->
queue.bind exchange, "rerender"
queue.on "queueBindOk", -> done()
"subscribe to queue": (done) ->
queue.subscribe
ack: true
, (message) -> rerender queue, arguments...
done()
, (err) ->
if err
console.error "An error happened :("
console.log err
throw err