Elixir: Nebulex (Caching library) error - caching

I'm new to Elixir, trying to use Nebulex for making a simple local cache (Panda.Cache). I followed its tutorial but finally, by doing these commands:
data = %{id: 1, text: "hello"}
Mycache.set(data[:id], data)
I get this error:
** (ArgumentError) argument error
(stdlib) :ets.lookup_element(Panda.Cache, :metadata, 2)
(nebulex) lib/nebulex/adapters/local/metadata.ex:19: Nebulex.Adapters.Local.Metadata.get/1
(nebulex) lib/nebulex/adapters/local.ex:177: Nebulex.Adapters.Local.set/4
(panda) lib/panda/cache.ex:2: Panda.Cache.execute/2
Panda is the name of my Elixir app and Panda.Cache the name of the cache I'm trying to make.
Any help or solution would be appreciated. Thank you in advance.
Update:
Project folders and files are like:
panda
config
config.exs
lib
panda.ex
panda
application.ex
cache.ex
config.exs file:
use Mix.Config
config :panda, Panda.Cache,
adapter: Nebulex.Adapters.Local,
gc_interval: 86_400 # 24 hrs
cache.ex file:
defmodule Panda.Cache do
use Nebulex.Cache, otp_app: :panda
end
application.ex file:
defmodule Panda.Application do
use Application
def start(_type, _args) do
import Supervisor.Spec
children = [
supervisor(Panda.Cache, [])
]
opts = [strategy: :one_for_one, name: Panda.Supervisor]
Supervisor.start_link(children, opts)
end
end
And, how I tried to use the cache in my code:
defmodule Panda do
def mytest do
data = %{id: 1, text: "hello"}
Panda.Cache.set(data[:id], data)
end
end

According to your code in GitHub (https://github.com/ab00zar/FirstElixirCode), in the mix.exs file you are missing the app module, currently it is like this:
def application do
[
extra_applications: [:logger]
]
end
But it should be like this:
def application do
[
extra_applications: [:logger],
mod: {Panda.Application, []}
]
end
Because of this, your app (and supervision tree) it is not being started and the cache (Nebulex) is started as part of your supervision tree. Let me know if that works for you.

I tried the code you posted, but I cannot replicate the error.
When something like this happens I often do the following:
mix deps.clean --all
mix clean
mix deps.get
mix deps.compile
mix compile
and then I try again.
I hope this helps :)

Related

Multiple namespaces on Rails ActiveAdmin issues

I'm using active_admin and having issues setting up multiple namespaces.
Relevant files:
config/initializers/active_admin.rb:
config.load_paths = [File.join(Rails.root, "app", "active_admin")]
config.default_namespace = :superadmin
config.namespace :superadmin do |superadmin|
superadmin.site_title = "superadmin"
superadmin.authentication_method = :authenticate_superadmin_user!
end
config.namespace :admins do |admins|
admins.site_title = "admins"
admins.authentication_method = :authenticate_admin_user!
end
Then I have two subfolders, /admins and /superadmin:
app/active_admin/superadmin/dashboard:
ActiveAdmin.register_page "Dashboard" do
...
app/active_admin/superadmin/product_product.rb:
ActiveAdmin.register Product::Product, as: Product do
...
So far, so good.
Now, I can't seem to figure out how to add a dashboard for admins (or any other pages/models) without it blowing up.
If I go to localhost:3000/admins, I get the error uninitialized constant Admins::DashboardController.
I've tried adding:
app/active_admin/admins/dashboard.rb:
ActiveAdmin.register_page "Dashboard do
...
(which does nothing)
And namespaced:
app/active_admin/admins/dashboard.rb:
ActiveAdmin.register_page "Dashboard, namespace: "admins" do
...
which gives the error:
Invalid route name, already in use: 'admins_root'
You may have defined two routes with the same name using the `:as` option,
or you may be overriding a route already defined by a resource
with the same naming
I've also tried adding the subfolders in the load_paths:
config.load_paths = [
File.join(Rails.root, "app", "active_admin"),
File.join(Rails.root, "app", "active_admin", "admins"),
File.join(Rails.root, "app", "active_admin", "superadmin")
]
Please note that I've looked at the following, but none seem to actually work...
http://activeadmin.info/docs/1-general-configuration.html#namespaces
2 Namespaces of activeadmin
How to use multiple Active Admin instances for Complete Separate Models
EDIT:
I've also tried to remove config.default_namespace and the only thing that did was break localhost:3000/superadmin so that it has the error:
uninitialized constant Superadmin::DashboardController

