Class: MKBrut::Segments::Sidekiq

Inherits:
Base
  • Object
show all
Defined in:
lib/mkbrut/segments/sidekiq.rb

Overview

Adds Sidekiq to a Brut app

Constant Summary

Constants included from MKBrut

VERSION

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.



4
5
6
7
# File 'lib/mkbrut/segments/sidekiq.rb', line 4

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

Class Method Details

.friendly_nameObject



3
# File 'lib/mkbrut/segments/sidekiq.rb', line 3

def self.friendly_name = "Use Sidekiq to Run Background Jobs"

Instance Method Details

#<=>(other) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mkbrut/segments/sidekiq.rb', line 18

def <=>(other)
  if self.class == other.class
    0
  elsif other.class == MKBrut::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



9
10
11
12
13
14
15
16
# File 'lib/mkbrut/segments/sidekiq.rb', line 9

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

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

#other_operationsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
# File 'lib/mkbrut/segments/sidekiq.rb', line 30

def other_operations
  [
    MKBrut::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
},
    ),
    MKBrut::Ops::AppendToFile.new(
      file: @project_root / "Procfile.development",
      content: "sidekiq: bin/run sidekiq\n"
    ),
    MKBrut::Ops::AppendToFile.new(
      file: @project_root / "Procfile.test",
      content: "sidekiq: bin/run sidekiq\n"
    ),
    MKBrut::Ops::AppendToFile.new(
      file: @project_root / "Gemfile",
      content: "# Sidekiq is used for background jobs\ngem \"sidekiq\"\n"
    ),
    MKBrut::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
}
    ),
    MKBrut::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
}
    ),
    MKBrut::Ops::InsertIntoFile.new(
      file: @project_root / "bin" / "test-server",
      before_line: "wait",
      content: "bin/run sidekiq &\n",
    ),
    MKBrut::Ops::InsertIntoFile.new(
      file: @project_root / "bin" / "setup",
      before_line: "  log \"Installing rspec binstubs\"",
      content: %{log "Installing sidekiq binstubs"
system! "bundle binstub sidekiq"
}
    ),
    MKBrut::Ops::InsertIntoFile.new(
      file: @project_root / "specs" / "spec_helper.rb",
      before_line: "require \"brut/spec_support\"",
      content: "require \"sidekiq/testing\""
    ),
    MKBrut::Ops::InsertIntoFile.new(
      file: @project_root / "config.ru",
      before_line: "bootstrap = Bootstrap.new.bootstrap!",
      content: "require \"sidekiq/web\"\n"
    ),
    MKBrut::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
}
    ),
    MKBrut::Ops::InsertCodeInMethod.new(
      file: @project_root / "app" / "src" / "app.rb",
      class_name: "App",
      method_name: "initialize",
      code: "@sidekiq_segment = SidekiqSegment.new",
    ),
    MKBrut::Ops::InsertCodeInMethod.new(
      file: @project_root / "app" / "src" / "app.rb",
      class_name: "App",
      method_name: "boot!",
      code: "@sidekiq_segment.boot!"
    ),
    MKBrut::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