diff --git a/README.md b/README.md index 5d30dd0..eb8a189 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,8 @@ module OutboxerIntegration class PublishJob include Sidekiq::Job - def perform_async(args) - # TODO: handle published message here + def perform(args) + # TODO: handle message here end end end diff --git a/db/seeds.rb b/db/seeds.rb deleted file mode 100644 index 1b2f8cd..0000000 --- a/db/seeds.rb +++ /dev/null @@ -1,11 +0,0 @@ -begin - Outboxer::Models::Setting.create!(name: 'messages.published.count.historic', value: '0') -rescue ActiveRecord::RecordNotUnique - # no op as record already exists -end - -begin - Outboxer::Models::Setting.create!(name: 'messages.failed.count.historic', value: '0') -rescue ActiveRecord::RecordNotUnique - # no op as record already exists -end diff --git a/lib/outboxer.rb b/lib/outboxer.rb index f8e4d89..d024493 100644 --- a/lib/outboxer.rb +++ b/lib/outboxer.rb @@ -14,6 +14,7 @@ require_relative "outboxer/logger" require_relative "outboxer/database" +require_relative "outboxer/settings" require_relative "outboxer/message" require_relative "outboxer/messages" require_relative "outboxer/publisher" diff --git a/lib/outboxer/models/setting.rb b/lib/outboxer/models/setting.rb index f09f568..c20fcb7 100644 --- a/lib/outboxer/models/setting.rb +++ b/lib/outboxer/models/setting.rb @@ -3,7 +3,7 @@ module Models class Setting < ActiveRecord::Base self.table_name = 'outboxer_settings' - validates :name, presence: true, uniqueness: true + validates :name, presence: true validates :value, presence: true end end diff --git a/lib/outboxer/publisher.rb b/lib/outboxer/publisher.rb index 35b3d42..6434095 100644 --- a/lib/outboxer/publisher.rb +++ b/lib/outboxer/publisher.rb @@ -385,6 +385,8 @@ def publish( db_config = database.config(env: env, pool: concurrency + 2, path: db_config_path) database.connect(config: db_config, logger: logger) + Settings.create + queue = Queue.new publisher = create( diff --git a/lib/outboxer/railtie.rb b/lib/outboxer/railtie.rb index ead265e..d4eab68 100644 --- a/lib/outboxer/railtie.rb +++ b/lib/outboxer/railtie.rb @@ -8,16 +8,5 @@ class Railtie < Rails::Railtie require_relative '../../generators/sidekiq_publisher_generator' require_relative '../../generators/publisher_generator' end - - rake_tasks do - namespace :outboxer do - namespace :db do - desc "Load outboxer's seed data" - task seed: :environment do - load File.expand_path('../../db/seeds.rb', __dir__) - end - end - end - end end end diff --git a/lib/outboxer/settings.rb b/lib/outboxer/settings.rb new file mode 100644 index 0000000..6dcd1d1 --- /dev/null +++ b/lib/outboxer/settings.rb @@ -0,0 +1,21 @@ +module Outboxer + module Settings + extend self + + def create + ActiveRecord::Base.connection_pool.with_connection do + begin + Outboxer::Models::Setting.create!(name: 'messages.published.count.historic', value: '0') + rescue ActiveRecord::RecordNotUnique + # no op as record already exists + end + + begin + Outboxer::Models::Setting.create!(name: 'messages.failed.count.historic', value: '0') + rescue ActiveRecord::RecordNotUnique + # no op as record already exists + end + end + end + end +end diff --git a/lib/outboxer/web.rb b/lib/outboxer/web.rb index ac06ddd..7accf2e 100755 --- a/lib/outboxer/web.rb +++ b/lib/outboxer/web.rb @@ -44,6 +44,10 @@ class Web < Sinatra::Base set :public_folder, File.expand_path('../web/public', __FILE__) set :show_exceptions, false + configure do + Settings.create + end + helpers do def outboxer_path(path) "#{request.script_name}#{path}" diff --git a/outboxer.gemspec b/outboxer.gemspec index 83b1226..1c3d17f 100644 --- a/outboxer.gemspec +++ b/outboxer.gemspec @@ -19,7 +19,6 @@ Gem::Specification.new do |spec| spec_files = Dir.chdir(__dir__) do [ - 'db/seeds.rb', 'db/migrate/**/*', 'generators/**/*', 'LICENCE.txt', diff --git a/spec/lib/outboxer/settings/create_spec.rb b/spec/lib/outboxer/settings/create_spec.rb new file mode 100644 index 0000000..2bdaea8 --- /dev/null +++ b/spec/lib/outboxer/settings/create_spec.rb @@ -0,0 +1,14 @@ +require 'spec_helper' + +module Outboxer + RSpec.describe Settings, type: :module do + describe '.create' do + it 'creates setting' do + Settings.create + + expect(Models::Setting.find_by(name: 'messages.published.count.historic').value).to eq('0') + expect(Models::Setting.find_by(name: 'messages.failed.count.historic').value).to eq('0') + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 85c986e..1f83afe 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -41,6 +41,6 @@ # ignore end - load File.join(File.dirname(__FILE__), '../db/seeds.rb') + Outboxer::Settings.create end end diff --git a/tasks/database.rake b/tasks/database.rake index 8381c6b..4db8664 100644 --- a/tasks/database.rake +++ b/tasks/database.rake @@ -57,8 +57,6 @@ namespace :outboxer do db_config = Outboxer::Database.config(env: env, pool: 1) ActiveRecord::Base.establish_connection(db_config) - require_relative "../db/seeds" - ActiveRecord::Base.connection.disconnect! end