Sha256: 547e51da5bdcc29bfd7f222ada6d0dceb4040e8704afbeb92289b2db2a97179c

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module Travis
  module Notifications
    class Worker
      autoload :Payload, 'travis/notifications/worker/payload'
      autoload :Queue,   'travis/notifications/worker/queue'

      EVENTS = /job:.*:created/

      class << self
        def enqueue(job)
          new.enqueue(job)
        end

        def amqp
          @amqp ||= Travis::Amqp
        end

        def amqp=(amqp)
          @amqp = amqp
        end

        def default_queue
          @default_queue ||= Queue.new('builds.common')
        end

        def queues
          @queues ||= Array(Travis.config.queues).compact.map do |queue|
            Queue.new(*queue.values_at(*[:queue, :slug, :target, :language]))
          end
        end

        def queue_for(job)
          slug = job.repository.slug
          target, language = job.config.values_at(:target, :language)
          queues.detect { |queue| queue.matches?(slug, target, language) } || default_queue
        end

        def payload_for(job, extra)
          Payload.new(job, extra).to_hash
        end
      end

      delegate :amqp, :queue_for, :payload_for, :to => :'self.class'

      def notify(event, job, *args)
        enqueue(job)
      end

      def enqueue(job)
        queue = queue_for(job).name
        payload_for(job, :queue => queue).tap do |payload|
          amqp.publish(queue, payload)
          job.update_attributes!(:queue => queue)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
travis-core-0.0.1 lib/travis/notifications/worker.rb