Jack Moore

Email: jack(at)jmoore53.com
Project Updates

Sidekiq, Redis, and Rails

26 Aug 2021 » streaming, rails, redis, sidekiq, poc

With the streaming server, there’s no way I could let the web server do the processing for web requests and also handle the ffmpeg processing for the stream.

Sidekiq/Redis Rails Config

Gemfile:

gem 'sidekiq'
gem 'redis'

config\sidekiq.yml:

development:
  :concurrency: 1

production:
  :concurrency: 1

:queues:
  - default

config\environment.rb Add this line:

config.active_job.queue_adapter = :sidekiq

config\cable.yml

development:
  adapter: redis
  url: <%= ENV.fetch("REDIS_URL") %>/12 %>
  channel_prefix: app_development

test:
  adapter: test

production:
  adapter: redis
  url: <%= ENV.fetch("REDIS_URL") { "redis://ip:6379/1" } %>
  channel_prefix: rails_skeleton_production

config\initializers\redis.rb:

# frozen_string_literal: true

Redis.current = Redis.new(url:  ENV['REDIS_URL'],
                          port: ENV['REDIS_PORT'])

config\initializers\sidekiq.rb:

Sidekiq.configure_client do |config|
    config.redis = { url: ENV['REDIS_URL'], size: 4, network_timeout: 5 }
end

Sidekiq.configure_server do |config|
    config.redis = { url: ENV['REDIS_URL'], size: 4, network_timeout: 5 }
end

config\routes.rb

# Add this require to the beginning
require 'sidekiq/web'

# Mount Sidekiq inside the Routes.draw.do
  mount Sidekiq::Web => '/sidekiq'

Ruby Calling the Job

Generating the worker from the console with:

rails g sidekiq:worker HardWorker

Note setting the queue from where we want to call the job. In this case this is from within StreamController < Application Controller

    def start_stream
        M3u8ConverterWorker.set(queue: 'default').perform_async("/location/to/stream/test0.m3u8","rtmp://localhost:1935/live/test0")
        render json: {started_stream: true, kicked_off_job: true}
    end

Starting Everything Up:

Start Redis.. For me this was starting it on another server

docker run -d --name phss_redis -p 6379:6379 redis

Then from within the phss_redis docker container:

# Enter the container
docker exec -it phss_redis bash

# From within the container
redis-cli

# From within Redis CLI
bind 0.0.0.0
save

# Exit the containers

Spinning up Sidekiq:

REDIS_URL="redis://ip.to.the.redis:6379" bundle exec sidekiq -e production -C config/sidekiq.yml

Spinning up Rails:

REDIS_URL=redis://10.0.5.10:6379 rails s -e production
© Jack Moore - This site was last built Fri 30 Aug 2024 12:31:24 PM EDT