Skip to content

Execute a command on current thread#13

Merged
pocke merged 8 commits intocookpad:masterfrom
pocke:run-on-current-thread
Aug 29, 2016
Merged

Execute a command on current thread#13
pocke merged 8 commits intocookpad:masterfrom
pocke:run-on-current-thread

Conversation

@pocke
Copy link
Copy Markdown
Contributor

@pocke pocke commented Aug 23, 2016

I've added a feature to execute a command on current thread.

Currently, we can execute a command on current thread by the following code.

require 'expeditor'
require 'concurrent/executor/immediate_executor'

service = Expeditor::Service.new executor: Concurrent::ImmediateExecutor.new
command = Expeditor::Command.new(service: service){puts 1; sleep 1; puts 2}

puts 'start'
command.start
puts 'end'

However, it's complexity. So, I've added Command#run method to execute on current thread.
The following code works same as the above.

service = Expeditor::Service.new
command = Expeditor::Command.new(service: service){puts 1; sleep 1; puts 2}

puts 'start'
command.run  # use run instead of start
puts 'end'

Naming of run is inspired by exec.Command of Golang. https://golang.org/pkg/os/exec/#Cmd.Run

@pocke
Copy link
Copy Markdown
Contributor Author

pocke commented Aug 23, 2016

TODO: remove unneeded clone in with_fallback method

@pocke
Copy link
Copy Markdown
Contributor Author

pocke commented Aug 25, 2016

I've changed the approach of the PR. So, I'll re-create a new PR for the purpose.
I've created a new PR ( #16 ), but I use this( #13 ) instead of the new PR.

@pocke pocke closed this Aug 25, 2016
@pocke pocke reopened this Aug 25, 2016
@pocke pocke force-pushed the run-on-current-thread branch 3 times, most recently from 6f54ab2 to eabd4cb Compare August 26, 2016 03:01
@pocke pocke changed the title [WIP] Execute a command on current thread Execute a command on current thread Aug 26, 2016
@pocke pocke force-pushed the run-on-current-thread branch 2 times, most recently from 45ffee2 to 8db6cc2 Compare August 26, 2016 04:53
@pocke
Copy link
Copy Markdown
Contributor Author

pocke commented Aug 26, 2016

Feature

Add Command#run method

I've added Expedior::Command#run method.
This method is similar to the Command#start, but the method is executed on the current thread.

Internal implementation

Initialise futures timing

Expeditor::Command should switch an executor to execute on current thread. So, I've changed timing of initialising futures. Futures are initialised before just start or run.

Add tests

@gfx
Copy link
Copy Markdown

gfx commented Aug 26, 2016

[IMO] The name of run seems difficult to learn because it never implies the running thread.

I prefer more descriptive names, like start_on_current_thread.

@k0kubun
Copy link
Copy Markdown
Contributor

k0kubun commented Aug 26, 2016

I prefer more descriptive names, like start_on_current_thread.

I think start implies "start asynchronous execution". Since run is synchronous, at least it should not start with "start".

The name of run seems difficult to learn because it never implies the running thread.

While run does not imply running thread, using current thread for synchronous execution is natural. So I prefer run rather than start_on_current_thread.

@k0kubun
Copy link
Copy Markdown
Contributor

k0kubun commented Aug 26, 2016

Oh, probably I'm confused about synchronous/asynchronous. Never mind.

@pocke
Copy link
Copy Markdown
Contributor Author

pocke commented Aug 26, 2016

@gfx

The name of run seems difficult to learn because it never implies the running thread.

yes, I think so, too.

I prefer more descriptive names, like start_on_current_thread.

I think start_on_current_thread is too long.
So, #start receives a keyword argument for this feature. and remove run method.
e.g.

command = Expeditor::Command.new{xxx}
command.start(current_thread: true) # execute on current thread

What do you think?

@gfx
Copy link
Copy Markdown

gfx commented Aug 26, 2016

Typing start(current_thread: true) seems much more difficult than start_on_current_thread because my text editor, RubyMine, can suggest instance methods, although the auto-completion does not always work perfectly.

@adorechic
Copy link
Copy Markdown

How about start_with_retry? It should be rename to start_with_retry_on_current_thread ?
WDYT?

@gfx
Copy link
Copy Markdown

gfx commented Aug 26, 2016

@adorechic

Nice suggestion. I think start_with_retry_on_current_thread is not a bad name because it's really clear and auto-completetion friendly, but I know there's someone who hates it.

@pocke
Copy link
Copy Markdown
Contributor Author

pocke commented Aug 26, 2016

I'll implement this as a keyword argument.
@gfx thanks for your suggestion.

Comment thread lib/expeditor/command.rb Outdated

def started?
@normal_future.executed?
!!@normal_future && @normal_future.executed?
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to be more simply @normal_future && @normal_future.executed?

Copy link
Copy Markdown
Contributor Author

@pocke pocke Aug 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's not good. Because @normal_future should be hidden from library users.
I follow the style guide if it exists.

Note: cookpad/styleguide doesn't have the rule.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be hidden from library users.

I agree it, but @normal_future && @normal_future.executed? doesn't returns @normal_future, does it?
Although it might returns nil, nil does not show any information about @normal_future.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was confused. The implementation doesn't return a future. I'll change it to avoiding !!.

@adorechic
Copy link
Copy Markdown

@pocke don't you support start_with_retry?

Comment thread lib/expeditor/command.rb Outdated
def start
# @param current_thread [Boolean] Execute the task on current thread(blocking)
def start(current_thread: false)
if not started?
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless seems to be more readable.

@pocke
Copy link
Copy Markdown
Contributor Author

pocke commented Aug 29, 2016

@pocke don't you support start_with_retry?

Thanks, I forgot it. I'll implement it.

@pocke pocke force-pushed the run-on-current-thread branch from 1450e5c to db50822 Compare August 29, 2016 02:28
Comment thread lib/expeditor/command.rb Outdated
def start_with_retry(retryable_options = {})
if not started?
unless started?
current_thread = retryable_options.delete(:current_thread)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current_thread is not a member of retryable_options, so this variable name is a little confusing.
I think you should rename it, for example options.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I think so, too. I'll rename it.

- Avoid double `!`
- Use kwrest argument instead of one option argument
@pocke
Copy link
Copy Markdown
Contributor Author

pocke commented Aug 29, 2016

I've fixed the suggestions.
Please review the code.


describe '#start' do
context 'with normal' do
it 'should execute on current thread' do
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should is unnecessary description. it 'executes on current thread' is enough.

@adorechic
Copy link
Copy Markdown

LGTM with a comment

@pocke
Copy link
Copy Markdown
Contributor Author

pocke commented Aug 29, 2016

should is unnecessary description. it 'executes on current thread' is enough.

Thanks, I've should from description.

@pocke pocke merged commit eb9d227 into cookpad:master Aug 29, 2016
@pocke pocke deleted the run-on-current-thread branch August 29, 2016 04:50
@pocke pocke mentioned this pull request Aug 29, 2016
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

Successfully merging this pull request may close these issues.

5 participants