Skip to content

Commit

Permalink
Merge pull request #3934 from bgeuken/rabbit
Browse files Browse the repository at this point in the history
[frontend] Publish events to a Rabbit MQ bus
  • Loading branch information
bgeuken committed Oct 5, 2017
2 parents 75f6003 + 57c88b4 commit 182ea73
Show file tree
Hide file tree
Showing 20 changed files with 231 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ Style/MultilineBlockChain:
Style/RedundantReturn:
Enabled: false

Style/GlobalVars:
AllowedVariables: ['$rabbitmq_conn', '$rabbitmq_exchange', '$rabbitmq_channel']

##################### Metrics ##################################

# Checks if the length a class exceeds some maximum value
Expand Down
8 changes: 0 additions & 8 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -809,14 +809,6 @@ Style/For:
Style/FormatStringToken:
EnforcedStyle: template

# Offense count: 7
# Configuration parameters: AllowedVariables.
Style/GlobalVars:
Exclude:
- 'src/api/test/functional/interconnect_test.rb'
- 'src/api/test/functional/request_controller_test.rb'
- 'src/api/test/functional/source_controller_test.rb'

# Offense count: 111
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
Expand Down
2 changes: 2 additions & 0 deletions src/api/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ gem 'peek-dalli'
gem 'peek-mysql2'
# for kerberos authentication
gem "gssapi", require: false
# for sending events to rabbitmq
gem 'bunny'

group :development, :production do
# to have the delayed job daemon
Expand Down
4 changes: 4 additions & 0 deletions src/api/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ GEM
airbrake (7.0.2)
airbrake-ruby (~> 2.4)
airbrake-ruby (2.4.0)
amq-protocol (2.2.0)
annotate (2.7.2)
activerecord (>= 3.2, < 6.0)
rake (>= 10.4, < 13.0)
Expand All @@ -63,6 +64,8 @@ GEM
bullet (5.6.1)
activesupport (>= 3.0.0)
uniform_notifier (~> 1.10.0)
bunny (2.7.1)
amq-protocol (>= 2.2.0)
capybara (2.15.3)
addressable
mini_mime (>= 0.1.3)
Expand Down Expand Up @@ -414,6 +417,7 @@ DEPENDENCIES
bcrypt
bullet
bundler
bunny
capybara
capybara_minitest_spec
chunky_png
Expand Down
4 changes: 4 additions & 0 deletions src/api/app/models/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def ldap_enabled?
CONFIG['ldap_mode'] == :on
end

def amqp_namespace
CONFIG['amqp_namespace'] || 'opensuse.obs'
end

def passwords_changable?
change_password && CONFIG['proxy_auth_mode'] != :on && CONFIG['ldap_mode'] != :on
end
Expand Down
12 changes: 12 additions & 0 deletions src/api/app/models/event/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'rabbitmq_bus'

# This class represents some kind of event within the build service
# that users (or services) would like to know about
module Event
Expand Down Expand Up @@ -78,6 +80,10 @@ def inherited(subclass)
subclass.create_jobs(*create_jobs)
subclass.receiver_roles(*receiver_roles)
end

def message_bus_queue
raise NotImplementedError
end
end

# just for convenience
Expand Down Expand Up @@ -273,6 +279,12 @@ def _roles(role, project, package = nil)
obj_roles(p, role)
end

def send_to_bus
RabbitmqBus.publish(self.class.message_bus_queue, read_attribute(:payload))
rescue Bunny::Exception => e
logger.error "Publishing to RabbitMQ failed: #{e.message}"
end

private

def shorten_payload_if_necessary
Expand Down
15 changes: 15 additions & 0 deletions src/api/app/models/event/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def custom_headers
class Event::BuildSuccess < Event::Build
self.raw_type = 'BUILD_SUCCESS'
self.description = 'Package has succeeded building'
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.package.build_success"
end
end

class Event::BuildFail < Event::Build
Expand All @@ -31,6 +36,11 @@ class Event::BuildFail < Event::Build
self.raw_type = 'BUILD_FAIL'
self.description = 'Package has failed to build'
receiver_roles :maintainer, :bugowner, :reader
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.package.build_fail"
end

def subject
"Build failure of #{payload['project']}/#{payload['package']} in #{payload['repository']}/#{payload['arch']}"
Expand Down Expand Up @@ -74,4 +84,9 @@ def custom_headers
class Event::BuildUnchanged < Event::Build
# no raw_type as it should not go to plugins
self.description = 'Package has succeeded building with unchanged result'
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.package.build_unchanged"
end
end
15 changes: 15 additions & 0 deletions src/api/app/models/event/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def custom_headers
class Event::CommentForProject < ::Event::Project
include CommentEvent
receiver_roles :maintainer
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.project.comment"
end

self.description = 'New comment for project created'

Expand All @@ -43,6 +48,11 @@ def subject
class Event::CommentForPackage < ::Event::Package
include CommentEvent
receiver_roles :maintainer
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.package.comment"
end

self.description = 'New comment for package created'

Expand All @@ -56,6 +66,11 @@ class Event::CommentForRequest < ::Event::Request
self.description = 'New comment for request created'
payload_keys :request_number
receiver_roles :source_maintainer, :target_maintainer, :creator, :reviewer
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.request.comment"
end

def subject
req = BsRequest.find_by_number(payload['number'])
Expand Down
50 changes: 50 additions & 0 deletions src/api/app/models/event/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class Package < Base
class CreatePackage < Package
self.raw_type = 'SRCSRV_CREATE_PACKAGE'
self.description = 'Package was created'
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.package.create"
end

def subject
"New Package #{payload['project']}/#{payload['package']}"
Expand All @@ -16,12 +21,22 @@ def subject
class UpdatePackage < Package
self.raw_type = 'SRCSRV_UPDATE_PACKAGE'
self.description = 'Package meta data was updated'
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.package.update"
end
end

class UndeletePackage < Package
self.raw_type = 'SRCSRV_UNDELETE_PACKAGE'
self.description = 'Package was undeleted'
payload_keys :comment
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.package.undelete"
end

def set_payload(attribs, keys)
attribs['comment'] = attribs['comment'][0..800] unless attribs['comment'].blank?
Expand All @@ -34,6 +49,11 @@ class DeletePackage < Package
self.raw_type = 'SRCSRV_DELETE_PACKAGE'
self.description = 'Package was deleted'
payload_keys :comment, :requestid
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.package.delete"
end

def set_payload(attribs, keys)
attribs['comment'] = attribs['comment'][0..800] unless attribs['comment'].blank?
Expand All @@ -45,6 +65,11 @@ class BranchCommand < Package
self.raw_type = 'SRCSRV_BRANCH_COMMAND'
self.description = 'Package was branched'
payload_keys :targetproject, :targetpackage, :user
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.package.branch"
end

def subject
"Package Branched: #{payload['project']}/#{payload['package']} => #{payload['targetproject']}/#{payload['targetpackage']}"
Expand All @@ -55,6 +80,11 @@ class VersionChange < Package
self.raw_type = 'SRCSRV_VERSION_CHANGE'
self.description = 'Package has changed its version'
payload_keys :comment, :requestid, :files, :rev, :newversion, :user, :oldversion
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.package.version_change"
end

def set_payload(attribs, keys)
attribs['comment'] = attribs['comment'][0..800] unless attribs['comment'].blank?
Expand All @@ -69,6 +99,11 @@ class Commit < Package
payload_keys :project, :package, :comment, :user, :files, :rev, :requestid

create_jobs :update_backend_infos_job
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.package.commit"
end

def subject
"#{payload['project']}/#{payload['package']} r#{payload['rev']} commited"
Expand All @@ -85,6 +120,11 @@ class Upload < Package
self.raw_type = 'SRCSRV_UPLOAD'
self.description = 'Package sources were uploaded'
payload_keys :project, :package, :comment, :filename, :requestid, :target, :user
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.package.upload"
end
end

class ServiceSuccess < Package
Expand All @@ -93,6 +133,11 @@ class ServiceSuccess < Package
payload_keys :comment, :package, :project, :rev, :user, :requestid
receiver_roles :maintainer, :bugowner
create_jobs :update_backend_infos_job
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.package.service_success"
end

def subject
"Source service succeeded of #{payload['project']}/#{payload['package']}"
Expand All @@ -111,6 +156,11 @@ class ServiceFail < Package
payload_keys :comment, :error, :package, :project, :rev, :user, :requestid
receiver_roles :maintainer, :bugowner
create_jobs :update_backend_infos_job
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.package.service_fail"
end

def subject
"Source service failure of #{payload['project']}/#{payload['package']}"
Expand Down
5 changes: 5 additions & 0 deletions src/api/app/models/event/packtrack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ class Event::Packtrack < Event::Base

# for package tracking in first place
create_jobs :update_released_binaries_job
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.repo.packtrack"
end
end

# == Schema Information
Expand Down
25 changes: 25 additions & 0 deletions src/api/app/models/event/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class CreateProject < Project
self.raw_type = 'SRCSRV_CREATE_PROJECT'
self.description = 'Project is created'
payload_keys :sender
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.project.create"
end

def subject
"New Project #{payload['project']}"
Expand All @@ -18,24 +23,44 @@ class UpdateProjectConfig < Project
self.raw_type = 'SRCSRV_UPDATE_PROJECT_CONFIG'
self.description = 'Project _config was updated'
payload_keys :sender, :files, :comment
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.project.update_project_conf"
end
end

class UndeleteProject < Project
self.raw_type = 'SRCSRV_UNDELETE_PROJECT'
self.description = 'Project was undeleted'
payload_keys :comment, :sender
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.project.undelete"
end
end

class UpdateProject < Project
self.raw_type = 'SRCSRV_UPDATE_PROJECT'
self.description = 'Project meta was updated'
payload_keys :sender
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.project.update"
end
end

class DeleteProject < Project
self.raw_type = 'SRCSRV_DELETE_PROJECT'
self.description = 'Project was deleted'
payload_keys :comment, :requestid, :sender
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.project.delete"
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions src/api/app/models/event/repo_publish_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ class Event::RepoPublishState < Event::Base
self.raw_type = 'REPO_PUBLISH_STATE'
self.description = 'Publish State of Repository has changed'
payload_keys :project, :repo, :state
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.repo.publish_state"
end
end

# == Schema Information
Expand Down
5 changes: 5 additions & 0 deletions src/api/app/models/event/repo_published.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ class Event::RepoPublished < Event::Base
self.raw_type = 'REPO_PUBLISHED'
self.description = 'Repository was published'
payload_keys :project, :repo
after_commit :send_to_bus

def self.message_bus_queue
"#{Configuration.amqp_namespace}.repo.published"
end
end

# == Schema Information
Expand Down
Loading

0 comments on commit 182ea73

Please sign in to comment.