tomo-plugin-sidekiq 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f3303d6ffcaaa101d28fd1e3febfae1c6b7c8e9cfff5940bfc8317b1a38ca8bb
4
+ data.tar.gz: ef8a62f99fb77781510bec581b9f3d72b5272c42869695c68e5c75c3936a5473
5
+ SHA512:
6
+ metadata.gz: c567215b96ffc0f723ab93532f3ef167fd4904132e00d693bdb0f5fe997746fb3e5f08e6c6973bb21bc164686df9e2fe6a86f4668cfb3f6586f710844634b993
7
+ data.tar.gz: 1fac066c500134c43872d0db603f8be576d8393ac3fa747c124c166e4f7162b29fc5dd08a167b907dc579551a70b45ca77e1e1a36aafa96bb37b5712119a5df0
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Matt Brictson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,158 @@
1
+ # tomo-plugin-sidekiq
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/tomo-plugin-sidekiq.svg)](https://rubygems.org/gems/tomo-plugin-sidekiq)
4
+ [![Travis](https://img.shields.io/travis/mattbrictson/tomo-plugin-sidekiq.svg?label=travis)](https://travis-ci.org/mattbrictson/tomo-plugin-sidekiq)
5
+ [![Circle](https://circleci.com/gh/mattbrictson/tomo-plugin-sidekiq.svg?style=shield)](https://circleci.com/gh/mattbrictson/tomo-plugin-sidekiq)
6
+ [![Code Climate](https://codeclimate.com/github/mattbrictson/tomo-plugin-sidekiq/badges/gpa.svg)](https://codeclimate.com/github/mattbrictson/tomo-plugin-sidekiq)
7
+
8
+ This is a [tomo](https://github.com/mattbrictson/tomo) plugin that provides tasks for managing [sidekiq](https://github.com/mperham/sidekiq) via [systemd](https://en.wikipedia.org/wiki/Systemd), based on the recommendations in the sidekiq documentation. This plugin assumes that you are also using the tomo `rbenv` and `env` plugins, and that you are using a systemd-based Linux distribution like Ubuntu 18 LTS.
9
+
10
+ ---
11
+
12
+ - [Installation](#installation)
13
+ - [Settings](#settings)
14
+ - [Tasks](#tasks)
15
+ - [Support](#support)
16
+ - [License](#license)
17
+ - [Code of conduct](#code-of-conduct)
18
+ - [Contribution guide](#contribution-guide)
19
+
20
+ ## Installation
21
+
22
+ Run:
23
+
24
+ ```
25
+ $ gem install tomo-plugin-sidekiq
26
+ ```
27
+
28
+ Or add it to your Gemfile:
29
+
30
+ ```ruby
31
+ gem "tomo-plugin-sidekiq"
32
+ ```
33
+
34
+ Then add the following to `.tomo/config.rb`:
35
+
36
+ ```ruby
37
+ plugin "sidekiq"
38
+
39
+ setup do
40
+ # ...
41
+ run "sidekiq:setup_systemd"
42
+ end
43
+
44
+ deploy do
45
+ # ...
46
+ # Place this task at *after* core:symlink_current
47
+ run "sidekiq:restart"
48
+ end
49
+ ```
50
+
51
+ ### enable-linger
52
+
53
+ This plugin installs sidekiq as a user-level service using systemctl --user. This allows sidekiq to be installed, started, stopped, and restarted without a root user or sudo. However, when provisioning the host you must make sure to run the following command as root to allow the sidekiq process to continue running even after the tomo deploy user disconnects:
54
+
55
+ ```
56
+ # run as root
57
+ $ loginctl enable-linger <DEPLOY_USER>
58
+ ```
59
+
60
+ ## Settings
61
+
62
+ | Name | Purpose |
63
+ | --------------------- | ------- |
64
+ | `sidekiq_systemd_service` | Name of the systemd unit that will be used to manage sidekiq <br>**Default:** `"sidekiq_%{application}.service"` |
65
+ | `sidekiq_systemd_service_path` | Location where the systemd unit will be installed <br>**Default:** `".config/systemd/user/%{sidekiq_systemd_service}"` |
66
+ | `sidekiq_systemd_service_template_path` | Local path to the ERB template that will be used to create the systemd unit <br>**Default:** [service.erb](https://github.com/mattbrictson/tomo-plugin-sidekiq/blob/master/lib/tomo/plugin/sidekiq/service.erb) |
67
+
68
+ ## Tasks
69
+
70
+ ### sidekiq:setup_systemd
71
+
72
+ Configures systemd to manage sidekiq. This means that sidekiq will automatically be restarted if it crashes, or if the host is rebooted. This task essentially does two things:
73
+
74
+ 1. Installs a `sidekiq.service` systemd unit
75
+ 1. Enables it using `systemctl --user enable`
76
+
77
+ Note that these units will be installed and run for the deploy user. You can use `:sidekiq_systemd_service_template_path` to provide your own template and customize how sidekiq and systemd are configured.
78
+
79
+ `sidekiq:setup_systemd` is intended for use as a [setup](https://tomo-deploy.com/commands/setup/) task. It must be run before sidekiq can be started during a deploy.
80
+
81
+ ### sidekiq:restart
82
+
83
+ Gracefully restarts the sidekiq service via systemd, or starts it if it isn't running already. Equivalent to:
84
+
85
+ ```
86
+ systemctl --user restart sidekiq.service
87
+ ```
88
+
89
+ ### sidekiq:start
90
+
91
+ Starts the sidekiq service via systemd, if it isn't running already. Equivalent to:
92
+
93
+ ```
94
+ systemctl --user start sidekiq.service
95
+ ```
96
+
97
+ ### sidekiq:stop
98
+
99
+ Stops the sidekiq service via systemd. Equivalent to:
100
+
101
+ ```
102
+ systemctl --user stop sidekiq.service
103
+ ```
104
+
105
+ ### sidekiq:status
106
+
107
+ Prints the status of the sidekiq systemd service. Equivalent to:
108
+
109
+ ```
110
+ systemctl --user status sidekiq.service
111
+ ```
112
+
113
+ ### sidekiq:log
114
+
115
+ Uses `journalctl` (part of systemd) to view the log output of the sidekiq service. This task is intended for use as a [run](https://tomo-deploy.com/commands/run/) task and accepts command-line arguments. The arguments are passed through to the `journalctl` command. For example:
116
+
117
+ ```
118
+ $ tomo run -- sidekiq:log -f
119
+ ```
120
+
121
+ Will run this remote script:
122
+
123
+ ```
124
+ journalctl -q --user-unit=sidekiq.service -f
125
+ ```
126
+
127
+ ## Recommendations
128
+
129
+ ### Sidekiq configuration
130
+
131
+ Add a `config/sidekiq.yml` file to your application (i.e. checked into git) and use that to configure sidekiq, using environment variables as necessary. For example:
132
+
133
+ ```yaml
134
+ ---
135
+ :queues:
136
+ - default
137
+ - mailers
138
+
139
+ :concurrency: <%= ENV.fetch("SIDEKIQ_CONCURRENCY", "1") %>
140
+ ```
141
+
142
+ Now you can tune sidekiq for each environment by simply setting environment variables (e.g. using `tomo run env:set`), without hard-coding configuration in git or within systemd files.
143
+
144
+ ## Support
145
+
146
+ If you want to report a bug, or have ideas, feedback or questions about the gem, [let me know via GitHub issues](https://github.com/mattbrictson/tomo-plugin-sidekiq/issues/new) and I will do my best to provide a helpful answer. Happy hacking!
147
+
148
+ ## License
149
+
150
+ The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
151
+
152
+ ## Code of conduct
153
+
154
+ Everyone interacting in this project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
155
+
156
+ ## Contribution guide
157
+
158
+ Pull requests are welcome!
@@ -0,0 +1,19 @@
1
+ require "tomo"
2
+ require "tomo/plugin/sidekiq/tasks"
3
+ require "tomo/plugin/sidekiq/version"
4
+
5
+ module Tomo
6
+ module Plugin
7
+ module Sidekiq
8
+ extend Tomo::PluginDSL
9
+
10
+ tasks Tomo::Plugin::Sidekiq::Tasks
11
+
12
+ # rubocop:disable Metrics/LineLength
13
+ defaults sidekiq_systemd_service: "sidekiq_%{application}.service",
14
+ sidekiq_systemd_service_path: ".config/systemd/user/%{sidekiq_systemd_service}",
15
+ sidekiq_systemd_service_template_path: File.expand_path("sidekiq/service.erb", __dir__)
16
+ # rubocop:enable Metrics/LineLength
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ [Unit]
2
+ Description=Sidekiq for <%= settings[:application] %>
3
+ After=syslog.target network.target
4
+ ConditionPathExists=<%= paths.current %>
5
+
6
+ [Service]
7
+ ExecReload=/usr/bin/kill -TSTP $MAINPID
8
+ ExecStart=/bin/bash -lc 'exec bundle exec sidekiq'
9
+ Restart=on-failure
10
+ RestartSec=1
11
+ StandardError=syslog
12
+ StandardOutput=syslog
13
+ SyslogIdentifier=%n
14
+ Type=simple
15
+ WorkingDirectory=<%= paths.current %>
16
+
17
+ # Greatly reduce Ruby memory fragmentation and heap usage
18
+ # https://www.mikeperham.com/2018/04/25/taming-rails-memory-bloat/
19
+ Environment=MALLOC_ARENA_MAX=2
20
+
21
+ [Install]
22
+ WantedBy=multi-user.target
@@ -0,0 +1,52 @@
1
+ module Tomo::Plugin::Sidekiq
2
+ class Tasks < Tomo::TaskLibrary
3
+ SystemdUnit = Struct.new(:name, :template, :path)
4
+
5
+ # rubocop:disable Metrics/AbcSize
6
+ def setup_systemd
7
+ linger_must_be_enabled!
8
+
9
+ remote.mkdir_p service.path.dirname
10
+ remote.write template: service.template, to: service.path
11
+
12
+ remote.run "systemctl --user daemon-reload"
13
+ remote.run "systemctl", "--user", "enable", service.name
14
+ end
15
+ # rubocop:enable Metrics/AbcSize
16
+
17
+ %i[reload restart start stop status].each do |action|
18
+ define_method(action) do
19
+ remote.run "systemctl", "--user", action, service.name
20
+ end
21
+ end
22
+
23
+ def log
24
+ remote.attach "journalctl", "-q",
25
+ raw("--user-unit=#{service.name.shellescape}"),
26
+ *settings[:run_args]
27
+ end
28
+
29
+ private
30
+
31
+ def service
32
+ SystemdUnit.new(
33
+ settings[:sidekiq_systemd_service],
34
+ paths.sidekiq_systemd_service_template,
35
+ paths.sidekiq_systemd_service
36
+ )
37
+ end
38
+
39
+ def linger_must_be_enabled!
40
+ loginctl_result = remote.run "loginctl", "user-status", remote.host.user
41
+ return unless loginctl_result.stdout.match?(/^\s*Linger:\s*no\s*$/i)
42
+
43
+ die <<~ERROR.strip
44
+ Linger must be enabled for the #{remote.host.user} user in order for
45
+ sidekiq to stay running in the background via systemd. Run the following
46
+ command as root:
47
+
48
+ loginctl enable-linger #{remote.host.user}
49
+ ERROR
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,7 @@
1
+ module Tomo
2
+ module Plugin
3
+ module Sidekiq
4
+ VERSION = "0.1.0".freeze
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tomo-plugin-sidekiq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Brictson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-10-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tomo
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-ci
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-reporters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '13.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '13.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 0.75.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 0.75.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-performance
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '='
116
+ - !ruby/object:Gem::Version
117
+ version: 1.5.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '='
123
+ - !ruby/object:Gem::Version
124
+ version: 1.5.0
125
+ description:
126
+ email:
127
+ - opensource@mattbrictson.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - LICENSE.txt
133
+ - README.md
134
+ - lib/tomo/plugin/sidekiq.rb
135
+ - lib/tomo/plugin/sidekiq/service.erb
136
+ - lib/tomo/plugin/sidekiq/tasks.rb
137
+ - lib/tomo/plugin/sidekiq/version.rb
138
+ homepage: https://github.com/mattbrictson/tomo-plugin-sidekiq
139
+ licenses:
140
+ - MIT
141
+ metadata:
142
+ bug_tracker_uri: https://github.com/mattbrictson/tomo-plugin-sidekiq/issues
143
+ changelog_uri: https://github.com/mattbrictson/tomo-plugin-sidekiq/releases
144
+ source_code_uri: https://github.com/mattbrictson/tomo-plugin-sidekiq
145
+ homepage_uri: https://github.com/mattbrictson/tomo-plugin-sidekiq
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: 2.4.0
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubygems_version: 3.0.6
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: sidekiq tasks for tomo
165
+ test_files: []