Elasticsearch: undefined method `params_encoder' for {"timeout"=>300, :proxy=>nil}:Hash - ruby-on-rails

I have install Elasticsearch inside docker.
I got response of elastic search like this-
curl -XGET http://localhost:9200
{
"name" : "NXgYHOl",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "_na_",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
Then I configure it in a rails project.
Gemfile
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
app/models
require 'elasticsearch/model'
class Movie < ApplicationRecord
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
end
Movie.import(force: true)
config/initilizers/elasticsearch.rb
config = {
host: "http://localhost:9200",
transport_options: {
request: { timeout: 5 }
}
}
if File.exists?("config/elasticsearch.yml")
config.merge!(YAML.load_file("config/elasticsearch.yml")[Rails.env].symbolize_keys)
end
Elasticsearch::Model.client = Elasticsearch::Client.new(config)
config/elasticsearch.yml
development: &default
host: 'http://localhost:9200'
transport_options:
request:
timeout: !!integer 300
test:
<<: *default
production:
host: 'http://localhost:9200'
transport_options:
request:
timeout: !!integer 300
It gives an error:
undefined method `params_encoder' for {"timeout"=>300, :proxy=>nil}:Hash
I don't understand this error.

It seems that transport_options is not an option according to the official docs of the ruby gem.
It does however mention a request_timeout option, which should work.
So change your config/elasticsearch.yml file to the following:
development: &default
host: 'http://localhost:9200'
request_timeout: !!integer 300
test:
<<: *default
production:
host: 'http://localhost:9200'
request_timeout: !!integer 300

Related

How to add environment variables in config.json?

I am working on a database migration project and it requires me to use sequelize. I initialized sequelize's CLI (using npx sequelize-cli init) that added the config.json file:
config, contains config file, which tells CLI how to connect with database
The config.json file has this object:
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
But I don't want to save my password in a config.json file. I want to use an environmental variable instead. What can I do?
Rename the config.json to config.js, install dotenv and use the below code
require('dotenv').config();
module.exports = {
development: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
host: process.env.DB_HOST,
dialect: process.env.DB_DRIVER
},
test: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
host: process.env.DB_HOST,
dialect: process.env.DB_DRIVER
},
production: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
host: process.env.DB_HOST,
dialect: process.env.DB_DRIVER
}
};
First you have to create a .sequelizerc file in root. Then add the following code there.
var path = require('path')
module.exports = {
'config': path.resolve('config', 'config.js'),
}
Note: Change the path as per your project.
Then you can rename the config.json file to config.js and add the below code there. (As in Tunde's answer.)
require('dotenv').config();
module.exports = {
development: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
host: process.env.DB_HOST,
dialect: process.env.DB_DRIVER
},
test: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
host: process.env.DB_HOST,
dialect: process.env.DB_DRIVER
},
production: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
host: process.env.DB_HOST,
dialect: process.env.DB_DRIVER
}
};

Webpacker::Manifest::MissingEntryError in Authentication::Login#index

