Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detach feature #9

Closed
mediaslave24 opened this issue Jul 28, 2015 · 4 comments
Closed

Detach feature #9

mediaslave24 opened this issue Jul 28, 2015 · 4 comments

Comments

@mediaslave24
Copy link

Hello,

i propose to add "detach" feature, which should be only an option to existing interface.

The rationale for this feature is:

  • No blocking for main thread.
  • Multiple forks.

Usage example with rabbitmq:

require 'iso_latte'
require 'bunny'

Message = Struct.new(:meta, :payload)

Job = Struct.new(:msg) do
  def perform
    $stderr.puts("Performing #{msg.payload.inspect}")
    sleep(rand(5)+1)
    $stderr.puts("Done!")
  end
end

conn = Bunny.new.start
channel = conn.create_channel

# consumer allowed to be sent up to 5 messages without acknowledgements 
channel.prefetch(5)

channel.queue('jobs').subscribe(manual_ack: true) do |_, meta, payload|
  msg = Message.new(meta, payload)

  IsoLatte.fork(detach: true,
                finish: proc { |success,rc| success ? msg.meta.ack : msg.meta.reject }) do
    conn.close
    Job.new(msg).perform
  end
end

In result i'll have worker supervisor, which able to handle upto 5 jobs simultaneously. Each job starts from scratch! no membloats! shitty jobs code protection!

@mediaslave24 mediaslave24 changed the title Add fork detaching Detach feature Jul 28, 2015
@nevinera
Copy link
Contributor

The 'finish' proc (and all the other exit status procs) execute in the parent, not in the child. If the parent moves on, then we'd need to leave an extra thread waiting for child termination. Is the intent that the 'detach' call spawns a thread to perform the fork and wait for its result?

It will certainly be hard to use properly - the waiting thread will need a Queue through which to communicate with the originating thread, and the child will be using a Pipe to send data to the waiting thread..

@mediaslave24
Copy link
Author

@nevinera

Yes, there should be a waiting thread. But better to keep it in reference object, like "IsoLatte::Process" for querying subprocess status. Example wouldn't work if "finish" will execute in child.

@nevinera
Copy link
Contributor

Sorry, forgot that edits don't get emailed:

IsoLatte is focused on solve the problem of isolation of execution - I wouldn't be averse to adding additional features that serve that purpose, but I'm not sure that adding an asynchronous execution system would really help with that - it's not a job queue.

It sounds more like another gem that could be used alongside IsoLatte to me. Actually, it's a bit similar to what I ended up building in https://github.com/emcien/parenting originally, though that is still organized around waiting for the whole collection of tasks to complete.

@nevinera
Copy link
Contributor

I'm going to close this for now - if you feel the capability is useful for isolation purposes, feel free to make your case here. Your implementation sounds fine, aside from being easy to misuse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants