funktor 0.2.1 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +35 -0
  3. data/.gitignore +2 -2
  4. data/Gemfile.lock +6 -6
  5. data/funktor.gemspec +1 -1
  6. data/lib/funktor.rb +0 -1
  7. data/lib/funktor/cli/bootstrap.rb +7 -7
  8. data/lib/funktor/cli/init.rb +135 -30
  9. data/lib/funktor/cli/templates/Gemfile +1 -3
  10. data/lib/funktor/cli/templates/{workers → app/workers}/hello_worker.rb +0 -0
  11. data/lib/funktor/cli/templates/config/boot.rb +16 -0
  12. data/lib/funktor/cli/templates/config/environment.yml +4 -2
  13. data/lib/funktor/cli/templates/{function_definitions → config/function_definitions}/incoming_job_handler.yml +1 -1
  14. data/lib/funktor/cli/templates/config/function_definitions/work_queue_handler.yml +11 -0
  15. data/lib/funktor/cli/templates/config/funktor.yml +27 -14
  16. data/lib/funktor/cli/templates/{iam_permissions → config/iam_permissions}/incoming_job_queue.yml +0 -0
  17. data/lib/funktor/cli/templates/{iam_permissions → config/iam_permissions}/ssm.yml +0 -0
  18. data/lib/funktor/cli/templates/{iam_permissions/active_job_queue.yml → config/iam_permissions/work_queue.yml} +1 -1
  19. data/lib/funktor/cli/templates/config/package.yml +4 -2
  20. data/lib/funktor/cli/templates/{resources → config/resources}/cloudwatch_dashboard.yml +268 -231
  21. data/lib/funktor/cli/templates/{resources → config/resources}/incoming_job_queue.yml +0 -0
  22. data/lib/funktor/cli/templates/{resources → config/resources}/incoming_job_queue_user.yml +0 -0
  23. data/lib/funktor/cli/templates/config/resources/work_queue.yml +22 -0
  24. data/lib/funktor/cli/templates/funktor_init.yml.tt +69 -0
  25. data/lib/funktor/cli/templates/{handlers → lambda_event_handlers}/incoming_job_handler.rb +1 -1
  26. data/lib/funktor/cli/templates/lambda_event_handlers/work_queue_handler.rb +8 -0
  27. data/lib/funktor/cli/templates/package.json +8 -1
  28. data/lib/funktor/cli/templates/serverless.yml +13 -12
  29. data/lib/funktor/incoming_job_handler.rb +11 -1
  30. data/lib/funktor/job.rb +4 -0
  31. data/lib/funktor/version.rb +1 -1
  32. data/lib/funktor/worker.rb +9 -0
  33. metadata +17 -18
  34. data/lib/funktor/cli/templates/function_definitions/active_job_handler.yml +0 -11
  35. data/lib/funktor/cli/templates/funktor.yml.tt +0 -51
  36. data/lib/funktor/cli/templates/handlers/active_job_handler.rb +0 -17
  37. data/lib/funktor/cli/templates/resources/active_job_queue.yml +0 -22
  38. data/lib/funktor/deploy/cli.rb +0 -42
  39. data/lib/funktor/deploy/serverless.rb +0 -60
  40. data/lib/funktor/deploy/serverless_templates/serverless.yml +0 -156