I am on windows machine and using Rails 6.0.3.1 . I have installed react using command rails webpacker:install:react. i have started webpack-dev-server successfully and it is compiling all the files as shown below.
But whenever i am refreshing my page http://localhost:3000/login i am getting below error.
Webpacker can't find hello_react.jsx in C:/Users/sanjay.salunkhe/SellAnything/public/packs/manifest.json. Possible causes:
1. You want to set webpacker.yml value of compile to true for your environment
unless you are using the `webpack -w` or the webpack-dev-server.
2. webpack has not yet re-run to reflect updates.
3. You have misconfigured Webpacker's config/webpacker.yml file.
4. Your webpack configuration is not creating a manifest.
Your manifest contains:
{
"application.js": "/packs/js/application-8b57fc2151de01963297.js",
"application.js.map": "/packs/js/application-8b57fc2151de01963297.js.map",
"entrypoints": {
"application": {
"js": [
"/packs/js/application-8b57fc2151de01963297.js"
],
"js.map": [
"/packs/js/application-8b57fc2151de01963297.js.map"
]
},
"hello_react": {
"js": [
"/packs/js/hello_react-4532b032a8e50c80f4dc.js"
],
"js.map": [
"/packs/js/hello_react-4532b032a8e50c80f4dc.js.map"
]
}
},
"hello_react.js": "/packs/js/hello_react-4532b032a8e50c80f4dc.js",
"hello_react.js.map": "/packs/js/hello_react-4532b032a8e50c80f4dc.js.map"
}
See my Below webpacker.yml file content
# Note: You must restart bin/webpack-dev-server for changes to take effect
default: &default
source_path: app/javascript
source_entry_path: packs
public_root_path: public
public_output_path: packs
cache_path: tmp/cache/webpacker
check_yarn_integrity: false
webpack_compile_output: true
# Additional paths webpack should lookup modules
# ['app/assets', 'engine/foo/app/assets']
resolved_paths: []
# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false
# Extract and emit a css file
extract_css: false
static_assets_extensions:
- .jpg
- .jpeg
- .png
- .gif
- .tiff
- .ico
- .svg
- .eot
- .otf
- .ttf
- .woff
- .woff2
extensions:
- .mjs
- .js
- .jsx
- .sass
- .scss
- .css
- .module.sass
- .module.scss
- .module.css
- .png
- .svg
- .gif
- .jpeg
- .jpg
development:
<<: *default
compile: true
# Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules
check_yarn_integrity: true
# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
https: false
host: localhost
port: 3035
public: localhost:3035
hmr: false
# Inline should be set to true if using HMR
inline: true
overlay: true
compress: true
disable_host_check: true
use_local_ip: false
quiet: false
pretty: false
headers:
'Access-Control-Allow-Origin': '*'
watch_options:
ignored: '**/node_modules/**'
test:
<<: *default
compile: false
# Compile test packs to a separate directory
public_output_path: packs-test
production:
<<: *default
# Production depends on precompilation of packs prior to booting for performance.
compile: false
# Extract and emit a css file
extract_css: true
# Cache manifest.json for performance
cache_manifest: true
see below babel.config.js file content.
module.exports = function(api) {
var validEnv = ['development', 'test', 'production']
var currentEnv = api.env()
var isDevelopmentEnv = api.env('development')
var isProductionEnv = api.env('production')
var isTestEnv = api.env('test')
if (!validEnv.includes(currentEnv)) {
throw new Error(
'Please specify a valid `NODE_ENV` or ' +
'`BABEL_ENV` environment variables. Valid values are "development", ' +
'"test", and "production". Instead, received: ' +
JSON.stringify(currentEnv) +
'.'
)
}
return {
presets: [
isTestEnv && [
'#babel/preset-env',
{
targets: {
node: 'current'
},
modules: 'commonjs'
},
'#babel/preset-react'
],
(isProductionEnv || isDevelopmentEnv) && [
'#babel/preset-env',
{
forceAllTransforms: true,
useBuiltIns: 'entry',
corejs: 3,
modules: false,
exclude: ['transform-typeof-symbol']
}
],
[
'#babel/preset-react',
{
development: isDevelopmentEnv || isTestEnv,
useBuiltIns: true
}
]
].filter(Boolean),
plugins: [
'babel-plugin-macros',
'#babel/plugin-syntax-dynamic-import',
isTestEnv && 'babel-plugin-dynamic-import-node',
'#babel/plugin-transform-destructuring',
[
'#babel/plugin-proposal-class-properties',
{
loose: true
}
],
[
'#babel/plugin-proposal-object-rest-spread',
{
useBuiltIns: true
}
],
[
'#babel/plugin-transform-runtime',
{
helpers: false,
regenerator: true,
corejs: false
}
],
[
'#babel/plugin-transform-regenerator',
{
async: false
}
],
isProductionEnv && [
'babel-plugin-transform-react-remove-prop-types',
{
removeImport: true
}
]
].filter(Boolean)
}
}
I am not sure why i am getting error Webpacker can't find hello_react.jsx in C:/Users/sanjay.salunkhe/SellAnything/public/packs/manifest.json . can someone please help me?
I found issue in my application.html.erb file. i was calling hello_react file with extension .jsx as shown below.
<%= javascript_pack_tag 'application', 'hello_react.jsx' %>
changing it to below works.
<%= javascript_pack_tag 'application', 'hello_react' %>

