#RubyOnRails - 26 April 2018
« Back 1 day Forward 1 day »
[00:51:38] K4L1: Hello, im following the Rails guide and i have a trouble when start the server, can someone here help me? https://imgur.com/a/xJ4TjjX
[02:47:51] fcser__: hmm there must be a simple way… I just want to see or log the current layout name…
[02:58:19] fcser__: ok nm it’s logged by default in console… “Rendering controller_name/action.html.erb” within layouts/layout_name
[08:31:30] dionysus69: I read that it could be set to 6 on 4 core machine, but I don't understand what benefit would that give
[08:34:57] dminuoso: dionysus69: Kind of depends really on your app in the end, but usually you will spend a lot of time waiting on the database.
[08:34:58] dionysus69: I see, so if I set 6 processes on 4 core machine, that sounds about right? with 8 gb ram
[08:35:15] dminuoso: dionysus69: So when those threads block waiting on the database the GVL is released and other threads can run.
[08:35:26] dionysus69: once I will have time to optimize app I am planning to move db on different VM altogether
[08:35:51] dminuoso: dionysus69: That will increase the amount of blocking IO on the database if you move them onto a different VM.
[08:37:49] dminuoso: dionysus69: It's hard to say and depends on what your app does really. I'd recommend you scale up and monitor performance characteristics
[08:38:47] dionysus69: I see, so far I have plenty of available RAM on the machine so I want to make it use more
[08:39:44] dionysus69: hmm ok, I dont really understand the idea of green threads, if they can't run in parallel, can't see application for them :S
[08:40:03] dionysus69: so whether it can be 5 or 16, I dont know what practical difference it would give to the app
[08:41:33] dionysus69: well, process gives a request and next request has to wait until the first one is done
[08:42:20] dionysus69: but if I am having let's say 6 processes/workers, what more can extra threads do? those workers will most likely exhaust IO throughout
[08:42:38] dminuoso: dionysus69: So when you do some network request you have to talk to the kernel.
[08:42:55] dminuoso: dionysus69: You can do this in a blocking fashion, that means your execution thread will be *paused* until the requested data is available
[08:45:00] dminuoso: dionysus69: thread A is executing. Then thread A requests some data from a TCP socket from the kernel (blocking). Now execution takes places in the kernel.
[08:45:13] dminuoso: The kernel sees that the data is not available, so it has to wait. Maybe the data have not been sent over the wire.
[08:45:42] dminuoso: The thread A is then halted, it will not be resumed until the data is available. Now the CPU is idle, so the kernel finds another thread to execute.
[08:46:38] dminuoso: Until thread B makes a call into the kernel again (in which case the kernel will check whether data for `thread A` has arrived. If so, it will resume `thread A`, otherwise it will find another thread to run.
[08:47:27] dminuoso: And this kind of dance is whats happening under your hood tens of thousands of times per second in your computer. =)
[08:48:03] dminuoso: dionysus69: So "blocking IO" essentially means the thread is put to sleep until the data is available. No CPU times are wasted.
[08:48:08] dionysus69: being a web dev is cool, you need to know more than little bit of everything :D
[08:48:37] dminuoso: So if one thread is "waiting on something from the kernel", another ruby thread can run in parallel.
[08:50:05] dionysus69: I was listening Matz on topic of threads and he said it was hard to program real threads, I stopped asking questions about threads after that :D
[08:53:08] dionysus69: ye I imagine it is hard to make operations on data with multiple processes, but that's why it's rewarding too
[08:53:39] tbuehlmann: dionysus69: when using puma, I use n workers for n cores. then just adjust threads to a good number
[08:55:13] dionysus69: in that case I would have to also set the database connection pool number at 16 too btw right?
[08:58:52] dionysus69: btw, if you have more time I have lot more questions, one of them is regarding preload_app! and on_worker_boot do establish_connection
[08:59:56] dionysus69: so let's say I set n workers and m threads, and enable preload_app, I was reading about copy_on_write thing, any more considerations I need to account for?
[09:00:33] dionysus69: will I get data corrupted or data inconsistency between threads or processes or any of that sort?
[09:01:44] tbuehlmann: when using preload_app, puma will load your rails application and then spawn workers. these worker will then use the already loaded application instead of loading it
[09:02:22] dionysus69: I know it deals with cutting memory usage down by sharing it, I am just interested in it because if it creates extra problems, maybe it's not worth dealing with it
[09:02:23] tbuehlmann: that's the basic idea. CoW will save you some memory when loading the application before spawning workers
[09:04:37] dionysus69: it's fine, DO offers lots of memory for cheap xD I am also planning implement lots of caching in app and put a shared redis server among possibly 1+ app servers
[09:07:00] dionysus69: how do you deal with env vars, do you set them in OS or have you moved all of them in 5.2 credentials? even vars like this WEB_CONCURRENCY
[09:07:18] tbuehlmann: a US coworker brought "banana bread" some time ago and it wasn't bread, it was cake. it was a lie all along
[09:08:17] tbuehlmann: dionysus69: depends on your deployment strategy I guess. when using heroku, I prefer environment variables all the way. when deploying via capistrano, config from files all the way
[09:13:07] tbuehlmann: I personally don't use rails credentials yet, since they are not per environment, which is really weird
[09:14:31] dionysus69: I have read an article online that explained how to use them with environment
[09:15:56] dionysus69: yea but instead of going into server and changing config you could change config locally and cap deploy
[09:17:19] tbuehlmann: also, share the master key with your colleagues over some kind of secure channel
[09:34:25] dionysus69: I have specified 5 threads but there are 6, do you know what the sixth one is for ?
[09:36:03] dminuoso: dionysus69: One common case is to provide a bit of elastic behavior when you get clients behind shitty connections.
[12:35:21] dionysus69: anyone got this issue with capistrano/puma ? when I start puma it daemonizes and then cap puma:stop cannot detect puma running, obviously restarts aren't working either
[13:58:25] OyoKooN: Hello! I'm using ActiveStorage and try to downlad a picture attached to a model. When I do (in dev), I get the following error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true. I'm downloading the picture with `Rails.application.routes.url_helpers.rails_blob_path(picture)` where picture is the attachment of my model. How can I make it work?
[13:59:37] broman: can someone please help me? at some point I'm trying to do this (after_action) session[:my_previous_url] = request.env["HTTP_REFERER"] and from another controller, after a submitted form, I'm trying to redirect_to session[:my_previous_url] when it says Cannot redirect to nil!
[14:02:21] broman: OyoKooN, thank you for you reply! I'm still getting the same error: "Cannot redirect to nil!
[14:03:46] OyoKooN: Anyway you should redirect to the session only if it's present and fallback to an other URL otherwise
[14:06:27] OyoKooN: Maybe try to put some logger.debug statements to see what's going on in your methods ;)
[14:08:45] broman: OyoKooN, cannot render console from xxx.xxx.xxx.xxx anything on the configuration file I should change?
[14:10:08] broman: I got it! I was putting it on the wrong controller! Sorry and thank you very much OyoKooN
[14:41:10] hays_: I am calling ActiveRecord::Base.establish_connection in an initialize method of an object. how can I make sure close_connection is called?
[14:43:10] dminuoso: def self.with_handle(&proc); ActiveRecord::Base.establish_connection; proc.call(); ActiveRecord::Base.close_connection; end;
[14:44:59] dminuoso: hays_: Well there must be some time when you "lose" that object. Destroy the connection then.
[14:46:16] dminuoso: hays_: establish_connection is a method of the singleton class of the object AR::Base itself.
[14:47:00] dminuoso: hays_: the concept of "class methods" is a lie designed to ease the transition from other language.
[14:48:56] hays_: so it doesn't make a lot of sense to have an.. erm.. object that isn't a class (not sure what you call that) that invokes this particular method
[14:51:18] dminuoso: point is, `new` is just a message send to `Array`. and there happens to be a _regular_ method behind it :)
[14:51:50] dminuoso: So `Array.new` does the same thing. It checks Array.class, and looks for a method named length
[14:52:33] dminuoso: hays_: The trick now is, every object has a dedicated singleton class. And the method lookup looks inside that singleton class first.
[14:52:37] hays_: does it? or does it check the methods in whatever new spat out to create the array object
[14:53:13] dminuoso: hays_: `def a.b` for any choice of a, will define a method `b` on the singleton class of `a`
[14:53:25] dminuoso: Inside that context `self` is the class object itself. So `def self.bar` defines a method on the singleton class of the class object itself.
[14:55:39] hays_: and maybe it delegates it to the class itself along with some information about the object?
[14:57:44] dminuoso: for method lookup on any receiver, "go one down" (klass), and then follow the chain to the right (super) until you find something that has a method (or method_missing) for the method.
[14:58:04] dminuoso: if found, it will be invoked, while setting `self` to be the receiver inside the method (this is just conceptually)
[14:58:42] hays_: I don't fully understand that, but its interesting. I might need to defer this exploration until later
[14:59:50] dminuoso: hays_: The RAII/bracket pattern allows you to automatically release resources at the correct time.
[15:00:58] hays_: to me, I think it doesn't make much sense to call something like establish_connection when creating a new object
[15:01:43] hays_: well I was given this code to use to connect to a database and retrieve some records
[15:03:23] hays_: and then there is another class that just defines some passthru methods to these classes and has an initialize method that establishes the connection
[15:03:53] dionysus69: ~/.rvm/bin/rvm default do bundle exec puma -C /var/www/user/shared/puma.rb --daemon capistrano or my service starts puma this way, it used to always work
[15:04:05] dminuoso: dionysus69: Your first assumption should be "how did I fuck up". Your second assumption should be "my assumptions are wrong"
[15:04:30] dionysus69: yea but I really didn't change anything, my staging and production both broke
[15:04:53] dionysus69: which makes it even more wierder, now I have production working with rails s until I figure out how did I fuck up :D
[15:05:58] dionysus69: I guess after I deployed one I didnt expect anything wrong so I deployed the second one too
[15:06:08] dminuoso: Happens quite regularly here too. Two servers mysteriously experience the same thing at the same time. It's always radiation.
[15:07:18] sevenseacat: yeah it sounds like a 'i'm running random things to try and fix something I broke'
[15:07:41] dionysus69: I was just adding puma workers plugin to Capfile which has nothing to do with all this, plus I reverted that commit and now it is back to just puma plugin
[15:10:45] dionysus69: which should sound fine, but nginx cant access it 201 connect() failed (111: Connection refused) while connecting to upstream, client
[15:12:22] sevenseacat: did the reverted commit actually revert all of the changes of adding the plugin?
[15:13:07] dionysus69: yes, even if it didn't I restarted puma manually 100 times since then probably
[16:57:58] guacamole: has anyone sucessfully gotten activeadmin to work with json types in postgres?
[17:04:34] IRCtoo: I am getting a "SQLite3::SQLException error about "no such table" and lists the table. This table is an OLD table that was renamed in a migration successfully. This causes ALL tests to throw an error (all errors after this one say undefined method 'each' for nil.NilClass. I have db:drop, db:create, db:migrate includin RAILS_ENV=test and still no avail. I have searched within all files
[17:04:34] IRCtoo: for the table name it references and it isn't listed anywhere except for a migration and it was sugbsequently renamed in a migration. I stopped and restarted spring as well as restarted the server. I looked in the SQLite database and there is no reference in any cell to the table listed after the "no such table". The test itself has nothing to do with this said table. I can run the
[17:04:34] IRCtoo: test only on controllers and only have a single test in the controllers test folder and it still throws it. Any suggestions?
[17:13:58] kapil___: hello i cannot run this functions: http://www.rubydoc.info/gems/gadget/0.7.0/Gadget#columns-class_method
[17:27:29] kapil___: i got it `conn = PG::Connection.open(:dbname => 'sce_smb_01', :password => 'postgres', :user => 'postgres')`
[17:27:48] IRCtoo: guacamole thank you, but I did already restart the server. :o Is it because that table that was renamed is somehow in cache with springer or something with minitest?
[18:01:54] Inside: I have a bunch of roles defined in Active Directory. Does it make sense to use pundit in front of that to perform authorization?
[18:02:13] Inside: It'd be an ability-based authorization based on a role-based authentication system
[18:04:07] Inside: Looks like putting cancan/cancancan in front of LDAP authentication is bad because on every query it goes to check to see if the specific 'can' is enabled
[22:41:31] Cache_Money: I craeted a new model (using rails g model ...) and it created the model file but the migration got kinda messed up (I'm assuming) because I tried to rollback and it started to rollback one of my previous migrations. When I run $ rake db:migrate it's trying to create that model file again and it gets hung up. Any tips on how to fix this?
[23:05:15] helpa: Cache_Money: Show rather than tell. Explaining your problem with code, stacktraces or errors is always preferred to explaining it with just text. Show us what's happening, rather than telling us. Put the code on https://gist.github.com and then give us the HTTP link to the Gist.
[23:18:31] Cache_Money: Radar: Here's the output from the console when I try to run the migration https://gist.github.com/SuperFexy/3601340c137b4202cebb10fe816e677e
[23:18:54] Radar: https://gist.github.com/SuperFexy/3601340c137b4202cebb10fe816e677e#file-console-txt-L13-L16
[23:20:14] Cache_Money: Radar: Because I waited 10 minutes and nothing happened. Usually a migration will finish in seconds
[23:21:16] Cache_Money: Radar: I added the migration here: https://gist.github.com/SuperFexy/3601340c137b4202cebb10fe816e677e
[23:22:19] Cache_Money: I had another terminal tab open with iPython open that was connected to the same database. I closed the connection and that tab and tried running the migration again but no luck
[23:22:33] Radar: Cache_Money: When you're running that migration, what does `logs/development.log` show? Any queries at all?
[23:24:06] Cache_Money: Radar: I added the end of that log file and included the most recent successful migration https://gist.github.com/SuperFexy/3601340c137b4202cebb10fe816e677e
[23:34:29] Cache_Money: Radar: I tried to stop my rails server and restart it and I'm getting this: "A server is already running.". Do you think that has something to do with it?
[23:40:40] Cache_Money: Radar: I'm not sure what was happening, so I closed my terminal and opened a new instance. I was able to successfully start a new rails server and then reverted to a previous commit and the new migration worked. ¯\_(ツ)_/¯