Class: Sad::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/sad/server.rb

Class Method Summary collapse

Class Method Details

.fetch(queue) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sad/server.rb', line 10

def fetch(queue)
	request = ::Sad::Config.redis.blpop(queue, 60)
	request.callback{|_, data|
		if data
			STDOUT.puts '-'*15 + data.inspect + '-'*15
			payload = Payload.decode(data)
			EM.defer{perform(payload.klass, payload.args)}
		end
		fetch(queue) unless shutdown?
	}
	request.errback{
		fetch(queue) unless shutdown?
	}
end

.perform(klass, args) ⇒ Object



25
26
27
# File 'lib/sad/server.rb', line 25

def perform(klass, args)
	klass.constantize.send :perform, args
end

.register_signalObject



29
30
31
32
33
# File 'lib/sad/server.rb', line 29

def register_signal
	trap('TERM') { shutdown!  }
   			trap('INT')  { shutdown!  }
   			trap('QUIT') { shutdown   }
end

.run(queue) ⇒ Object



4
5
6
7
8
# File 'lib/sad/server.rb', line 4

def run(queue)
	@_shutdown = false
	register_signal
	fetch([Sad::Config.namespace, queue].join ':')
end

.shutdownObject



44
45
46
# File 'lib/sad/server.rb', line 44

def shutdown
	@_shutdown = true
end

.shutdown!Object



35
36
37
38
# File 'lib/sad/server.rb', line 35

def shutdown!
	EM.stop
	exit(0)
end

.shutdown?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/sad/server.rb', line 40

def shutdown?
	@_shutdown
end