I have a simple application that works with the twitter stream api, sidekiq, unicorn and sinatra. All works well, instead of... the job are not being processed at all. They are stuck in Enqueued (And I know that from the sidekiq web UI).
This is my code:
Procfile:
sidekiq: bundle exec sidekiq -C config/sidekiq.yml -e development -r ./lib/tweet_streamer.rb
unicorn: bundle exec unicorn -c config/unicorn.rb
redis: redis-stable/src/redis-server
config/unicorn.rb:
listen 5000, :tcp_nopush => true
timeout 30
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true
before_fork do |server, worker|
end
after_fork do |server, worker|
end
the tweet_streamer.rb:
require_relative "../config/tweetstream"
require_relative "workers"
class TweetStreamer
client = TweetStream::Client.new
client.on_enhance_your_calm do |s|
puts "on_enhance_your_calm #{s}"
end
puts "onstream"
client.userstream do |status|
##
## TODO: Some kind of log
##
puts "start process"
# TweetStatusWorker.perform_async(status.to_hash)
Sidekiq::Client.push('class' => TweetStatusWorker, 'args' => ["a"])
puts "process started!"
end
end
the workers.rb
require_relative "../config/sidekiq"
require_relative 'workers/tweet_status_worker'
the config/sidekiq.rb:
require 'sidekiq'
Sidekiq.configure_client do |config|
config.redis = { :size => 1 }
end
Sidekiq.configure_server do |config|
config.redis = { :size => 6 }
end
the worker:
class TweetStatusWorker
include Sidekiq::Worker
def perform(status)
logger.warn "I'm working on it!"
end
end
My sidekiq.yml:
---
:pidfile: tmp/pids/sidekiq.pid
development:
:verbose: true
:logfile: log/sidekiq_development.log
My config.ru:
require "rubygems"
require "sinatra"
Bundler.require
require File.expand_path '../twitter_module.rb', __FILE__
require 'sidekiq/web'
run Rack::URLMap.new('/' => TwitterModule, '/sidekiq' => Sidekiq::Web)
My app class:
require 'sinatra/base'
class TwitterModule < Sinatra::Base
end
This is the log that I received when I start the application and I make a tweet for start a process and all seems to be fine..
17:36:31 sidekiq.1 | started with pid 65599
17:36:31 unicorn.1 | started with pid 65600
17:36:31 redis.1 | started with pid 65601
17:36:31 redis.1 | [65601] 14 Oct 17:36:31.717 # Warning: no config file specified, using the default config. In order to specify a config file use redis-stable/src/redis-server /path/to/redis.conf
17:36:31 redis.1 | [65601] 14 Oct 17:36:31.718 * Max number of open files set to 10032
17:36:31 redis.1 | _._
17:36:31 redis.1 | _.-``__ ''-._
17:36:31 redis.1 | _.-`` `. `_. ''-._ Redis 2.6.16 (f132ada8/1) 64 bit
17:36:31 redis.1 | .-`` .-```. ```\/ _.,_ ''-._
17:36:31 redis.1 | ( ' , .-` | `, ) Running in stand alone mode
17:36:31 redis.1 | |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
17:36:31 redis.1 | | `-._ `._ / _.-' | PID: 65601
17:36:31 redis.1 | `-._ `-._ `-./ _.-' _.-'
17:36:31 redis.1 | |`-._`-._ `-.__.-' _.-'_.-'|
17:36:31 redis.1 | | `-._`-._ _.-'_.-' | http://redis.io
17:36:31 redis.1 | `-._ `-._`-.__.-'_.-' _.-'
17:36:31 redis.1 | |`-._`-._ `-.__.-' _.-'_.-'|
17:36:31 redis.1 | | `-._`-._ _.-'_.-' |
17:36:31 redis.1 | `-._ `-._`-.__.-'_.-' _.-'
17:36:31 redis.1 | `-._ `-.__.-' _.-'
17:36:31 redis.1 | `-._ _.-'
17:36:31 redis.1 | `-.__.-'
17:36:31 redis.1 |
17:36:31 redis.1 | [65601] 14 Oct 17:36:31.719 # Server started, Redis version 2.6.16
17:36:31 redis.1 | [65601] 14 Oct 17:36:31.728 * DB loaded from disk: 0.010 seconds
17:36:31 redis.1 | [65601] 14 Oct 17:36:31.728 * The server is now ready to accept connections on port 6379
17:36:33 unicorn.1 | I, [2013-10-14T17:36:33.208428 #65600] INFO -- : Refreshing Gem list
17:36:33 unicorn.1 | I, [2013-10-14T17:36:33.813454 #65600] INFO -- : listening on addr=0.0.0.0:5000 fd=9
17:36:33 unicorn.1 | I, [2013-10-14T17:36:33.814790 #65600] INFO -- : master process ready
17:36:33 unicorn.1 | I, [2013-10-14T17:36:33.816138 #65607] INFO -- : worker=0 ready
17:36:33 sidekiq.1 | onstream
17:36:43 sidekiq.1 | start process
17:36:43 sidekiq.1 | process started!
nothing in the sidekiq log obviously:
2013-10-14T16:36:43Z 65599 TID-ouo5je95k INFO: Booting Sidekiq 2.15.1 using redis://localhost:6379/0 with options {:size=>6}
2013-10-14T16:37:32Z 65599 TID-ouo5je95k DEBUG: Terminating 4 actors...
UPDATE:
Ok I found it. Is the streamer the problem. If I run the job outside of the streamer block, this works well. Any Idea about why this append and how I can solve it?
Related
I have troubles with the output of this simple query:
select
pid,
state
from pg_stat_activity
where datname = 'My_DB_name'
while running it different ways:
In IDE
Via running psql in terminal
In bash script:
QUERY="copy (select pid, state from pg_stat_activity where datname = 'My_DB_name') to stdout with csv"
psql -h host -U user -d database -t -c "$QUERY" >> result
1 and 2 return results as I need them:
1:
pid state
------ -----------------------------
23126 idle
25573 active
2642 active
20420 idle
23391 idle
5339 idle
7710 idle
1558 idle
12506 idle
2862 active
716 active
9834 idle in transaction (aborted)
2:
pid | state
-------+-------------------------------
23126 | idle
25573 | idle
2642 | active
20420 | idle
23391 | idle
5339 | active
7710 | idle
1558 | idle
12506 | idle
2211 | active
716 | active
9834 | idle in transaction (aborted)
3 is weird - it doesnt give me any state name except 'active'
23126,
25573,
2642,
20420,
23391,
5339,
7710,
1558,
12506,
1660,active
716,active
1927,active
9834,
What am I missing? How to get all the state names via bash script?
pg_stat_activity is a catalog view that will show different content depending on whether you're logged in as a superuser, or as a non-privileged user.
From your output, it looks like you're logged in as superuser in #1 and #2, but as a normal user in #3.
I have a sinatra app that works great when i start it from the project directory using bundle exec unicorn -c config/unicorn.rb -D. However when i use god to start it, I get error stating unicorn is not part of the bundle. Add it to Gemfile. (Gem::LoadError). I do have unicorn in my gemfile. I have been struggling with it for some time now, without much help. Any clues?
The exact error I get is
/Users/pranav/.rvm/gems/ruby-2.1.5#global/gems/bundler-1.11.2/lib/bundler/rubygems_integration.rb:304:in `block in replace_gem': unicorn is not part of the bundle. Add it to Gemfile. (Gem::LoadError)
from /Users/pranav/.rvm/gems/ruby-2.1.5/bin/unicorn:22:in `<main>'
from /Users/pranav/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `eval'
from /Users/pranav/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `<main>'
Here is the god config I use
proj_dir = ENV['DIR_API_HANDLER']
script_name = 'api_handler'
pid_file = File.join(proj_dir, 'shared', 'pids', 'unicorn.pid')
log_file = File.join(proj_dir, 'shared', 'log', 'unicorn.stderr.log')
God.watch do |w|
w.name = script_name
w.group = 'apihandler'
w.log = log_file
w.dir = proj_dir
w.env = ENV
w.start = "cd #{ proj_dir } && bundle exec unicorn -c config/unicorn.rb -D"
# QUIT gracefully shuts down workers
w.stop = "cd #{ proj_dir} && kill -QUIT `cat #{ pid_file }`"
# USR2 causes the master to re-create itself and spawn a new worker pool
w.restart = "kill -USR2 `cat #{ pid_file }`"
w.start_grace = 10.seconds
w.restart_grace = 15.seconds
w.interval = 30.minutes
w.pid_file = pid_file
w.behavior(:clean_pid_file)
w.start_if do |start|
start.condition(:process_running) do |c|
c.interval = 5.seconds
c.running = false
end
end
w.restart_if do |restart|
restart.condition(:memory_usage) do |c|
c.above = 300.megabytes
c.times = [3, 5] # 3 out of 5 intervals
end
restart.condition(:cpu_usage) do |c|
c.above = 50.percent
c.times = 5
end
end
# lifecycle
w.lifecycle do |on|
on.condition(:flapping) do |c|
c.to_state = [:start, :restart]
c.times = 5
c.within = 5.minute
c.transition = :unmonitored
c.retry_in = 10.minutes
c.retry_times = 5
c.retry_within = 2.hours
end
end
end
My Gemfile looks like
source 'https://rubygems.org'
ruby '2.1.5'
gem 'sinatra'
gem 'sinatra-contrib'
gem 'json'
gem 'unicorn', '~>5.1.0'
gem 'ruby-kafka', '~>0.3.2'
gem 'dotenv'
gem 'curb'
The config/unicorn.rb looks like below
# set path to application
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
working_directory app_dir
# Set unicorn options
worker_processes 2
preload_app false
timeout 30
listen 9001
# Set up socket location
#listen "#{shared_dir}/sockets/unicorn.sock", :backlog => 64
# Logging
stderr_path "#{shared_dir}/log/unicorn.stderr.log"
stdout_path "#{shared_dir}/log/unicorn.stdout.log"
# Set master PID location
pid "#{shared_dir}/pids/unicorn.pid"
The log from god -c config.god -D is below
I [2016-04-22 18:42:37] INFO: Loading octo.god
I [2016-04-22 18:42:37] INFO: Syslog enabled.
I [2016-04-22 18:42:37] INFO: Using pid file directory: /Users/pranav/.god/pids
I [2016-04-22 18:42:37] INFO: Started on drbunix:///tmp/god.17165.sock
I [2016-04-22 18:42:37] INFO: api_handler move 'unmonitored' to 'up'
I [2016-04-22 18:42:37] INFO: api_handler moved 'unmonitored' to 'up'
I [2016-04-22 18:42:37] INFO: api_handler [trigger] process is not running (ProcessRunning)
I [2016-04-22 18:42:37] INFO: api_handler move 'up' to 'start'
I [2016-04-22 18:42:37] INFO: api_handler before_start: no pid file to delete (CleanPidFile)
I [2016-04-22 18:42:37] INFO: api_handler start: cd /Users/pranav/workspace/apihandler && bundle exec unicorn -c config/unicorn.rb -D
W [2016-04-22 18:42:38] WARN: api_handler start command exited with non-zero code = 1
It looks like God is using default location for PID file, while the location is explicitly specified. Could it be because of this?
I get the following error:
eventmachine.rb:534:in `start_tcp_server': no acceptor (port is in use or requires root privileges)
I'm not sure what the process or port is and want to kill it so I can run my script again.
How do I do that?
ps ax | grep ruby
kill -9 [pid]
Example:
ps ax | grep ruby
#=> 857 pts/19 Sl+ 0:04 /home/andreydeineko/.rvm/rubies/ruby-2.2.3/bin/ruby bin/rails server
#=> 14639 pts/18 S+ 0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn rails
#=> 25368 pts/17 Sl+ 0:02 /home/andreydeineko/.rvm/rubies/ruby-2.2.3/bin/ruby bin/rails c
kill -9 857
ps ax | grep ruby
#=> 14639 pts/18 S+ 0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn rails
#=> 25368 pts/17 Sl+ 0:02 /home/andreydeineko/.rvm/rubies/ruby-2.2.3/bin/ruby bin/rails c
I've been looking for a way to run a one time script that loads data into our database. Currently we're using dokku-alt for our development environment and we have a python script that runs to update our schema, data and functions we need available for our application. The problem that I'm facing is trying to find a way to run our script on application deployment through dokku-alt.
I've ventured into using a worker, but the workers themselves don't perform how I would expect them to. From what I've noticed is that a worker will terminate every process once it's complete. This is NOT what we need. We need to run the script once to load up our data and schema and close gracefully. We still want our web process to continue working, so the child process sending a kill signal to the other process.
So my question is, is there a way to run a script just one time on deployment without having to write a custom plugin?
05:23:07 schema.1 | started with pid 15
05:23:07 function.1 | started with pid 17
05:23:07 data.1 | started with pid 19
05:23:07 web.1 | started with pid 21
05:23:07 web.1 | Picked up JAVA_TOOL_OPTIONS: -Xmx384m -Xss512k -Dfile.encoding=UTF-8 -Djava.rmi.server.useCodebaseOnly=true
05:23:12 function.1 | Begin dbupdater
05:23:12 function.1 | pq://user:UcW3P587Eki8Fqrr#postgresql:5432/db
05:23:13 schema.1 | Begin dbupdater
05:23:13 data.1 | Begin dbupdater
05:23:13 data.1 | pq://user:UcW3P587Eki8Fqrr#postgresql:5432/db
05:23:13 schema.1 | pq://user:UcW3P587Eki8Fqrr#postgresql:5432/db
05:23:13 schema.1 | do (AccountCrosstabKey_create.sql)
05:23:13 schema.1 | Done
05:23:13 data.1 | do (Accountinfo_data.sql)
05:23:13 function.1 | do (Connectby_create.sql)
05:23:13 function.1 | Done
05:23:13 data.1 | Done
05:23:13 schema.1 | exited with code 0
05:23:13 system | sending SIGTERM to all processes
05:23:13 function.1 | terminated by SIGTERM
05:23:13 data.1 | terminated by SIGTERM
05:23:13 web.1 | terminated by SIGTERM
Python script:
#!/usr/bin/python
import os
import sys
import glob
import shlex
import subprocess
import postgresql
import postgresql.driver as pg_driver
try:
print('Begin dbupdater')
dbhost = os.environ.get('DATABASE_URL','localhost').replace('postgres://', 'pq://')
print(dbhost)
targetDir = sys.argv[1]
db = postgresql.open(dbhost)
os.chdir(targetDir)
currDir = os.getcwd()
for file in glob.glob("*.sql"):
sqlCmd = ''
with open(file,'r') as myfile:
sqlCmd = myfile.read().replace('DO', '').replace('$do$', '')
db.do('plpgsql',sqlCmd)
print('do (' + file + ')')
db.close()
print('Done')
except (ValueError, KeyError, TypeError) as error:
print (error)
You can execute this, to rum custom python script in dokku instance.
dokku --rm-container run [APP_NAME] python [your_script_name.py]
--rm-container flag delete the container after script finished.
In one of my project i am using Sidekiq
Is there any inbuilt Sidekiq console method/method that helps me to find whether sidekiq is running or not.
My requirement is kind of a pre check condition where if Sidekiq is not running i will raise a error.
I tried using the grep like
'ps -ef | grep sidekiq'
but it's not solving my purpose.
The method i am looking for should be something like:
Sidekiq.is_running?
Thanks in advance.
I also Tried
Sidekiq not running
1.9.3p392 :021 > system 'ps aux | grep sidekiq'
ankitgupta 6683 0.0 0.0 2432768 600 s001 R+ 11:47AM 0:00.00 grep sidekiq
ankitgupta 6681 0.0 0.0 2433432 916 s001 S+ 11:47AM 0:00.01 sh -c ps aux | grep sidekiq
=> true
Sidekiq is running
1.9.3p392 :022 > system 'ps aux | grep sidekiq'
ankitgupta 6725 0.0 0.0 2432768 600 s001 S+ 11:57AM 0:00.00 grep sidekiq
ankitgupta 6723 0.0 0.0 2433432 916 s001 S+ 11:57AM 0:00.00 sh -c ps aux | grep sidekiq
ankitgupta 6707 0.0 1.3 3207416 111608 s002 S+ 11:56AM 0:07.46 sidekiq 2.11.2 project_name [0 of 25 busy]
=> true
It is always returning true.. I want to catch the process when it runs
A little trick:
ps aux | grep '[s]idekiq'
Hope it works
Ideally you can do this directly from ruby itself. Put this in some rake task or standalone script (don't forget to specify Sidekiq connection details)
ps = Sidekiq::ProcessSet.new
ps.size # => 2
ps.each do |process|
p process['busy'] # => 3
p process['hostname'] # => 'myhost.local'
p process['pid'] # => 16131
end
ps.each(&:quiet!) # equivalent to the USR1 signal
ps.each(&:stop!) # equivalent to the TERM signal
From https://github.com/mperham/sidekiq/wiki/API#processes
See this question for how to filter ps output using grep, while eliminating the grep command from the the output.
I see, try this out:
module Process
class << self
def is_running?(pid)
begin
Process.kill(0, pid)
true
rescue Errno::ESRCH
false
end
end
end
end
1.9.3p392 :001 > puts `ps aux | grep -i [s]idekiq`
It'll return you pid like: 12247, and you can check if it's running:
Process.is_running?(12247) // true | false
You can add the following lines to your config/routes.rb
require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'
Start your server, open a browser and check your processess and your jobs
You can find it from Sidekiq Wiki