Start redis server using gulp - redis

Most of our front end development workflow is automated using gulp tasks. We're wondering if there is a way to create a gulp task for starting redis.
Currently we're using redis-server which we launch with redis-server. We'd like to be able do something like: gulp redis. What would this entail?

you could spawn a child process that starts up redis (this basically just runs the bash command used to start up your redis instance, so you can add different options to it as well - like you would if you start it from your terminal):
var gulp = require('gulp');
var child_process = require('child_process');
gulp.task('redis-start', function() {
child_process.exec('redis-server', function(err, stdout, stderr) {
console.log(stdout);
if (err !== null) {
console.log('exec error: ' + err);
}
});
});

If you are using OS X you can install redis through Homebrew:
brew install redis
and adjust it to start during OS startup as described in Homebrew formula:
To have launchd start redis at login:
ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
Then to load redis now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
I think this is better and easy then invent different spikes for start/stop redis use Glup.

Related

Collecting Coverage Reports in Jest with Integration Tests

I have a bunch of tests setup with Jest that test Express server endpoints in the same repo. In order to accomplish this testing, I have Jest spinning up an Express server in the beforeAll() method. When I run Jest with the --coverage flag, I get coverage information for just the scripts that were run in order to start Jest and no reporting on the scripts that were triggered by hitting the endpoints. That makes sense, but is there a way to get coverage information on the endpoint code?
Snippet of test code:
const rp = require('request-promise')
describe('testFunctions', () => {
beforeAll(done => {
app.listen(config.PORT, async () => {
done()
})
})
it('hit endpoint', async () => {
const response = await rp({ uri: '/testFunctions', method: 'POST' })
expect(response).toEqual('response)
})
})
I'm trying to get a coverage report for all of the server code hit with the /testFunctions request.
This is the solution that worked for me. It required a bit of refactoring, but I think it was cleaner in the end. In my ecosystem, we are running a Parse Server as middleware with an Express server, but this could apply to anyone who needs to run a separate server with the tests.
So to get my server in a place that nyc (the coverage reporting tool I'm using) could monitor, I abstracted the server initialization completely out of the jest test suite and created a special npm script to run it in an entirely separate process.
NPM Script (package.json):
"scripts": {
"coverage": "nyc --silent npm start & ./listen_on_port_5000.sh && jest test/cloud/integration && kill $(lsof -t -i tcp:5000) && nyc report --reporter=html",
}
listen_on_port_5000.sh
while ! nc -z localhost 5000
do
sleep 0.5
done
So how does this work?
nyc --silent npm start runs which is the normal command we would run to start our server with Express and Parse but with the prepended nyc part with the --silent flag so that nyc can run in the background and watch the server code.
Because we know that the server always starts on port 5000, we run the start script in the background and start a separate process running a shell script (listen_on_port_5000.sh) to wait for the server to boot up.
Once the listener script detects anything running on port 5000, the npm script moves onto the jest command (all while the Express server is up and running).
When Jest finishes running, the final script runs a kill script to close the server running on port 5000.
We then run a second nyc script to generate the report that it collected in the first script. The generated report can be found in your project's directory under /coverage/lcov-report/ (or you can use a different coverage reporter.

serverless-dynamodb-local web shell error

First time trying out serverless framework.
Am trying to use the local web shell to do some inspection.
But realise I couldn't list tables or show a list of records.
Web shell example:
var params = {
TableName: 'stocks-table-dev',
};
dynamodb.scan(params, function(err, data) {
if (err) ppJson(err); // an error occurred
else ppJson(data); // successful response
});
The above command throws a status code 413 error.
But aws cli works fine: aws dynamodb scan --table-name=stocks-table-dev --endpoint-url='http://localhost:8000'
I start the web shell with the command sls dynamodb start.
Prior to that, I used the following commands to install the plugin:
npm install --save-dev serverless-dynamodb-local
sls dynamodb install
Am I suppose to use the web shell for inspection?
Is there some configuration to be done for the web shell to work?

How to run a Redis server AND another application inside Docker?

I created a Django application which runs inside a Docker container. I needed to create a thread inside the Django application so I used Celery and Redis as the Celery Database.
If I install redis in the docker image (Ubuntu 14.04):
RUN apt-get update && apt-get -y install redis-server
RUN pip install redis
The Redis server is not launched: the Django application throws an exception because the connection is refused on the port 6379. If I manually start Redis, it works.
If I start the Redis server with the following command, it hangs :
RUN redis-server
If I try to tweak the previous line, it does not work either :
RUN nohup redis-server &
So my question is: is there a way to start Redis in background and to make it restart when the Docker container is restarted ?
The Docker "last command" is already used with:
CMD uwsgi --http 0.0.0.0:8000 --module mymodule.wsgi
RUN commands are adding new image layers only. They are not executed during runtime. Only during build time of the image.
Use CMD instead. You can combine multiple commands by externalizing them into a shell script which is invoked by CMD:
CMD start.sh
In the start.sh script you write the following:
#!/bin/bash
nohup redis-server &
uwsgi --http 0.0.0.0:8000 --module mymodule.wsgi
When you run a Docker container, there is always a single top level process. When you fire up your laptop, that top level process is an "init" script, systemd or the like. A docker image has an ENTRYPOINT directive. This is the top level process that runs in your docker container, with anything else you want to run being a child of that. In order to run Django, a Celery Worker, and Redis all inside a single Docker container, you would have to run a process that starts all three of them as child processes. As explained by Milan, you could set up a Supervisor configuration to do it, and launch supervisor as your parent process.
Another option is to actually boot the init system. This will get you very close to what you want since it will basically run things as though you had a full scale virtual machine. However, you lose many of the benefits of containerization by doing that :)
The simplest way altogether is to run several containers using Docker-compose. A container for Django, one for your Celery worker, and another for Redis (and one for your data store as well?) is pretty easy to set up that way. For example...
# docker-compose.yml
web:
image: myapp
command: uwsgi --http 0.0.0.0:8000 --module mymodule.wsgi
links:
- redis
- mysql
celeryd:
image: myapp
command: celery worker -A myapp.celery
links:
- redis
- mysql
redis:
image: redis
mysql:
image: mysql
This would give you four containers for your four top level processes. redis and mysql would be exposed with the dns name "redis" and "mysql" inside your app containers, so instead of pointing at "localhost" you'd point at "redis".
There is a lot of good info on the Docker-compose docs
use supervisord which would control both processes. The conf file might look like this:
...
[program:redis]
command= /usr/bin/redis-server /srv/redis/redis.conf
stdout_logfile=/var/log/supervisor/redis-server.log
stderr_logfile=/var/log/supervisor/redis-server_err.log
autorestart=true
[program:nginx]
command=/usr/sbin/nginx
stdout_events_enabled=true
stderr_events_enabled=true

Forever Node.JS Express 4

How do you run the Express 4 app with Forever? (or is there a new package?)
I am running my Express 3 apps with Forever installed locally with the package manager. I use the command:
forever -a start app.js
Try this:
forever start ./bin/www
Let's take a look to package.json:
"scripts": {
"start": "node ./bin/www"
},
I guess when we call npm start, ./bin/www will be executed at some point.
Then look at the content of./bin/www:
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
so we are ready to listen for connections.
forever start --minUptime 1000 --spinSleepTime 1000 ./bin/www
If you use npm start to run your app, this works in place of it:
forever start -c "npm start" /path/to/app/dir/
Source: https://github.com/foreverjs/forever/issues/540
Try node app.js first, for me, I added a new module in code base, but i did not run npm install in my AWS box, forever is not giving you the error, it just stopped silently, but node will give you the error
http://expressjs.com/guide.html
in Expressjs guide doc,
use 'npm start'
I want use 'forever' but can not too
so,
add code at 'app.js'
var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});
and
$node app.js
can use it.
and forever can use too
Feb 2021
This solution is for express.js & forever, on Windows OS.
package.json
"scripts": {
"start": "set PORT=3001 && set DEBUG=myapp:* && forever start --minUptime 1000 --spinSleepTime 1000 ./bin/www",
}
Now run:
npm start
NOTE: For other OS, and customization see following link: https://expressjs.com/en/starter/generator.html

