Skip to content

Commit

Permalink
Jets Autoloaders with zeitwerk
Browse files Browse the repository at this point in the history
* Jets Autoloaders: main for the project, once for Jets internals
* Replace ruby autoload calls and custom jets eager_load
* Remove unnecessary definitions since zeitwerk autovivifies modules
* Adjust Jets.boot: only load turbines vs full Jets once eager load
* Add Jets::Bundle module to centralize bundler logic
* Override JETS_ENV earlier than eager_load in the boot process
* Set up Afterburner with Jets Turbo charge earlier in the boot process
at the CLI start level.
* Fix docs: iot, functions
* clean up PreheatJob.prewarm
  • Loading branch information
tongueroo committed May 30, 2019
1 parent 98347e2 commit 8d7b027
Show file tree
Hide file tree
Showing 119 changed files with 446 additions and 773 deletions.
3 changes: 3 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "bundler/setup"
Bundler.setup
require "bundler/gem_tasks"
require "rspec/core/rake_task"

Expand All @@ -8,6 +10,7 @@ RSpec::Core::RakeTask.new
require_relative "lib/jets"
desc "Generates cli reference docs as markdown"
task :docs do
Jets::Autoloaders.once.eager_load
Jets::Commands::Markdown::Creator.create_all
end

Expand Down
19 changes: 10 additions & 9 deletions docs/_docs/function-resources/function-resources-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ module IotExtension
def thermostat_rule(logical_id, props={})
defaults = {
topic_rule_payload: {
sql: "select * from TemperatureTopic where temperature > 60"
sql: "SELECT * FROM 'TemperatureTopic' WHERE temperature > 60",
actions: [
lambda: { function_arn: "!Ref {namespace}LambdaFunction" }
],
rule_disabled: true,
},
actions: [
lambda: { function_arn: "!Ref {namespace}LambdaFunction" }
]
}
props = defaults.deep_merge(props)
resource(logical_id, "AWS::Iot::TopicRule", props)
resource(logical_id, "AWS::IoT::TopicRule", props)
end
end
```
Expand All @@ -39,7 +40,7 @@ class TemperatureJob < ApplicationJob
end
```

The code above creates an `AWS::Iot::TopicRule` and runs the `record` Lambda function for incoming IoT thermostat data. You can add your own custom business logic to handle the received data accordingly.
The code above creates an `AWS::IoT::TopicRule` and runs the `record` Lambda function for incoming IoT thermostat data. You can add your own custom business logic to handle the received data accordingly.

## Three Resource Forms

Expand All @@ -52,7 +53,7 @@ def thermostat_rule(logical_id, props={})
# ...
resource(
logical_id : {
type: "AWS::Iot::TopicRule",
type: "AWS::IoT::TopicRule",
properties: props
}
)
Expand All @@ -65,7 +66,7 @@ end
def thermostat_rule(logical_id, props={})
# ...
resource(logical_id,
type: "AWS::Iot::TopicRule",
type: "AWS::IoT::TopicRule",
properties: props
)
end
Expand All @@ -76,7 +77,7 @@ end
```ruby
def thermostat_rule(logical_id, props={})
# ...
resource(logical_id, "AWS::Iot::TopicRule", props)
resource(logical_id, "AWS::IoT::TopicRule", props)
end
```

Expand Down
4 changes: 2 additions & 2 deletions docs/_docs/shared-resources/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ You then define the function in the `app/shared/functions` folder:
app/shared/functions/bob.rb:

```ruby
def handler_function(event, context)
def lambda_handler(event:, context:)
puts("hello bob")
end
```

By default, the `function` method creates Ruby lambda functions. The default Ruby handler is `handler_function`.
By default, the `function` method creates Ruby lambda functions. The default Ruby handler is `lambda_handler`.

There is also a `ruby_function` alias to the `function` method. They do the same thing.