ExecJS: keeping the context between two calls

I'm currently trying to use ExecJS to run Handlebars for one of the product I work on (note: I know the handlebars.rb gem which is really cool and I used it for some times but there is issues to get it installed on Windows, so I try another homemade solution).
One of the problem I'm having is that the Javascript context is not kept between each "call" to ExecJS.
Here the code where I instantiate the #js attribute:
class Context
attr_reader :js, :partials, :helpers
def initialize
src = File.open(::Handlebars::Source.bundled_path, 'r').read
#js = ExecJS.compile(src)
end
end
And here's a test showing the issue:
let(:ctx) { Hiptest::Handlebars::Context.new }
it "does not keep context properly (or I'm using the tool wrong" do
ctx.js.eval('my_variable = 42')
expect(ctx.js.eval('my_variable')).to eq(42)
end
And now when I run it:
rspec spec/handlebars_spec.rb:10 1 ↵
I, [2015-02-21T16:57:30.485774 #35939] INFO -- : Not reporting to Code Climate because ENV['CODECLIMATE_REPO_TOKEN'] is not set.
Run options: include {:locations=>{"./spec/handlebars_spec.rb"=>[10]}}
F
Failures:
1) Hiptest::Handlebars Context does not keep context properly (or I'm using the tool wrong
Failure/Error: expect(ctx.js.eval('my_variable')).to eq(42)
ExecJS::ProgramError:
ReferenceError: Can't find variable: my_variable
Note: I got the same issue with "exec" instead of "eval".
That is a silly example. What I really want to do it to run "Handlebars.registerPartial" and later on "Handlebars.compile". But when trying to use the partials in the template it fails because the one registered previously is lost.
Note that I've found a workaround but I find it pretty ugly :/
def register_partial(name, content)
#partials[name] = content
end
def call(*args)
#context.js.call([
"(function (partials, helpers, tmpl, args) {",
" Object.keys(partials).forEach(function (key) {",
" Handlebars.registerPartial(key, partials[key]);",
" })",
" return Handlebars.compile(tmpl).apply(null, args);",
"})"].join("\n"), #partials, #template, args)
end
Any idea on how to fix the issue ?
Only the context you create when you call ExecJS.compile is preserved between evals. Anything you want preserved needs to be part of the initial compile.

Replace gevent.spawn_link_exception

This old code makes a call to gevent.spawn_link_exception which no longer exists:
def start(self, checkpoint=None):
for gl in self._greenlets:
gl.kill()
self.load_config()
self._greenlets = [
gevent.spawn_link_exception(self.periodic_checkpoint, 5) ]
for master_uri in self._config:
self._greenlets.append(
gevent.spawn_link_exception(
self.replicate, master_uri, checkpoint))
Please help me to update this code so that it works with the latest version of gevent. Thanks.
You can approach the same thing via Greenlet.link_exception method. Here is your modified example:
def start(self, checkpoint=None):
for gl in self._greenlets:
gl.kill()
self.load_config()
def exception_callback(greenlet):
print "Exception happened in ", greenlet
self._greenlets = [gevent.spawn(self.periodic_checkpoint, 5)]
self._greenlets[-1].link_exception(exception_callback)
for master_uri in self._config:
self._greenlets.append(gevent.spawn(self.replicate, master_uri, checkpoint))
self._greenlets[-1].link_exception(exception_callback)

