Skip to content

Instantly share code, notes, and snippets.

@bcoe
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bcoe/9094018 to your computer and use it in GitHub Desktop.
Save bcoe/9094018 to your computer and use it in GitHub Desktop.
Semaphore using EventMachine gratifications.
# Execute an IMAP command from the frontend
# API on a specific user's ImapConnection.
#
# @param command [symbol] the command to execute.
# @param params [Hash] the parameters for the command.
def execute_imap_command(command, params)
log_info("executing #{command} for #{email}")
# we should re-select the mailbox
# once per command executed.
@imap_connection.mailbox = nil
previous_command = @previous_command
# Use EM gratifications to create a mutex on imap commands.
if previous_command && previous_command.instance_variable_get(:@deferred_status) == :unknown
@previous_command = previous_command.
bind! do
@imap_connection.send(command, params)
end
else
@previous_command = @imap_connection.send(command, params)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment