Class: Brut::CLI::Apps::New::Segments::Sidekiq

Inherits:
Base
  • Object
show all
Defined in:
lib/brut/cli/apps/new/segments/sidekiq.rb

Overview

Adds Sidekiq to a Brut app

Instance Attribute Summary

Attributes inherited from Base

#project_root

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#create!

Constructor Details

#initialize(project_root:, templates_dir:) ⇒ Sidekiq

Returns a new instance of Sidekiq.



7
8
9
10
# File 'lib/brut/cli/apps/new/segments/sidekiq.rb', line 7

def initialize(project_root:, templates_dir:)
  @project_root  = project_root
  @templates_dir = templates_dir / "segments" / "Sidekiq"
end

Class Method Details

.friendly_nameObject



4
# File 'lib/brut/cli/apps/new/segments/sidekiq.rb', line 4

def self.friendly_name = "Sidekiq for Background Jobs"

.segment_nameObject



5
# File 'lib/brut/cli/apps/new/segments/sidekiq.rb', line 5

def self.segment_name = "sidekiq"

Instance Method Details

#<=>(other) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/brut/cli/apps/new/segments/sidekiq.rb', line 43

def <=>(other)
  if self.class == other.class
    0
  elsif other.class == Brut::CLI::Apps::New::Segments::Heroku
    # If both herkou and sidekiq segments are activated, we want to do heroku first,
    # since Sidekiq will need to modify it.
    1
  else
    -1
  end
end

#add!Object



34
35
36
37
38
39
40
41
# File 'lib/brut/cli/apps/new/segments/sidekiq.rb', line 34

def add!
  operations = copy_files(@templates_dir, @project_root) + 
               other_operations

  operations.each do |operation|
    operation.call
  end
end

#other_operationsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/brut/cli/apps/new/segments/sidekiq.rb', line 55

def other_operations
  [
    Brut::CLI::Apps::New::Ops::AppendToFile.new(
      file: @project_root / "docker-compose.dx.yml",
      content: %{
redis:
  # Change the value to what you are using in production.
  # If you are using actual Redis, change that here.
  image: valkey/valkey:8.1
},
    ),
    Brut::CLI::Apps::New::Ops::AppendToFile.new(
      file: @project_root / "Procfile.development",
      content: "sidekiq: bin/run sidekiq\n"
    ),
    Brut::CLI::Apps::New::Ops::AppendToFile.new(
      file: @project_root / "Procfile.test",
      content: "sidekiq: bin/run sidekiq\n"
    ),
    Brut::CLI::Apps::New::Ops::AppendToFile.new(
      file: @project_root / "Gemfile",
      content: "# Sidekiq is used for background jobs\ngem \"sidekiq\"\n"
    ),
    Brut::CLI::Apps::New::Ops::AppendToFile.new(
      file: @project_root / ".env.development",
      content: %{
# URL of the Redis/ValKey to use for Sidekiq
SIDEKIQ_REDIS_URL=redis://redis:6379/1
# Tells Sidekiq which ENV var to use for the Redis/ValKey URL
REDIS_PROVIDER=SIDEKIQ_REDIS_URL
# Username for basic auth into the Sidekiq Admin UI
SIDEKIQ_BASIC_AUTH_USER=sidekiq
# Passsword for basic auth into the Sidekiq Admin UI
SIDEKIQ_BASIC_AUTH_PASSWORD=password
}
    ),
    Brut::CLI::Apps::New::Ops::AppendToFile.new(
      file: @project_root / ".env.test",
      content: %{
SIDEKIQ_REDIS_URL=redis://redis:6379/2
REDIS_PROVIDER=SIDEKIQ_REDIS_URL
SIDEKIQ_BASIC_AUTH_USER=sidekiq-test
SIDEKIQ_BASIC_AUTH_PASSWORD=password
}
    ),
    Brut::CLI::Apps::New::Ops::InsertIntoFile.new(
      file: @project_root / "bin" / "test-server",
      before_line: "wait",
      content: "bin/run sidekiq &\n",
    ),
    Brut::CLI::Apps::New::Ops::InsertIntoFile.new(
      file: @project_root / "bin" / "setup",
      before_line: "      step \"Installing rspec binstubs\",   exec: \"bundle binstub rspec-core\"",
      content:     "      step \"Installing sidekiq binstubs\", exec: \"bundle binstub sidekiq\""   
    ),
    Brut::CLI::Apps::New::Ops::InsertIntoFile.new(
      file: @project_root / "specs" / "spec_helper.rb",
      before_line: "require \"brut/spec_support\"",
      content: "require \"sidekiq/testing\""
    ),
    Brut::CLI::Apps::New::Ops::InsertIntoFile.new(
      file: @project_root / "config.ru",
      before_line: "bootstrap = Bootstrap.new.bootstrap!",
      content: "require \"sidekiq/web\"\n"
    ),
    Brut::CLI::Apps::New::Ops::InsertIntoFile.new(
      file: @project_root / "config.ru",
      before_line: "  run bootstrap.rack_app",
      content: %{
map "/sidekiq" do
  use Rack::Auth::Basic, "Sidekiq" do |username, password|
    [username, password] == [ENV.fetch("SIDEKIQ_BASIC_AUTH_USER"), ENV.fetch("SIDEKIQ_BASIC_AUTH_PASSWORD")]
  end
  run Sidekiq::Web.new
end
}
    ),
    Brut::CLI::Apps::New::Ops::InsertCodeInMethod.new(
      file: @project_root / "app" / "src" / "app.rb",
      class_name: "App",
      method_name: "initialize",
      code: "@sidekiq_segment = SidekiqSegment.new",
    ),
    Brut::CLI::Apps::New::Ops::InsertCodeInMethod.new(
      file: @project_root / "app" / "src" / "app.rb",
      class_name: "App",
      method_name: "boot!",
      code: "@sidekiq_segment.boot!"
    ),
    Brut::CLI::Apps::New::Ops::InsertCodeInMethod.new(
      file: project_root / "deploy" / "heroku_config.rb",
      class_name: "HerokuConfig",
      method_name: "additional_images",
      class_method: true,
      ignore_if_file_not_found: true,
      code: %{
{
"sidekiq" => {
  cmd: "bin/run sidekiq",
}
}
      },
      where: :end
    ),
  ]
end

#output_post_add_messaging(stdout:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/brut/cli/apps/new/segments/sidekiq.rb', line 12

def output_post_add_messaging(stdout:)
  stdout.puts ""
  stdout.puts "Sidekiq has now been set up for your app. The configuration used is"
  stdout.puts "a basic one, suitable for getting started, however you know own this"
  stdout.puts "configuration.  Most of it is in these files:"
  stdout.puts ""
  stdout.puts "    app/config/sidekiq.yml"
  stdout.puts "    app/src/back_end/segments/sidekiq_segment.rb"
  stdout.puts ""
  stdout.puts "You are encouraged to verify everything is set up as follows:"
  stdout.puts ""
  stdout.puts "1. Quit dx/start, and start it back up - this will downloaded and set up ValKey/Redis"
  stdout.puts "2. Re-run bin/setup. This will install needed gems and create binstubs"
  stdout.puts "3. Run the example integration test:"
  stdout.puts ""
  stdout.puts "   bin/test e2e specs/integration/sidekiq_works.spec.rb"
  stdout.puts ""
  stdout.puts "   This will use the actual Sidekiq server, so if it passes, you should"
  stdout.puts "   all set and can start creating jobs"
  stdout.puts ""
end