RabbitMQ: Verify version of rabbitmq

How can I verify which version of rabbitmq is running on a server?
Is there a command to verify that rabbitmq is running?
sudo rabbitmqctl status
and look for line that looks like that:
{rabbit,"RabbitMQ","2.6.1"},
You can simply execute from the command line:
sudo rabbitmqctl status | grep rabbit
If rabbitimq can not start I found the only way to determine version is via installer system.
Eample Debian/Ubuntu:
dpkg -s rabbitmq-server | grep Version
If you have no access to rabbitmqctl or rabbitmq-server is not running, on linux do :
ls /usr/lib/rabbitmq/lib/
I got :
rabbitmq_server-3.5.6
As Marek said on a local server, or, on a remote server (using amqplib):
from amqplib import client_0_8 as amqp
import sys
conn = amqp.Connection(host=sys.argv[1], userid="guest", password="guest", virtual_host="/", insist=False)
for k, v in conn.server_properties.items():
print k, v
Save as checkVersion.py and run with python checkVersion.py dev.rabbitmq.com:
% python checkVersion.py dev.rabbitmq.com
information Licensed under the MPL. See http://www.rabbitmq.com/
product RabbitMQ
copyright Copyright (C) 2007-2011 VMware, Inc.
capabilities {}
platform Erlang/OTP
version 2.6.0
On debian systems, you can just run:
dpkg-query --showformat='${Version}' --show rabbitmq-server
To get RabbitMQ version using the .NET/C# RabbitMQ Client Library:
using (var connection = connectionFactory.CreateConnection())
{
if (connection.ServerProperties.ContainsKey("version"))
Console.WriteLine("Version={0}",
Encoding.UTF8.GetString((byte[])connection.ServerProperties["version"]));
}
Output:
Version=3.6.3
In the likely event you're using the "management" (web) plug-in, the RabbitMQ version appears in the upper-right corner of every web page, along with the version of the Erlang run-time.
I use following command to trim output down to version,
rabbitmqctl status | grep "{rabbit,\"RabbitMQ\""
Output:
{rabbit,"RabbitMQ","3.7.3"},
Since I was looking to do this in C# on a Windows machine and all the current answers are for *nix, I'll post the code that I ended up using:
public string GetRabbitMqVersion()
{
string prefix = "rabbitmq_server-";
var dirs = System.IO.Directory.EnumerateDirectories(#"C:\Program Files (x86)\RabbitMQ Server", string.Format("{0}*",prefix));
foreach (var dir in dirs)
{
//Just grab the text after 'rabbitmq_server-' and return the first item found
var i = dir.LastIndexOf(prefix);
return dir.Substring(i+16);
}
return "Unknown";
}
On Centos you can use yum list rabbitmq-server.
Login to management ui and in top right you can find the version. Also use the following command to find the version
# sudo bash
# rabbitmqctl status | grep rabbit