wolf_core 1.1.15 → 1.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45745f516e8eb69746381a70a65e22f580f3dda4b3600b6eda99eda75bf0f057
|
4
|
+
data.tar.gz: 1448150fb318029c96b9406852ac0326fd4f433af9226308441583a3511083d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd293374a86ee9166d0b820a0392493efa6828597c3e3a7d74e8337141782142e8303cb82d525e17cefe7d1e6dc15f479e15b17af295920339bb5995e4e78037
|
7
|
+
data.tar.gz: 32c9a77c6ba34ba5ffc8e5ea81a1294d65f7a13314489d6d8ee26125105dcd9a78a02117fe53cf5363bff0d1f6359c742f66eabd4d6d5aab1aeb4ba9ce123a07
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "time"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
module WolfCore
|
7
|
+
class JobSchedulerDataSource
|
8
|
+
attr_reader :default_group_name, :default_timezone
|
9
|
+
|
10
|
+
def initialize(region: "us-east-1", client: nil, default_group_name: "default", default_timezone: "UTC")
|
11
|
+
@client = client || Aws::Scheduler::Client.new(region: region)
|
12
|
+
@default_group_name = default_group_name
|
13
|
+
@default_timezone = default_timezone
|
14
|
+
end
|
15
|
+
|
16
|
+
def schedule_at(job_id:, run_at:, target:, config: {})
|
17
|
+
Result.try do
|
18
|
+
expr = at_expression(run_at)
|
19
|
+
normalized_target = normalize_target(target)
|
20
|
+
response = schedule_with_expression(job_id: job_id, expr: expr, target: normalized_target, config: config)
|
21
|
+
Result.success(data: { response: response })
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def schedule_recurring(job_id:, schedule:, target:, config: {})
|
26
|
+
Result.try do
|
27
|
+
expr = expression_from_schedule!(schedule)
|
28
|
+
normalized_target = normalize_target(target)
|
29
|
+
response = schedule_with_expression(job_id: job_id, expr: expr, target: normalized_target,
|
30
|
+
config: config)
|
31
|
+
Result.success(data: { response: response })
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def cancel_schedule(job_id:, options: {})
|
36
|
+
Result.try do
|
37
|
+
response = @client.delete_schedule(
|
38
|
+
name: job_id,
|
39
|
+
group_name: options[:group_name] || default_group_name
|
40
|
+
)
|
41
|
+
Result.success(data: { response: response })
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def get_schedule(job_id:, options: {})
|
46
|
+
Result.try do
|
47
|
+
response = @client.get_schedule(
|
48
|
+
name: job_id,
|
49
|
+
group_name: options[:group_name] || default_group_name
|
50
|
+
)
|
51
|
+
Result.success(data: { schedule: response })
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def create_schedule(name:, schedule_expression:, target:, options: {})
|
58
|
+
@client.create_schedule(
|
59
|
+
name: name,
|
60
|
+
group_name: options[:group_name],
|
61
|
+
schedule_expression: schedule_expression,
|
62
|
+
schedule_expression_timezone: options[:timezone],
|
63
|
+
flexible_time_window: { mode: "OFF" },
|
64
|
+
target: target,
|
65
|
+
description: options[:description],
|
66
|
+
state: "ENABLED",
|
67
|
+
client_token: options[:client_token]
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
def schedule_with_expression(job_id:, expr:, target:, config: {})
|
72
|
+
options = build_options(config)
|
73
|
+
create_schedule(name: job_id, schedule_expression: expr, target: target, options: options)
|
74
|
+
end
|
75
|
+
|
76
|
+
def normalize_target(target)
|
77
|
+
arn = target[:arn] || target[:target_arn]
|
78
|
+
role = target[:role_arn]
|
79
|
+
payload = target[:payload]
|
80
|
+
raise ArgumentError, "target[:arn] o target[:target_arn] es requerido" unless arn
|
81
|
+
raise ArgumentError, "target[:role_arn] es requerido" unless role
|
82
|
+
|
83
|
+
build_target(target_arn: arn, role_arn: role, payload: payload)
|
84
|
+
end
|
85
|
+
|
86
|
+
def build_options(config)
|
87
|
+
{
|
88
|
+
description: config[:description],
|
89
|
+
group_name: config[:group_name] || default_group_name,
|
90
|
+
timezone: config[:timezone] || default_timezone,
|
91
|
+
client_token: config[:client_token]
|
92
|
+
}
|
93
|
+
end
|
94
|
+
|
95
|
+
def expression_from_schedule!(schedule)
|
96
|
+
expr = schedule[:cron_expression] || schedule[:cron] || schedule[:rate_expression] || schedule[:rate]
|
97
|
+
raise ArgumentError, "Se requiere schedule[:cron] o schedule[:rate]" unless expr
|
98
|
+
|
99
|
+
expr
|
100
|
+
end
|
101
|
+
|
102
|
+
def build_target(target_arn:, role_arn:, payload: nil)
|
103
|
+
input = case payload
|
104
|
+
when nil then nil
|
105
|
+
when String then payload
|
106
|
+
else
|
107
|
+
payload.respond_to?(:to_json) ? payload.to_json : payload.to_s
|
108
|
+
end
|
109
|
+
|
110
|
+
target = { arn: target_arn, role_arn: role_arn }
|
111
|
+
target[:input] = input if input
|
112
|
+
target
|
113
|
+
end
|
114
|
+
|
115
|
+
def at_expression(run_at)
|
116
|
+
time = case run_at
|
117
|
+
when Time then run_at.utc
|
118
|
+
when DateTime then run_at.to_time.utc
|
119
|
+
when String then Time.parse(run_at).utc
|
120
|
+
else
|
121
|
+
raise ArgumentError, "run_at debe ser Time, DateTime o String ISO8601"
|
122
|
+
end
|
123
|
+
"at(#{time.iso8601})"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
data/lib/wolf_core/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wolf_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.17
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Javier Roncallo
|
@@ -143,6 +143,7 @@ files:
|
|
143
143
|
- lib/wolf_core/infrastructure/in_memory_storage_data_source.rb
|
144
144
|
- lib/wolf_core/infrastructure/in_memory_storage_operations.rb
|
145
145
|
- lib/wolf_core/infrastructure/instance_application_serializer.rb
|
146
|
+
- lib/wolf_core/infrastructure/job_scheduler_data_source.rb
|
146
147
|
- lib/wolf_core/infrastructure/lambda_function_data_source.rb
|
147
148
|
- lib/wolf_core/infrastructure/lambda_function_operations.rb
|
148
149
|
- lib/wolf_core/infrastructure/no_sql_db_data_source.rb
|