Module.extend is a public method
[gitorious:mainline.git] / lib / gitorious / messaging / resque_adapter.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2011-2013 Gitorious AS
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU Affero General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU Affero General Public License for more details.
14 #
15 #   You should have received a copy of the GNU Affero General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #++
18 require "resque"
19 require 'resque/plugins/job_stats'
20
21 # Resque backed implementation of the Gitorious messaging API. For use with
22 # lib/gitorious/messaging
23 module Gitorious::Messaging::ResqueAdapter
24   module Publisher
25     QUEUES = {
26       # Maps queue => processor class (see app/processors)
27       "GitoriousDestroySshKey" => "DestroySshKey",
28       "GitoriousMergeRequestBackend" => "MergeRequestGitBackend",
29       "GitoriousMergeRequestCreation" => "MergeRequest",
30       "GitoriousMergeRequestVersionDeletion" => "MergeRequestVersion",
31       "GitoriousEmailNotifications" => "MessageForwarding",
32       "GitoriousNewSshKey" => "NewSshKey",
33       "GitoriousPush" => "Push",
34       "GitoriousRepositoryCloning" => "RepositoryCloning",
35       "GitoriousProjectRepositoryCreation" => "ProjectRepositoryCreation",
36       "GitoriousTrackingRepositoryCreation" => "TrackingRepositoryCreation",
37       "GitoriousRepositoryDeletion" => "RepositoryDeletion",
38       "GitoriousPostReceiveWebHook" => "Service",
39       "GitoriousWikiRepositoryCreation" => "WikiRepositoryCreation"
40     }
41
42     # Locate the correct class to pick queue from
43     #
44     def queue(queue)
45       @queues ||= {}
46       queue_class_name = "#{QUEUES[queue.to_s.sub(/\/queue\//, "")]}Processor"
47       @queues[queue] ||= ResqueQueue.new(queue, queue_class_name)
48     end
49
50     def do_publish(queue, message)
51       queue(queue).publish(message)
52     end
53   end
54
55   module Consumer
56     def self.included(klass)
57       klass.extend(self) if klass != Gitorious::Messaging::Consumer
58     end
59
60     def self.extended(mod)
61       mod.extend(Resque::Plugins::JobStats)
62     end
63
64     def consumes(queue, options = {})
65       @queue = queue.sub(/\/queue\//, "")
66     end
67
68     def perform(message)
69       self.new.consume(message)
70     end
71   end
72
73   class ResqueQueue
74     attr_reader :queue, :processor
75
76     def initialize(queue, processor)
77       @queue = queue
78       @processor = processor
79     end
80
81     def publish(payload)
82       Resque.push(queue, :class => processor, :args => [payload])
83     end
84   end
85 end