Class: Smash::CloudPowers::Synapse::Queue::Board
- Inherits:
-
Object
- Object
- Smash::CloudPowers::Synapse::Queue::Board
- Includes:
- AwsResources, Helper, Smash::CloudPowers::Synapse::Queue, Zenv
- Defined in:
- lib/cloud_powers/synapse/queue/board.rb
Overview
The Queue::Board class helps wrap up information and functionality of a Queue on SQS. It is basically just an abstraction to make using SQS simpler
Instance Attribute Summary collapse
-
#address ⇒ Object
Gives the Queue address (URL).
-
#name ⇒ Object
The name the Aws::SQS::Queue uses.
-
#poller ⇒ Object
Gets a QueuePoller for the Queue attached to this Board instance.
-
#sqs ⇒ Object
An Aws::SQS::Client.
Class Method Summary collapse
-
.build(name, this_sqs = nil) ⇒ Object
Builds a Queue::Board object and returns it.
-
.create!(name, this_sqs = nil) ⇒ Object
Builds then Creates the Object and makes the API call to SQS to create the queue.
Instance Method Summary collapse
-
#best_guess_address ⇒ Object
Gives a best guess at the URL that points toward this Board's Queue.
-
#create_queue! ⇒ Object
Creates an actual Queue in SQS using the standard format for this queue name (camel case).
-
#destroy! ⇒ Object
Deletes an actual Queue from SQS.
-
#exists? ⇒ Boolean
Predicate method to query SQS for the queue.
-
#i_var ⇒ Object
Gives back a string representation of the instance variable for this board.
-
#initialize(name, this_sqs = sqs) ⇒ Board
constructor
Creates a Board object.
-
#message_count ⇒ Object
Gets the approximate message count for a Queue using the 'ApproximateMessageCount' attribute.
-
#pluck_message ⇒ Object
Retrieves a message from the Queue and deletes it from the Queue in SQS.
-
#save! ⇒ Object
This method creates the queue in SQS for the given Board instance It can be coupled with the #build() method in order to use a queue without making the call to create it on AWS.
-
#send_message(message) ⇒ Object
Sends the given message to the queue.
Methods included from Zenv
#env_vars, #file_tree_search, #i_vars, #project_root, #project_root=, #system_vars, #zfind
Methods included from Helper
#attr_map!, #available_resources, #called_from, #create_logger, #deep_modify_keys_with, #format_error_message, #log_file, #logger, #modify_keys_with, #smart_retry, #task_path, #task_require_path, #to_camel, #to_hyph, #to_i_var, #to_pascal, #to_ruby_file_name, #to_snake, #update_message_body, #valid_json?, #valid_url?
Methods included from Smash::CloudPowers::Synapse::Queue
#board_name, #build_queue, #delete_queue_message, #get_queue_message_count, #pluck_queue_message, #poll, #queue_exists?, #queue_poller, #queue_search, #send_queue_message
Methods included from AwsResources
#ec2, #image, #kinesis, #region, #s3, #sns
Methods included from Auth
Constructor Details
#initialize(name, this_sqs = sqs) ⇒ Board
Creates a Board object.
The #new() method is wrapped in #build() and #create!() but isn't private so
#new() can be used instead of build.
The Board doesn't create Queue(s) or any other API calls until it is instructed to.
Parameters
- name
String- the name of the board can be used to find its arn/url etc
Returns
Queue::Board
32 33 34 35 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 32 def initialize(name, this_sqs = sqs) @sqs = this_sqs @name = name end |
Instance Attribute Details
#address ⇒ Object
Gives the Queue address (URL). First the environment is searched, using Zenv and if nothing is found, the best guess attempt at the correct address is used.
Returns
- queue address
14 15 16 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 14 def address @address end |
#name ⇒ Object
The name the Aws::SQS::Queue uses
16 17 18 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 16 def name @name end |
#poller ⇒ Object
Gets a QueuePoller for the Queue attached to this Board instance.
Returns
Aws::SQS::QueuePoller
Notes
- Provide an existing SQS Client if one exists. This is used to sort out development production work.
18 19 20 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 18 def poller @poller end |
#sqs ⇒ Object
An Aws::SQS::Client. See #Smash::CloudPowers::AwsResources.sqs()
20 21 22 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 20 def sqs @sqs end |
Class Method Details
.build(name, this_sqs = nil) ⇒ Object
Builds a Queue::Board object and returns it. No API calls are sent using this method. This is handy if you need to use a Queue but you don't want to create any resources in AWS.
Parameters name String
Returns Queue::Board
Example exp_board = Board.build('exampleBoard') puts exp_board.best_guess_address
=> 'https://sqs.us-west-2.amaz.....'
80 81 82 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 80 def self.build(name, this_sqs = nil) new(name, this_sqs) end |
.create!(name, this_sqs = nil) ⇒ Object
Builds then Creates the Object and makes the API call to SQS to create the queue
Parameters
- name
Returns
Queue::Board with an actual Queue in SQS
Example exp_board = Board.create!('exampleBoard') queue_search(exp_board.name)
=> ['https://sqs.us-west-2.amazonaws.com/81234567890/exampleBoard']
96 97 98 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 96 def self.create!(name, this_sqs = nil) build(name, this_sqs).create_queue! end |
Instance Method Details
#best_guess_address ⇒ Object
Gives a best guess at the URL that points toward this Board's Queue. It uses a couple params to build a standard URL for SQS. The only problem with using this last resort is you may need to use a Queue from a different region, account or name but it can be a handy catch-all for the URLs for most cases.
Returns String
65 66 67 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 65 def best_guess_address "https://sqs.#{zfind(:aws_region)}.amazonaws.com/#{zfind(:account_number)}/#{@name}" end |
#create_queue! ⇒ Object
Creates an actual Queue in SQS using the standard format for this queue name (camel case)
Returns
Queue::Board
104 105 106 107 108 109 110 111 112 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 104 def create_queue! begin sqs.create_queue(queue_name: to_camel(@name)) self rescue Aws::SQS::Errors::QueueDeletedRecently => e sleep 5 retry end end |
#destroy! ⇒ Object
Deletes an actual Queue from SQS
115 116 117 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 115 def destroy! sqs.delete_queue(queue_url: address) end |
#exists? ⇒ Boolean
Predicate method to query SQS for the queue
Example board = Board.build('example') board.exists?
=> false
board.save! board.exists?
=> true
128 129 130 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 128 def exists? queue_exists?(@name) end |
#i_var ⇒ Object
Gives back a string representation of the instance variable for this board.
Returns String - the instance variable for this Board in string format
Example board = Board.new(:backlog) Smash.instance_variable_get(board.i_var) #=> Board <name: :backlog, ...>
45 46 47 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 45 def i_var "@#{@name}" end |
#message_count ⇒ Object
Gets the approximate message count for a Queue using the 'ApproximateMessageCount' attribute
Returns
Integer
136 137 138 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 136 def (address) end |
#pluck_message ⇒ Object
Retrieves a message from the Queue and deletes it from the Queue in SQS
154 155 156 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 154 def (@name) end |
#save! ⇒ Object
This method creates the queue in SQS for the given Board instance It can be coupled with the #build() method in order to use a queue without making the call to create it on AWS
Example board = Board.build('example') board.exists?
=> false
board.save! board.exists?
=> true
169 170 171 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 169 def save! create_queue! end |
#send_message(message) ⇒ Object
Sends the given message to the queue
Parameters
- message - used as JSON or converted into it
177 178 179 180 181 |
# File 'lib/cloud_powers/synapse/queue/board.rb', line 177 def () ( address, (valid_json?() ? : .to_json), sqs ) end |