Expand Down
6 changes: 3 additions & 3 deletions docs/_reference/jets-call.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ For [jobs](http://rubyonjets.com/docs/jobs/), the event is passed through untouc
jets call hard_job-drive '{"test":1}' | jq .
jets call hard_job-drive file://event.json | jq . # load event with a file
jets call posts_controller-index # event payload is optional
jets call posts_controller-index '{"test":1}' --show-log | jq .
jets call posts_controller-index '{"path":"/posts"}' --show-log | jq .
jets call posts_controller-index 'file://event.json' --show-log | jq .

The equivalent AWS Lambda CLI command:

aws lambda invoke --function-name demo-dev-hard_job-dig --payload '{"test":1}' outfile.txt
aws lambda invoke --function-name demo-dev-hard_job-dig --payload '{"path":"/posts","test":1}' outfile.txt
cat outfile.txt | jq '.'

aws lambda invoke --function-name demo-dev-posts_controller-index --payload '{"queryStringParameters":{"test":1}}' outfile.txt
aws lambda invoke --function-name demo-dev-posts_controller-index --payload '{"queryStringParameters":{"path":"/posts",test":1}}' outfile.txt
cat outfile.txt | jq '.'

For convenience, you can also provide the function name with only dashes and `jets call` figures out what function you intend to call. Examples:
Expand Down
61 changes: 5 additions & 56 deletions lib/jets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,22 @@
require "active_support/ordered_hash"
require "active_support/ordered_options"
require "fileutils"
require "jets/camelizer"
require "jets/version"
require "memoist"
require "rainbow/ext/string"

# Looks like for zeitwerk module autovivification to work `bundle exec` must be called.
# We run Bundle.setup initially which is what `bundle exec does.
# This allows zeitwork module autovivification to work even if the user has not called jets with `bundle exec jets`.
# We check for Afterburner mode since in that mode jets is a standalone tool.
#
require "jets/turbo"
if Jets::Turbo.new.jets?
require "bundler/setup"
Bundler.setup # Same as Bundler.setup(:default)
end
require "zeitwerk"
require "jets/autoloaders"
Jets::Autoloaders.log! if ENV["JETS_AUTOLOAD_LOG"]
Jets::Autoloaders.once.setup

module Jets
RUBY_VERSION = "2.5.3"
class Error < StandardError; end

autoload :Application, "jets/application"
autoload :AwsInfo, "jets/aws_info"
autoload :AwsServices, "jets/aws_services"
autoload :Booter, 'jets/booter'
autoload :Builders, 'jets/builders'
autoload :Call, "jets/call"
autoload :Cfn, 'jets/cfn'
autoload :CLI, "jets/cli"
autoload :Commands, "jets/commands"
autoload :Controller, 'jets/controller'
autoload :Core, "jets/core"
autoload :Db, 'jets/db'
autoload :Dotenv, 'jets/dotenv'
autoload :Erb, "jets/erb"
autoload :Generator, "jets/generator"
autoload :Inflections, "jets/inflections"
autoload :IO, "jets/io"
autoload :Job, 'jets/job'
autoload :Klass, 'jets/klass'
autoload :Lambda, 'jets/lambda'
autoload :Logger, "jets/logger"
autoload :Mailer, "jets/mailer"
autoload :Mega, "jets/mega"
autoload :Middleware, "jets/middleware"
autoload :Naming, 'jets/naming'
autoload :PolyFun, 'jets/poly_fun'
autoload :Preheat, "jets/preheat"
autoload :Processors, 'jets/processors'
autoload :RackServer, "jets/rack_server"
autoload :Rdoc, "jets/rdoc"
autoload :Resource, "jets/resource"
autoload :Route, "jets/route"
autoload :Router, "jets/router"
autoload :Rule, 'jets/rule'
autoload :Stack, "jets/stack"
autoload :TmpLoader, "jets/tmp_loader"
autoload :Turbine, 'jets/turbine'
autoload :Turbo, 'jets/turbo'
autoload :Util, "jets/util"

extend Core # root, logger, etc
end

require "jets/core_ext/kernel"

root = File.expand_path("..", File.dirname(__FILE__))
root = File.expand_path("..", __dir__)

$:.unshift("#{root}/vendor/jets-gems/lib")
require "jets-gems"
Expand All @@ -81,4 +30,4 @@ class Error < StandardError; end
$:.unshift("#{root}/vendor/rails/actionview/lib")
# will require action_controller, action_pack, etc later when needed

Jets::Db # trigger autoload
Jets::Autoloaders.once.preload("#{__dir__}/jets/db.rb") # required for booter.rb: setup_db
Loading

0 comments on commit 8d7b027

Please sign in to comment.