Correct way to close down RabbitMQ and Celery? - django

I'm using Django to call a Selenium script through Celery and RabbitMQ as a message broker.
The problem I'm having is often changes I make to my Selenium script aren't actually changing anything when I rerun my Django server. It sounds weird but I feel like something is still using my old code, not the one with the changes I make.
My guess is that this is something to do with RabbitMQ being kept running while I make changes to my Selenium script? Do I need to 'refresh' it somehow each time I make changes or re-start the process?
Also, I just ctrl+C to close everything when I'm finished, is there a 'proper' way to close down each time?

Related

How to change Mongoid db details in Rails without having to restart the machine

Ok, I got a problem which seems very odd to me. Whenever I change db settings in mongoid.yml file, the changes won't be reflected (like doing a rails c and executing some code like Model.count) until I restart my computer.
Is that a normal behaviour?
In my experience, this kind of problem is often caused by application preloader (like spring) bugging out and not properly reloading changes in application code/config. In case of spring, it is often sufficient to stop it.
spring stop
BUT I've had a few cases where spring would hang up and refuse to stop. In which case I kill it forcefully
killall -9 -m spring
(kill every process with "spring" in its name)

Django / SQLite : Is there a way to see if someone is *currently* logged-in by checking db.sqlite3?

I'm using Django 1.10 with SQLite as my database back-end.
I have a site running with nginx/uwsgi and the above configuration, which I'm constantly updating with new code.
Each time I want to update the site code, I'm shutting down uwsgi, nginx, git pull ing the new version from my repo, then restarting uwsgi and nginx.
While this works, in the sense that the site is updated with the new version of my code, the side-effect is that in the event that a user is currently logged in the site and working on something (which generally will result in modifying the db), the user's work will be interrupted.
Worse still would be the case that the new version of my code contains migrations, which will change the db structure, with (unpredictable?) consequences on the user's on-going work.
So the question is: is there a way, like a command-line script, to check db.sqlite3 and see if a user is currently logged-in, before deciding to shutdown uwsgi and nginx ?
I would say NO. There is chances like
User logged in and didn't log out for a while.
Logged in, but inactive
We can get logged in users who didnt logout in django_session table.
A better approach would be to minimize the downtime. As a start, there's no reason to restart nginx when you update your code or uwsgi, and you can git pull without affecting the currently running code, so you don't need to stop uwsgi before pulling in the new code.
When it comes to migrations, try to avoid any migrations that will break the currently running code. For example, don't delete fields/models that are still used by the current code, but wait until the next update (when those fields/models are no longer in use). That way you can run your migrations while you're still running the old code, without generating any errors.
Next up, you should reload the uwsgi process rather than stopping and starting it manually. This will finish handling any open requests before reloading the process. It will also keep on listening for new requests, so that these can be handled immediately after the process has reloaded. Users may experience a slowdown, but this will not drop any requests unless the queue fills up before the process can be reloaded. For small-scale websites this should never happen.
So, to avoid downtime you can use this to update your website:
$ git pull # pull in new code while the old process is still running
$ python manage.py migrate # run your migrations, and possibly `collectstatic` etc.
$ kill -HUP `cat <pidfile>` # gracefully reload the uwsgi process

Ember-cli: Stop or restart server

Is there a quick way to stop or restart the server in ember-cli? Specially, when you've hit an error and need to restart it. Currently, I just close the window and reopen it, but that gets tedious when I'm making large changes to my Brocfile.js and such. Thanks.
I think it's important to note that This has nothing to do with ember.js or ember CLI. This is an issue with your terminal.
control + C
will terminate the current process in your terminal shell.
If it doesn't, then you should be checking your OS, or the type of terminal you are using.

Does django project has time delay if i modify some codes?

I was wondering that i always got the wrong message when i corrected my code, but not for long, it would return the right result of my program. Is that normal?
Sounds like you are not restarting the built-in django development server manually after making the changes:
The development server automatically reloads Python code for each
request, as needed. You don’t need to restart the server for code
changes to take effect. However, some actions like adding files don’t
trigger a restart, so you’ll have to restart the server in these
cases.
As documentation says, sometimes in order to see the changes, you need to restart the server manually.
Also, sometimes Django dev server reloader doesn't see the changes right away and it takes some time for the server to notice changes and trigger restart. If you see this often, restart the server manually.
Also note that in Django 1.7 kernel signals are used to autoreload the server on linux - this should make it pick up the changes and restart faster:
Changed in Django 1.7:
If you are using Linux and install pyinotify, kernel signals will be
used to autoreload the server (rather than polling file modification
timestamps each second). This offers better scaling to large projects,
reduction in response time to code modification, more robust change
detection, and battery usage reduction.

Updating live server from VCS

I run all my Django sites as SCGI daemons. I won't get into the fundamentals of why I do this but this means that when a site is running, there is a set of processes running from the following command:
/websites/website-name/manage.py runfcgi method=threaded host=127.0.0.1 port=3036 protocol=scgi
All is fine until I want to roll out a new release from the VCS (Bazaar in my case). I made an alias script called up that does the following:
alias up='bzr up; killall manage.py'
It is this generic for one simple reason: I'm lazy. I want one command that I can use under any site to update it. I'm logged into the server most of the time anyway, so, I just hop into the root of the right site and call up. Site updates from BZR and restarts.
The first downside of this is it kills all the manage.py processes on the machine. Currently 6 sites and growing rapidly. The second (and potentially worse -- at least for end-users) is it's a severely non-graceful restart. If somebody was uploading an image or doing something else with a long connection time, their request would just die on the vine.
So what I'm looking for is suggestions for a single method that:
Is generic for lazy people like me (eg I can run it from any site root without having to remember which command I need to call; 'up' is perfect in name.
Only kills the current site. I'm only updating the current site, so only this one should die.
Does the restart in a graceful manner. If possible, it should wait until there are no more active connections. I've no idea how feasible this is.
Instead of killing everything with manage.py in the name, could you write a script for each site that only kills manage.py processes from that site? (Edit: just write the scripts and put them in the root of each site (which you cd to anyway) and run those – still only one command to remember)
I don't know enough about SCGI or Bazaar to suggest much more than that... My method (I'm lazy too) uses Mercurial and Fabric for deployment: http://stevelosh.com/blog/entry/2009/1/15/deploying-site-fabric-and-mercurial/ – maybe it will give you an idea you can use?