Why does Searchkick::ImportError - "type"=>"unavailable_shards_exception", "reason"=> "Primary Shard is not active" occur?

I am using searchkick gem in Rails 5.2.2 and trying to Index the User model through rails console. User.reindex is returning this error:
Searchkick::ImportError ({"type"=>"unavailable_shards_exception", "reason"=>"[users_production_20191118071025562][3] primary shard is not active Timeout: [1m], request: [BulkShardRequest [[users_production_20191118071025562][3]] containing [52] requests]"} on item with id '1')
This is my config/initializers/searchkick.rb initializer file:
url = begin
ENV['ELASTICSEARCH_URL']
rescue StandardError
"localhost"
end
Searchkick.client = Elasticsearch::Client.new(
hosts: ["http://#{url}"],
retry_on_failure: true,
transport_options: {
request: {
timeout: 450
}
}
)
Output of curl -XGET "localhost:9200"
"name" : "RZdkAgz",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "PG5iZcMnQB677DBW2jStuA",
"version" : {
"number" : "6.7.0",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "8453f77",
"build_date" : "2019-03-21T15:32:29.844721Z",
"build_snapshot" : false,
"lucene_version" : "7.7.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
Output of curl -XGET "localhost:9200/_cluster/health?pretty"
"cluster_name" : "elasticsearch",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 20,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 0.0
}
One reason can be that, the node has just joined the cluster, but it hasn’t been assigned any shards. Refer this post for more information: Unassigned Shards

TypeORM connection provider as Connection class

Is it possible to use connection class as provide like here?
import { Connection, createConnection } from 'typeorm';
export const databaseProviders = [{
provide: Connection,
useFactory: async () => await createConnection({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'postgres',
database: 'testo',
entities: [
__dirname + '/../**/*.entity{.ts,.js}',
],
logging: true,
synchronize: true,
}),
}];
To make imports work like:
constructor(
private connection: Connection,
) {
this.repository = connection.getRepository(Project);
}
In that case nestjs can't find dependency. I guess the problem is in typeorm, it is compiled to plain es5 function. But maybe there a solution for this?
repository to reproduce
UPDATE:
I found acceptable solution nestjs typeorm module, but don't understand why my Connection class is not worked, but it works well with strings. Hope #kamil-myśliwiec will help to understand.
modules: [
TypeOrmModule.forRoot(
[
Build,
Project,
],
{
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'postgres',
database: 'testo',
entities: [
Build,
],
logging: true,
synchronize: true,
}),
],
// And the n inject like this by entity name
#InjectRepository(Build) private repository: Repository<Build>,

Issues with redis-rails and connection_pool

How I can initialize my redis_store to have connection pooling. I want to add pooling attributes { pool_size: 10, pool_timeout: 10 } I have added connection_pool gem
Example::Application.config.session_store :redis_store,
servers: { host: 'localhost',
port: 6379,
db: 0,
namespace: 'session'
},
expires_in: 25.hours,
It gives error on server boot. If I add something like below;
Example::Application.config.session_store :redis_store,
servers: { host: 'localhost',
port: 6379,
db: 0,
namespace: 'session'
},
expires_in: 25.hours,
{ pool_size: 10, pool_timeout: 10 }
After I'd executed your code, I've run into a simple SyntaxError. Might be your problem, since the correct specifying of the options should be:
Example::Application.config.session_store :redis_store,
servers: { host: 'localhost',
port: 6379,
db: 0,
namespace: 'session'
},
expires_in: 25.hours,
pool_size: 10,
pool_timeout: 10

Resources