sasa1977 / con_cache

ets based key/value cache with row level isolated writes and ttl support
MIT License
910 stars 71 forks source link

Compatibility with module-based supervisors #47

Closed tominkozhimala closed 6 years ago

tominkozhimala commented 6 years ago

Version information:

Currently attempting to integrate ConCache into my existing project, which uses a module-based supervisor to manage child processes:

defmodule MyService.Supervisor do
  use Supervisor

  def start_link(opts) do
    Supervisor.start_link(__MODULE__, :ok, opts)
  end

  def init(:ok) do
    children = [
      ...
      {ConCache, [name: :cache, ets_options: [:set]]}, id: :cache_1, type: :supervisor)
    ]

    Supervisor.init(children, strategy: :one_for_one)
  end
end

Then, I attempt to call my cache from one of the other children processes in the above Supervision tree. However, my application is crashing with the following error:

The module ConCache was given as a child to a supervisor 
but it does not implement child_spec/1.

...

However, if you don't own the given module and it doesn't implement
child_spec/1, instead of passing the module name directly as a supervisor
child, you will have to pass a child specification as a map:

    %{
      id: ConCache,
      start: {ConCache, :start_link, [arg1, arg2]}
    }

See the Supervisor documentation for more information.

So, I replaced my Supervisor.child_spec/2 function as follows:

...
children = [
  ...
  %{
    id: :cache_1,
    start: {ConCache, :start_link, [[name: :cache, ets_options: [:set]]]},
    type: :supervisor
  }
]

This causes my application to crash with the following error:

[error] Task #PID<0.231.0> started from #PID<0.222.0> terminating
** (stop) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
    (con_cache) lib/con_cache/owner.ex:17: ConCache.Owner.cache/1
    (con_cache) lib/con_cache.ex:149: ConCache.put/3

Is there an existing workaround to get this functioning?

tominkozhimala commented 6 years ago

My first guess is that the child_spec function is being called on ConCache.Owner and failing, since it doesn't exist yet in 0.12.1.

I assume this error wouldn't occur on module-based supervisors once the child_spec changes from earlier this month are released. Any word on when the merged changes will be released as a hex package?

sasa1977 commented 6 years ago

My first guess is that the child_spec function is being called on ConCache.Owner and failing, since it doesn't exist yet in 0.12.1.

I assume this error wouldn't occur on module-based supervisors once the child_spec changes from earlier this month are released. Any word on when the merged changes will be released as a hex package?

Yes, you're right that it doesn't work because the current master has not yet been released. The main reason is because there have been some breaking changes in #42 and #44 which I want to review, and create the changelog. I didn't manage to find the time for this because I was busy with other things (most notably the work on the 2nd edition of Elixir in Action), but I'll make it a priority now, and see about releasing the new version as soon as possible.

In the meantime, you have two options.

The first option is to depend on the GitHub version instead of hex. This will work, but the annoying thing is that the official docs at hexdoc.pm are incorrect when it comes to ttl configuration. You can refer to the latest README for explanation.

Another approach is to use the latest published version, and create your own child_spec. Here's the spec which should work:

Supervisor.start_link(
  [
    %{
      id: :my_cache,
      type: :supervisor,
      start: {ConCache, :start_link, [[], [name: :my_cache]]}
    }
  ],
  strategy: :one_for_one,
  name: Test.Supervisor
)

The reason why you need this particular shape of the :start key is because start_link in 0.12.1 takes two arguments.

I agree that this is all unfortunate and confusing, so I'll make it a priority to release the new version as soon as possible.

sasa1977 commented 6 years ago

The new version (0.13.0) is now published to hex.

tominkozhimala commented 6 years ago

Thanks for the quick turnaround, @sasa1977

Seems to be working as expected now. Just had to add ttl_check_interval: false as an option.