@@ -0,0 +1,22 @@
1
+ Resources:
2
+ <%= work_queue_name.camelize %>Queue:
3
+ Type: AWS::SQS::Queue
4
+ Properties:
5
+ QueueName: ${self:custom.funktor.<%= work_queue_name.camelize %>QueueName}
6
+ VisibilityTimeout: 300
7
+ RedrivePolicy:
8
+ deadLetterTargetArn:
9
+ "Fn::GetAtt": [ <%= work_queue_name.camelize %>DeadLetterQueue, Arn ]
10
+ maxReceiveCount: 5
11
+ <%= work_queue_name.camelize %>DeadLetterQueue:
12
+ Type: AWS::SQS::Queue
13
+ Properties:
14
+ QueueName: ${self:custom.funktor.<%= work_queue_name.camelize %>DeadJobQueueName}
15
+
16
+ Outputs:
17
+ <%= work_queue_name.camelize %>QueueUrl:
18
+ Value:
19
+ Ref: <%= work_queue_name.camelize %>Queue
20
+ <%= work_queue_name.camelize %>DeadLetterQueueUrl:
21
+ Value:
22
+ Ref: <%= work_queue_name.camelize %>DeadLetterQueue
@@ -0,0 +1,69 @@
1
+ # ℹ️ WARNING: This file doesn't control anything directly. It's used to allow you to set/maintain config options in a central place.
2
+ # When you make changes to this file you should run `funktor init` again to propagate the changes to the 'active' configuration files.
3
+ appName: <%= name %>
4
+ runtime: ruby2.7
5
+
6
+ # These values will be applied to any funktor handlers that don't specify custom values.
7
+ # TODO - These still need to be wired up to do anything.
8
+ handlerDefaults:
9
+ # timeout is how long the handler can possibly run. Up to 10 messages may be delivered
10
+ # to a handler at one time, so you'll want this to be at least 10x the maximum time you
11
+ # expect to spend for one message. We default to a high number here to allow for the
12
+ # times when things go weird.
13
+ timeout: 300
14
+
15
+ # reservedConcurrency represents the maximum number of concurrent executions.
16
+ # Usually you'll want to leave this as null so that handlers can scale infinitely
17
+ # (within your account limits). If you need to restrict concurrency to prevent
18
+ # resource exhaustion you should do it at the queue level.
19
+ reservedConcurrency: null
20
+
21
+ # provisionedConcurrency represents the number of lambda functions that will always
22
+ # be available. You probably want to set this on individual queues if you're going
23
+ # to use it.
24
+ provisionedConcurrency: null
25
+
26
+ # Use memory_size to adjust the reousrces (both memory and CPU) available.
27
+ # We default to 256 as a reasonable trade off on saving money but getting
28
+ # reasonable performance.
29
+ memorySize: 256
30
+
31
+ # You can set the batch size. Max of 10_000 for normal queues, 10 for FIFO.
32
+ batchSize: 10
33
+
34
+ # How many seconds should AWS wait for a batch to fill up before executing lambda?
35
+ # For immediate execution set the batch size to 1.
36
+ maximumBatchingWindow : 1
37
+
38
+ # Visibility timeout should only come into play in the case of Funktor errors.
39
+ # Application level errors should be handled by Funktor retry mechanisms.
40
+ # The visibility timeout should be at least as long as the function timeout, and up to 6 times larger.
41
+ visibilityTimeout: 900
42
+
43
+ # Set log rentention to save money
44
+ logRetentionInDays: 30
45
+
46
+ # Incoming Job Handler
47
+ incomingJobHandler:
48
+ # If your jobs are bursty AND time-sensitive you might want to have some lambdas pre-provisioned
49
+ # to quickly handle jobs at the beginning of a burst. Uncomment the line below if so.
50
+ # provisionedConcurrency: 4
51
+
52
+
53
+ queues:
54
+ - default:
55
+ # Set queue specific config options here
56
+ # memorySize: 512
57
+ # TODO - Is it advisable to use FIFO queuues with Funktor? Maybe this isn't really even supported by CloudFormation?
58
+ # fifo: false
59
+ - singleThread:
60
+ reservedConcurrency: 1
61
+
62
+ # TODO - Maybe this shouldn't be surfaced this early?
63
+ # TODO - This still needs to be wired up to do anything.
64
+ package:
65
+ patterns:
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - app/**
69
+ - config/**
@@ -1,4 +1,4 @@
1
- require 'funktor'
1
+ require_relative '../config/boot'
2
2
 
3
3
  $handler = Funktor::IncomingJobHandler.new
4
4
 
@@ -0,0 +1,8 @@
1
+ require_relative '../config/boot'
2
+
3
+ $handler = Funktor::ActiveJobHandler.new
4
+
5
+ def call(event:, context:)
6
+ $handler.call(event: event, context: context)
7
+ end
8
+
@@ -1 +1,8 @@
1
- {"name":"serverless-sub","description":"","version":"0.1.0","devDependencies":{"serverless-ruby-layer":"^1.4.0"}}
1
+ {
2
+ "name":"<%= name %>",
3
+ "description":"",
4
+ "version":"0.1.0",
5
+ "devDependencies":{
6
+ "serverless-ruby-layer":"^1.4.0"
7
+ }
8
+ }
@@ -12,7 +12,7 @@
12
12
  # Happy Coding!
13
13
 
14
14
  # The name of your service. All your AWS resources will contain this name.
15
- service: yourapp-funktor
15
+ service: <%= name %>
16
16
 
17
17
  # This causes serverless to throw an error early if the config is bad, instead of waiting for CloudFormation to try and fail to deploy it.
18
18
  configValidationMode: error
@@ -22,13 +22,14 @@ frameworkVersion: '2'
22
22
 
23
23
  provider:
24
24
  name: aws
25
- runtime: ruby2.7
25
+ runtime: <%= runtime %>
26
26
  lambdaHashingVersion: 20201221
27
27
  environment: ${file(config/environment.yml)}
28
+ versionFunctions: false # Reduces the amount of storage used since all Lambdas together are limited to 75GB
28
29
  iamRoleStatements:
29
- - ${file(iam_permissions/ssm.yml)}
30
- - ${file(iam_permissions/active_job_queue.yml)}
31
- - ${file(iam_permissions/incoming_job_queue.yml)}
30
+ <%- all_iam_permissions.each do |iam_permission| -%>
31
+ - ${file(<%= iam_permission %>)}
32
+ <%- end -%>
32
33
 
33
34
 
34
35
  custom:
@@ -41,14 +42,14 @@ custom:
41
42
  package: ${file(config/package.yml)}
42
43
 
43
44
  functions:
44
- incomingJobHandler: ${file(function_definitions/incoming_job_handler.yml)}
45
- activeJobHandler: ${file(function_definitions/active_job_handler.yml)}
45
+ <%- all_function_definitions.each do |function_definition| -%>
46
+ <%= File.basename(function_definition, ".yml").camelize %>: ${file(<%= function_definition %>)}
47
+ <%- end -%>
46
48
 
47
49
  resources:
48
- - ${file(resources/active_job_queue.yml)}
49
- - ${file(resources/incoming_job_queue.yml)}
50
- - ${file(resources/incoming_job_queue_user.yml)}
51
- - ${file(resources/cloudwatch_dashboard.yml)}
50
+ <%- all_resources.each do |resource| -%>
51
+ - ${file(<%= resource %>)}
52
+ <%- end -%>
52
53
 
53
54
  plugins:
54
- - /Users/jgreen/projects/serverless-ruby-layer
55
+ - serverless-ruby-layer
@@ -1,4 +1,5 @@
1
1
  require 'aws-sdk-sqs'
2
+ require 'active_support/core_ext/string/inflections'
2
3
 
3
4
  module Funktor
4
5
  class IncomingJobHandler
@@ -26,10 +27,19 @@ module Funktor
26
27
  ENV['FUNKTOR_ACTIVE_JOB_QUEUE']
27
28
  end
28
29
 
30
+ def queue_for_job(job)
31
+ queue_name = job.queue || 'default'
32
+ queue_constant = "FUNKTOR_#{queue_name.underscore.upcase}_QUEUE"
33
+ puts "queue_constant = #{queue_constant}"
34
+ puts "ENV value = #{ENV[queue_constant]}"
35
+ ENV[queue_constant] || ENV['FUNKTOR_DEFAULT_QUEUE']
36
+ end
37
+
29
38
  def push_to_active_job_queue(job)
39
+ puts "job = #{job.to_json}"
30
40
  sqs_client.send_message({
31
41
  # TODO : How to get this URL...
32
- queue_url: active_job_queue,
42
+ queue_url: queue_for_job(job),
33
43
  message_body: job.to_json,
34
44
  delay_seconds: job.delay
35
45
  })
data/lib/funktor/job.rb CHANGED
@@ -10,6 +10,10 @@ module Funktor
10
10
  @job_data ||= Funktor.parse_json(job_string)
11
11
  end
12
12
 
13
+ def queue
14
+ job_data["queue"]
15
+ end
16
+
13
17
  def worker_class_name
14
18
  job_data["worker"]
15
19
  end
@@ -1,3 +1,3 @@
1
1
  module Funktor
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.6"
3
3
  end
@@ -28,6 +28,10 @@ module Funktor::Worker
28
28
  get_funktor_options[:queue_url]
29
29
  end
30
30
 
31
+ def custom_queue
32
+ get_funktor_options[:queue]
33
+ end
34
+
31
35
  def queue_url
32
36
  # TODO : Should this default to FUNKTOR_ACTIVE_JOB_QUEUE?
33
37
  # Depends how e handle this in pro...?
@@ -73,10 +77,15 @@ module Funktor::Worker
73
77
  @client ||= Aws::SQS::Client.new
74
78
  end
75
79
 
80
+ def work_queue
81
+ (self.custom_queue || 'default').to_s
82
+ end
83
+
76
84
  def build_job_payload(job_id, delay, *worker_params)
77
85
  {
78
86
  worker: self.name,
79
87
  worker_params: worker_params,
88
+ queue: self.work_queue,
80
89
  job_id: job_id,
81
90
  delay: delay,
82
91
  funktor_options: get_funktor_options
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: funktor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Green
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-04 00:00:00.000000000 Z
11
+ date: 2021-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-sqs
@@ -89,6 +89,7 @@ executables:
89
89
  extensions: []
90
90
  extra_rdoc_files: []
91
91
  files:
92
+ - ".github/workflows/ruby.yml"
92
93
  - ".gitignore"
93
94
  - ".rspec"
94
95
  - ".travis.yml"
@@ -114,29 +115,27 @@ files:
114
115
  - lib/funktor/cli/generate/work_queue.rb
115
116
  - lib/funktor/cli/init.rb
116
117
  - lib/funktor/cli/templates/Gemfile
118
+ - lib/funktor/cli/templates/app/workers/hello_worker.rb
119
+ - lib/funktor/cli/templates/config/boot.rb
117
120
  - lib/funktor/cli/templates/config/environment.yml
121
+ - lib/funktor/cli/templates/config/function_definitions/incoming_job_handler.yml
122
+ - lib/funktor/cli/templates/config/function_definitions/work_queue_handler.yml
118
123
  - lib/funktor/cli/templates/config/funktor.yml
124
+ - lib/funktor/cli/templates/config/iam_permissions/incoming_job_queue.yml
125
+ - lib/funktor/cli/templates/config/iam_permissions/ssm.yml
126
+ - lib/funktor/cli/templates/config/iam_permissions/work_queue.yml
119
127
  - lib/funktor/cli/templates/config/package.yml
128
+ - lib/funktor/cli/templates/config/resources/cloudwatch_dashboard.yml
129
+ - lib/funktor/cli/templates/config/resources/incoming_job_queue.yml
130
+ - lib/funktor/cli/templates/config/resources/incoming_job_queue_user.yml
131
+ - lib/funktor/cli/templates/config/resources/work_queue.yml
120
132
  - lib/funktor/cli/templates/config/ruby_layer.yml
121
- - lib/funktor/cli/templates/function_definitions/active_job_handler.yml
122
- - lib/funktor/cli/templates/function_definitions/incoming_job_handler.yml
123
- - lib/funktor/cli/templates/funktor.yml.tt
133
+ - lib/funktor/cli/templates/funktor_init.yml.tt
124
134
  - lib/funktor/cli/templates/gitignore
125
- - lib/funktor/cli/templates/handlers/active_job_handler.rb
126
- - lib/funktor/cli/templates/handlers/incoming_job_handler.rb
127
- - lib/funktor/cli/templates/iam_permissions/active_job_queue.yml
128
- - lib/funktor/cli/templates/iam_permissions/incoming_job_queue.yml
129
- - lib/funktor/cli/templates/iam_permissions/ssm.yml
135
+ - lib/funktor/cli/templates/lambda_event_handlers/incoming_job_handler.rb
136
+ - lib/funktor/cli/templates/lambda_event_handlers/work_queue_handler.rb
130
137
  - lib/funktor/cli/templates/package.json
131
- - lib/funktor/cli/templates/resources/active_job_queue.yml
132
- - lib/funktor/cli/templates/resources/cloudwatch_dashboard.yml
133
- - lib/funktor/cli/templates/resources/incoming_job_queue.yml
134
- - lib/funktor/cli/templates/resources/incoming_job_queue_user.yml
135
138
  - lib/funktor/cli/templates/serverless.yml
136
- - lib/funktor/cli/templates/workers/hello_worker.rb
137
- - lib/funktor/deploy/cli.rb
138
- - lib/funktor/deploy/serverless.rb
139
- - lib/funktor/deploy/serverless_templates/serverless.yml
140
139
  - lib/funktor/fake_job_queue.rb
141
140
  - lib/funktor/incoming_job_handler.rb
142
141
  - lib/funktor/job.rb
@@ -1,11 +0,0 @@
1
- handler: handlers/active_job_handler.call
2
- timeout: ${self:custom.funktor.activeJobHandler.timeout, 900}
3
- reservedConcurrency: ${self:custom.funktor.activeJobHandler.reservedConcurrency, null}
4
- provisionedConcurrency: ${self:custom.funktor.activeJobHandler.provisionedConcurrency, null}
5
- memorySize: ${self:custom.funktor.activeJobHandler.memorySize, 256}
6
- events:
7
- - sqs:
8
- arn:
9
- Fn::GetAtt:
10
- - ActiveJobQueue
11
- - Arn
@@ -1,51 +0,0 @@
1
- # ℹ️ WARNING: This file doesn't control anything directly. It's used to allow you to set/maintain config options in a central place.
2
- # When you make changes to this file you should run `funktor init` again to propagate the changes to the 'active' configuration files.
3
- runtime: ruby2.7
4
-
5
- # Incoming Job Handler
6
- incoming_job_handler:
7
- # Use memory_size to adjust the reousrces (both memory and CPU) available
8
- # memory_size: 512
9
- timeout_in_seconds: 20 # This handler might have to handle several incoming jobs at once
10
- # WARNING : You probably don't want to limit concurrency on incoming jobs.
11
- # concurrency: 100
12
-
13
- # Delayed Job Activator
14
- delayed_job_activator:
15
- # Use memory_size to adjust the reousrces (both memory and CPU) available
16
- # memory_size: 512
17
- execution_schedule: rate(1 minute)
18
- activation_window_in_seconds: 120 # Activate any jobs scheduled for the next two minutes
19
- timeout_in_seconds: 300 # Allow an activation job to run for up to 5 minutes
20
- concurrency: 1
21
-
22
-
23
- queues:
24
- default:
25
- # Use memory_size to adjust the reousrces (both memory and CPU) available
26
- # memory_size: 512
27
- # You can set the batch size. Max of 10_000 for normal queues, 10 for FIFO.
28
- # batch_size: 10
29
- # How many seconds should AWS wait for a batch to fill up before executing lambda?
30
- # For immediate execution set the batch size to 1.
31
- # maximumBatchingWindow : 1
32
- # A single handler can receive up to batch_size jobs at a time. Make sure timeout is long enough.
33
- timeout_in_seconds: 300
34
- # You might want to limit concurrency of executing jobs to stay within resource limits (like DB connections).
35
- # concurrency: 10
36
- # Visibility timeout should only come into play in the case of Funktor errors.
37
- # Application level errors should be handled by Funktor retry mechanisms.
38
- # The visibility timeout should be at least as long as the function timeout, and up to 6 times larger.
39
- # visibility_timeout: 300
40
- # TODO - Is it advisable to use FIFO queuues with Funktor?
41
- # TODO - Maybe this isn't really even supported by CloudFormation?
42
- # fifo: false
43
-
44
- # TODO - Maybe this is handled in the Dockerfile?
45
- package:
46
- patterns:
47
- - Gemfile
48
- - Gemfile.lock
49
- - ../app/**
50
- - ../config/**
51
- - ../lambda_handlers/**
@@ -1,17 +0,0 @@
1
- require 'funktor'
2
-
3
- # Bundler is hard to make work because AWS includes some gems in the basic ruby runtime.
4
- # We're probably going to need to use containers...
5
- #require "rubygems"
6
- #require "bundler/setup"
7
- #Bundler.require(:default)
8
-
9
- # TODO : Ideally this wouldn't be needed
10
- require_relative '../workers/hello_worker'
11
-
12
- $handler = Funktor::ActiveJobHandler.new
13
-
14
- def call(event:, context:)
15
- $handler.call(event: event, context: context)
16
- end
17
-
@@ -1,22 +0,0 @@
1
- Resources:
2
- ActiveJobQueue:
3
- Type: AWS::SQS::Queue
4
- Properties:
5
- QueueName: ${self:custom.funktor.activeJobQueueName}
6
- VisibilityTimeout: 300
7
- RedrivePolicy:
8
- deadLetterTargetArn:
9
- "Fn::GetAtt": [ ActiveJobDeadLetterQueue, Arn ]
10
- maxReceiveCount: 5
11
- ActiveJobDeadLetterQueue:
12
- Type: AWS::SQS::Queue
13
- Properties:
14
- QueueName: ${self:custom.funktor.deadJobQueueName}
15
-
16
- Outputs:
17
- ActiveJobQueueUrl:
18
- Value:
19
- Ref: ActiveJobQueue
20
- ActiveJobDeadLetterQueueUrl:
21
- Value:
22
- Ref: ActiveJobDeadLetterQueue
@@ -1,42 +0,0 @@
1
- require 'optparse'
2
- require 'funktor/deploy/serverless'
3
-
4
- module Funktor
5
- module Deploy
6
- class CLI
7
- attr_reader :options
8
- def initialize
9
- @options = {
10
- verbose: false,
11
- file: 'funktor.yml',
12
- tmp_dir_prefix: '.funktor',
13
- stage: 'dev'
14
- }
15
- end
16
-
17
- def parse(argv = ARGV)
18
- OptionParser.new do |opts|
19
- opts.on('-v', '--verbose', 'Display verbose output') do |verbose|
20
- options[:verbose] = verbose
21
- end
22
- opts.on('-f', '--file=FILE', 'The path to the funktor.yml file to deploy') do |file|
23
- options[:file] = file
24
- end
25
- opts.on('-t', '--tmp_dir_prefix=TMP_DIR_PREFIX', 'The prefix for the tmp dir. The stage will be appended.') do |tmp_dir_prefix|
26
- options[:tmp_dir_prefix] = tmp_dir_prefix
27
- end
28
- opts.on('-s', '--stage=STAGE', 'The stage to deploy to. Defaults to "dev"') do |stage|
29
- options[:stage] = stage
30
- end
31
- opts.on('-h') { puts opts; exit }
32
- opts.parse!(argv)
33
- end
34
- end
35
-
36
- def run
37
- Funktor::Deploy::Serverless.new(**options).call
38
- end
39
- end
40
- end
41
- end
42
-