Ruby: add new fields to YAML file

I've been searching around for a bit and couldn't find anything that really helped me. Especially because sometimes things don't seem to be consistant.
I have the following YAML that I use to store data/ configuration stuff:
---
global:
name: Core Config
cfg_version: 0.0.1
databases:
main_database:
name: Main
path: ~/Documents/main.reevault
read_only: false
...
I know how to update fields with:
cfg = YAML.load_file("test.yml")
cfg['global']['name'] = 'New Name'
File.open("test.yml", "w"){ |f| YAML.dump(cfg, f) }
And that's essentially everybody on the internet talks about. However here is my problem: I want to dynamically be able to add new fields to that file. e.g. under the "databases" section have a "secondary_db" field with it's own name, path and read_only boolean. I would have expected to do that by just adding stuff to the hash:
cfg['global']['databases']['second_db'] = nil
cfg['global']['databases']['second_db']['name'] = "Secondary Database"
cfg['global']['databases']['second_db']['path'] = "http://someurl.remote/blob/db.reevault"
cfg['global']['databases']['second_db']['read_only'] = "true"
File.open("test.yml", "w"){ |f| YAML.dump(cfg, f) }
But I get this error:
`<main>': undefined method `[]=' for nil:NilClass (NoMethodError)
My question now is: how do I do this? Is there a way with the YAML interface? Or do I have to write stuff into the file manually? I would prefer something via the YAML module as it takes care of formatting/ indentation for me.
Hope someone can help me.
Yo have to initialize cfg['global']['database']['second_db'] to be a hash not nil. Try this cfg['global']['database']['second_db'] = {}

How to mock/stub the config initializer hash in rails 3

Environment : Rails 3.1.1 and Rspec 2.10.1
I am loading all my application configuration through an external YAML file. My initializer (config/initializers/load_config.rb) looks like this
AppConfig = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV]
And my YAML file sits under config/config.yml
development:
client_system: SWN
b2c_agent_number: '10500'
advocacy_agent_number: 16202
motorcycle_agent_number: '10400'
tso_agent_number: '39160'
feesecure_eligible_months_for_monthly_payments: 1..12
test:
client_system: SWN
b2c_agent_number: '10500'
advocacy_agent_number: 16202
motorcycle_agent_number: '10400'
tso_agent_number: '39160'
feesecure_eligible_months_for_monthly_payments: 1..11
And I access these values as, For example AppConfig['feesecure_eligible_months_for_monthly_payments']
In one of my tests I need AppConfig['feesecure_eligible_months_for_monthly_payments'] to return a different value but am not sure how to accomplish this. I tried the following approach with no luck
describe 'monthly_option_available?' do
before :each do
#policy = FeeSecure::Policy.new
#settlement_breakdown = SettlementBreakdown.new
#policy.stub(:settlement_breakdown).and_return(#settlement_breakdown)
#date = Date.today
Date.should_receive(:today).and_return(#date)
#config = mock(AppConfig)
AppConfig.stub(:feesecure_eligible_months_for_monthly_payments).and_return('1..7')
end
.....
end
In my respective class I am doing something like this
class Policy
def eligible_month?
eval(AppConfig['feesecure_eligible_months_for_monthly_payments']).include?(Date.today.month)
end
....
end
Can someone please point me in the right direction!!
The method that is being called when you do AppConfig['foo'] is the [] method, which takes one argument (the key to retrieve)
Therefore what you could do in your test is
AppConfig.stub(:[]).and_return('1..11')
You can use with to setup different expectations based on the value of the argument, ie
AppConfig.stub(:[]).with('foo').and_return('1..11')
AppConfig.stub(:[]).with('bar').and_return(3)
You don't need to setup a mock AppConfig object - you can stick your stub straight on the 'real' one.

Resources