tasks_scheduler 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 599df8db33d832f04a2ad1a0581c7775fbd17f345da397e6b2e6e14090dfe9d1
4
- data.tar.gz: f20e6101384683f6d398ef55603f2a1d47d1e79dbf111237d68b2bfcc426292c
3
+ metadata.gz: 28ebfbf74a345e2c2e495dc2f5bb256f7180d4c58e31dd8575f70d23fe047933
4
+ data.tar.gz: a186a7918233e6287013b4cf6c956a4dcf09afcc459586a7a64a125908c4aacd
5
5
  SHA512:
6
- metadata.gz: ca786519acd5d3925cc521ffed0f3d61471d409abefe94b70b32002951ce27d51cc63435567b2b820d7be7315265f7d7a847723ba721835246531fb391474539
7
- data.tar.gz: 6e0e915ebd3b41d75499899b3155b33e71f34ad08fe80f03f30dc03dc75ead448acbd34dc71c6eb8ab38c8e9f49fd1c0f95c8922c4c76afb6ea9f3a30847368c
6
+ metadata.gz: 72198e57632d3ca14feedbb13e170fab472b52b4c9001b4b2649ff33fee266b64412b3897102f50cfd2a8d4b2284d3cc7a76244c96f424fcbd8b309085cbbe18
7
+ data.tar.gz: 848503897531631a676f7fa20fbaa8f252779c692f1e922f511350d667db5e2f52eb30dfdc5922b6fd0cf66ae952282da5de058dfa6d8530ab661c20af0e8ea4
@@ -1,115 +1,4 @@
1
1
  //= require active_scaffold
2
2
  //= require js-routes
3
-
4
- function TasksScheduler() {
5
- }
6
-
7
- TasksScheduler.Status = function () {
8
- };
9
-
10
- // Shortcut
11
- var _S = TasksScheduler.Status;
12
-
13
- _S.initialized = false;
14
-
15
- _S.init = function (url, interval_max) {
16
- if (!_S.initialized) {
17
- _S.initialized = true;
18
- _S.url = url;
19
- _S.interval_max = interval_max;
20
- _S.update();
21
- }
22
- };
23
-
24
- _S.content = function () {
25
- return $('#TaskScheduler_Status_Content');
26
- };
27
-
28
- _S.status = function () {
29
- return $('#TaskScheduler_Status_Status');
30
- };
31
-
32
- _S.update_status = function () {
33
- _S.status().html(
34
- "Updating in " + _S.interval + " seconds..."
35
- );
36
- };
37
-
38
- _S.check = function () {
39
- if (_S.interval <= 0) {
40
- _S.update();
41
- } else {
42
- _S.interval--;
43
- _S.update_status();
44
- setTimeout(_S.check, 1000);
45
- }
46
- };
47
-
48
- _S.update = function () {
49
- $.ajax({
50
- url: _S.url,
51
- success: function (result) {
52
- _S.content().html(result);
53
- },
54
- complete: function (result) {
55
- _S.interval = _S.interval_max + 1;
56
- _S.last_update = new Date();
57
- _S.check();
58
- }
59
- });
60
- };
61
-
62
- TasksScheduler.Alert = function () {
63
- };
64
-
65
- _A = TasksScheduler.Alert;
66
-
67
- _A.DEFAULT_REFRESH_INTERVAL = 5000;
68
- _A.DEFAULT_ELEMENT_SELECTOR = '#tasks_scheduler_alert';
69
- _A.CSS_CLASSES_PREFIX = 'alert_';
70
- _A.url = Routes.status_tasks_scheduler_daemon_path();
71
-
72
- _A.init = function (options) {
73
- options = typeof options !== 'undefined' ? options : {};
74
- $(document).ready(function () {
75
- _A.options = options;
76
- if (!_A.options.refresh_interval) {
77
- _A.options.refresh_interval = _A.DEFAULT_REFRESH_INTERVAL;
78
- }
79
- if (!_A.options.element_selector) {
80
- _A.options.element_selector = _A.DEFAULT_ELEMENT_SELECTOR;
81
- }
82
- _A.refresh();
83
- });
84
- };
85
-
86
- _A.setNextRefresh = function () {
87
- setTimeout(_A.refresh, _A.options.refresh_interval);
88
- };
89
-
90
- _A.refresh = function () {
91
- $.ajax({
92
- url: _A.url,
93
- success: function (result) {
94
- var alert = $(_A.options.element_selector);
95
- var pattern = new RegExp('(^|\\s)' + _A.CSS_CLASSES_PREFIX + "\\S+", 'g');
96
- alert.removeClass (function (index, className) {
97
- return (className.match (pattern) || []).join(' ');
98
- });
99
- alert.addClass(_A.resultToCssClass(result));
100
- },
101
- complete: function (result) {
102
- _A.setNextRefresh();
103
- }
104
- });
105
- };
106
-
107
- _A.resultToCssClass = function(result) {
108
- var suffix = "ok"
109
- if (!result.daemon_running) {
110
- suffix = "daemon_stopped";
111
- } else if (!result.tasks_all_ok) {
112
- suffix = "task_failed";
113
- }
114
- return _A.CSS_CLASSES_PREFIX + suffix;
115
- };
3
+ //= require tasks_scheduler/alert
4
+ //= require tasks_scheduler/status
@@ -0,0 +1,62 @@
1
+ if (typeof TasksScheduler == "undefined") {
2
+ TasksScheduler = function() {};
3
+ }
4
+
5
+ TasksScheduler.Alert = function () {
6
+ };
7
+
8
+ _A = TasksScheduler.Alert;
9
+
10
+ _A.DEFAULT_REFRESH_INTERVAL = 5000;
11
+ _A.DEFAULT_ELEMENT_SELECTOR = '#tasks_scheduler_alert';
12
+ _A.CSS_CLASSES_PREFIX = 'alert_';
13
+ _A.url = Routes.status_tasks_scheduler_daemon_path();
14
+
15
+ _A.init = function (options) {
16
+ options = typeof options !== 'undefined' ? options : {};
17
+ $(document).ready(function () {
18
+ _A.options = options;
19
+ if (!_A.options.refresh_interval) {
20
+ _A.options.refresh_interval = _A.DEFAULT_REFRESH_INTERVAL;
21
+ }
22
+ if (!_A.options.element_selector) {
23
+ _A.options.element_selector = _A.DEFAULT_ELEMENT_SELECTOR;
24
+ }
25
+ _A.refresh();
26
+ });
27
+ };
28
+
29
+ _A.setNextRefresh = function () {
30
+ setTimeout(_A.refresh, _A.options.refresh_interval);
31
+ };
32
+
33
+ _A.refresh = function () {
34
+ $.ajax(_A.refreshAjaxData());
35
+ };
36
+
37
+ _A.refreshAjaxData = function () {
38
+ return {
39
+ url: _A.url,
40
+ success: function (result) {
41
+ var alert = $(_A.options.element_selector);
42
+ var pattern = new RegExp('(^|\\s)' + _A.CSS_CLASSES_PREFIX + "\\S+", 'g');
43
+ alert.removeClass (function (index, className) {
44
+ return (className.match (pattern) || []).join(' ');
45
+ });
46
+ alert.addClass(_A.resultToCssClass(result));
47
+ },
48
+ complete: function (result) {
49
+ _A.setNextRefresh();
50
+ }
51
+ };
52
+ };
53
+
54
+ _A.resultToCssClass = function(result) {
55
+ var suffix = "ok"
56
+ if (!result.daemon_running) {
57
+ suffix = "daemon_stopped";
58
+ } else if (!result.tasks_all_ok) {
59
+ suffix = "task_failed";
60
+ }
61
+ return _A.CSS_CLASSES_PREFIX + suffix;
62
+ };
@@ -0,0 +1,62 @@
1
+ if (typeof TasksScheduler == "undefined") {
2
+ TasksScheduler = function() {};
3
+ }
4
+
5
+ TasksScheduler.Status = function () {
6
+ };
7
+
8
+ // Shortcut
9
+ var _S = TasksScheduler.Status;
10
+
11
+ _S.initialized = false;
12
+
13
+ _S.init = function (url, interval_max) {
14
+ if (!_S.initialized) {
15
+ _S.initialized = true;
16
+ _S.url = url;
17
+ _S.interval_max = interval_max;
18
+ _S.update();
19
+ }
20
+ };
21
+
22
+ _S.content = function () {
23
+ return $('#TaskScheduler_Status_Content');
24
+ };
25
+
26
+ _S.status = function () {
27
+ return $('#TaskScheduler_Status_Status');
28
+ };
29
+
30
+ _S.update_status = function () {
31
+ _S.status().html(
32
+ "Updating in " + _S.interval + " seconds..."
33
+ );
34
+ };
35
+
36
+ _S.check = function () {
37
+ if (_S.interval <= 0) {
38
+ _S.update();
39
+ } else {
40
+ _S.interval--;
41
+ _S.update_status();
42
+ setTimeout(_S.check, 1000);
43
+ }
44
+ };
45
+
46
+ _S.update = function () {
47
+ $.ajax(_S.updateAjaxData());
48
+ };
49
+
50
+ _S.updateAjaxData = function() {
51
+ return {
52
+ url: _S.url,
53
+ success: function (result) {
54
+ _S.content().html(result);
55
+ },
56
+ complete: function (result) {
57
+ _S.interval = _S.interval_max + 1;
58
+ _S.last_update = new Date();
59
+ _S.check();
60
+ }
61
+ };
62
+ };
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TasksSchedulerHelper
4
+ NAVBAR_ENTRIES = {
5
+ tasks_scheduler_daemon: 'tasks_scheduler_daemon',
6
+ scheduled_tasks: 'scheduled_tasks',
7
+ tasks_scheduler_status: :status_scheduled_tasks
8
+ }.freeze
9
+
10
+ def tasks_scheduler_navbar
11
+ content_tag(:navbar) do
12
+ safe_join(tasks_scheduler_navbar_entries.map { |label, path| link_to label, path }, ' | ')
13
+ end
14
+ end
15
+
16
+ def tasks_scheduler_navbar_entries
17
+ NAVBAR_ENTRIES
18
+ .map { |i18n_key, path_name| [::I18n.t(i18n_key), send("#{path_name}_path")] }
19
+ .to_h
20
+ end
21
+ end
@@ -61,6 +61,8 @@ class ScheduledTask < ActiveRecord::Base
61
61
  check_log("Running? #{process_running?}")
62
62
  check_log("Last fail status: #{last_fail_status}")
63
63
  check_log("Enabled: #{enabled?}")
64
+ check_log("Tasks running: #{::TasksScheduler::Info.tasks_running_current}/" +
65
+ ::TasksScheduler::Info.tasks_running_limit.to_s)
64
66
  end
65
67
 
66
68
  def check_task_without_next_run
@@ -72,11 +74,13 @@ class ScheduledTask < ActiveRecord::Base
72
74
  def check_task_with_next_run
73
75
  if !task_exist?
74
76
  check_on_task_not_exist
75
- elsif next_run < Time.zone.now
77
+ elsif next_run >= Time.zone.now
78
+ check_log('Next run not reached')
79
+ elsif !::TasksScheduler::Info.can_run_new_task?
80
+ check_log('Running tasks limit reached. Waiting...')
81
+ else
76
82
  check_log('Next run reached. Running...')
77
83
  spawn_task
78
- else
79
- check_log('Next run not reached')
80
84
  end
81
85
  end
82
86
 
@@ -1,2 +1,2 @@
1
- <%= render partial: '/tasks_scheduler/navbar' %>
1
+ <%= tasks_scheduler_navbar %>
2
2
  <%= render :super %>
@@ -1,3 +1,4 @@
1
+ <%= tasks_scheduler_navbar %>
1
2
  <p>Log file: <%= @log_file %></p>
2
3
  <% if File.exist?(@log_file) %>
3
4
  <p>Content: </p>
@@ -1,4 +1,4 @@
1
- <%= render partial: '/tasks_scheduler/navbar' %>
1
+ <%= tasks_scheduler_navbar %>
2
2
  <h2><%= I18n.t(:tasks_scheduler_status) %></h2>
3
3
  <div id="TaskScheduler_Status_Status">
4
4
  </div>
@@ -4,6 +4,10 @@
4
4
  <br/>
5
5
  <strong>Daemon status: </strong>
6
6
  <%= render partial: '/tasks_scheduler_daemon/running_status' %>
7
+ <br/>
8
+ <strong>Tasks running/limit: </strong>
9
+ <%= ::TasksScheduler::Info.tasks_running_current %> /
10
+ <%= ::TasksScheduler::Info.tasks_running_limit %>
7
11
  </p>
8
12
  <table>
9
13
  <thead>
@@ -1,4 +1,4 @@
1
- <%= render partial: '/tasks_scheduler/navbar' %>
1
+ <%= tasks_scheduler_navbar %>
2
2
  <h2><%= I18n.t(:tasks_scheduler_daemon) %></h2>
3
3
  <%= render partial: 'daemon', locals: {status_label: 'Status:'} %>
4
4
  <%= render partial: 'logs' %>
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Rails.application.routes.draw do
4
- resources(:scheduled_tasks) do
5
- as_routes
4
+ concern :active_scaffold, ActiveScaffold::Routing::Basic.new(association: true)
5
+ resources(:scheduled_tasks, concerns: :active_scaffold) do
6
6
  collection do
7
7
  get :status
8
8
  get :status_content
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class CreateScheduledTasks < (
4
- Rails.version < '5.2' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
4
+ Rails.version < '5' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
5
5
  )
6
6
  def change
7
7
  create_table :scheduled_tasks do |t|
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class AddStatusAttributesToScheduledTasks < (
4
- Rails.version < '5.2' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
4
+ Rails.version < '5' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
5
5
  )
6
6
  def change
7
7
  add_column :scheduled_tasks, :last_run_start, :datetime
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class AddPidToScheduledTasks < (
4
- Rails.version < '5.2' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
4
+ Rails.version < '5' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
5
5
  )
6
6
  def change
7
7
  add_column :scheduled_tasks, :pid, :integer
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class AddArgsToScheduledTasks < (
4
- Rails.version < '5.2' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
4
+ Rails.version < '5' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
5
5
  )
6
6
  def change
7
7
  add_column :scheduled_tasks, :args, :string
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class AddLastFailStatusToScheduledTasks < (
4
- Rails.version < '5.2' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
4
+ Rails.version < '5' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
5
5
  )
6
6
  def change
7
7
  add_column :scheduled_tasks, :last_fail_status, :string
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class AddEnabledToScheduledTasks < (
4
- Rails.version < '5.2' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
4
+ Rails.version < '5' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
5
5
  )
6
6
  def change
7
7
  add_column :scheduled_tasks, :enabled, :boolean, null: false, default: true
@@ -16,7 +16,7 @@ module TasksScheduler
16
16
  Signal.trap('TERM') { running = false }
17
17
  while running
18
18
  Rails.logger.info('Checking all tasks...')
19
- ::ScheduledTask.all.each(&:check)
19
+ ::ScheduledTask.all.order(next_run: :asc).each(&:check)
20
20
  Rails.logger.info("All tasks checked. Sleeping for #{CHECK_INTERVAL} second(s)...")
21
21
  sleep(CHECK_INTERVAL)
22
22
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TasksScheduler
4
+ class Info
5
+ TASKS_LIMIT_KEY = 'TASKS_SCHEDULER_TASKS_LIMIT'
6
+ TASKS_LIMIT_DEFAULT_VALUE = '-1'
7
+
8
+ class << self
9
+ def can_run_new_task?
10
+ return true if tasks_running_limit.negative?
11
+
12
+ tasks_running_current < tasks_running_limit
13
+ end
14
+
15
+ def tasks_running_current
16
+ ::ScheduledTask.all.select(&:process_running?).count
17
+ end
18
+
19
+ def tasks_running_limit
20
+ ENV[TASKS_LIMIT_KEY].if_present(TASKS_LIMIT_DEFAULT_VALUE, &:to_i)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TasksScheduler
4
- VERSION = '0.5.0'
4
+ VERSION = '0.6.0'
5
5
  end
@@ -0,0 +1,3 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../javascripts .js
3
+ //= link_directory ../stylesheets .css
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tasks_scheduler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo H. Bogoni
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-21 00:00:00.000000000 Z
11
+ date: 2020-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_scaffold
@@ -64,14 +64,20 @@ dependencies:
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '0.43'
67
+ version: '0.56'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 0.56.1
68
71
  type: :runtime
69
72
  prerelease: false
70
73
  version_requirements: !ruby/object:Gem::Requirement
71
74
  requirements:
72
75
  - - "~>"
73
76
  - !ruby/object:Gem::Version
74
- version: '0.43'
77
+ version: '0.56'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 0.56.1
75
81
  - !ruby/object:Gem::Dependency
76
82
  name: js-routes
77
83
  requirement: !ruby/object:Gem::Requirement
@@ -106,14 +112,14 @@ dependencies:
106
112
  requirements:
107
113
  - - ">="
108
114
  - !ruby/object:Gem::Version
109
- version: 4.2.11.3
115
+ version: 4.2.11
110
116
  type: :runtime
111
117
  prerelease: false
112
118
  version_requirements: !ruby/object:Gem::Requirement
113
119
  requirements:
114
120
  - - ">="
115
121
  - !ruby/object:Gem::Version
116
- version: 4.2.11.3
122
+ version: 4.2.11
117
123
  - !ruby/object:Gem::Dependency
118
124
  name: eac_ruby_gem_support
119
125
  requirement: !ruby/object:Gem::Requirement
@@ -161,12 +167,15 @@ files:
161
167
  - README.rdoc
162
168
  - Rakefile
163
169
  - app/assets/javascripts/tasks_scheduler.js
170
+ - app/assets/javascripts/tasks_scheduler/alert.js
171
+ - app/assets/javascripts/tasks_scheduler/status.js
164
172
  - app/assets/stylesheets/tasks_scheduler.scss
165
173
  - app/assets/stylesheets/tasks_scheduler/alert.scss
166
174
  - app/controllers/scheduled_tasks_controller.rb
167
175
  - app/controllers/tasks_scheduler_daemon_controller.rb
168
176
  - app/controllers/tasks_scheduler_daemon_controller/_download_log.rb
169
177
  - app/helpers/scheduled_tasks_helper.rb
178
+ - app/helpers/tasks_scheduler_helper.rb
170
179
  - app/models/scheduled_task.rb
171
180
  - app/models/scheduled_task/checker.rb
172
181
  - app/models/scheduled_task/log.rb
@@ -177,7 +186,6 @@ files:
177
186
  - app/views/scheduled_tasks/status.html.erb
178
187
  - app/views/scheduled_tasks/status_content.html.erb
179
188
  - app/views/tasks_scheduler/_alert.html.erb
180
- - app/views/tasks_scheduler/_navbar.html.erb
181
189
  - app/views/tasks_scheduler_daemon/_daemon.html.erb
182
190
  - app/views/tasks_scheduler_daemon/_logs.html.erb
183
191
  - app/views/tasks_scheduler_daemon/_results.html.erb
@@ -203,9 +211,11 @@ files:
203
211
  - lib/tasks_scheduler/cron_scheduling_validator.rb
204
212
  - lib/tasks_scheduler/daemon.rb
205
213
  - lib/tasks_scheduler/engine.rb
214
+ - lib/tasks_scheduler/info.rb
206
215
  - lib/tasks_scheduler/version.rb
207
216
  - test/dummy/README.rdoc
208
217
  - test/dummy/Rakefile
218
+ - test/dummy/app/assets/config/manifest.js
209
219
  - test/dummy/app/assets/javascripts/application.js
210
220
  - test/dummy/app/assets/stylesheets/application.css
211
221
  - test/dummy/app/controllers/application_controller.rb
@@ -291,6 +301,7 @@ test_files:
291
301
  - test/dummy/app/views/layouts/application.html.erb
292
302
  - test/dummy/app/controllers/application_controller.rb
293
303
  - test/dummy/app/helpers/application_helper.rb
304
+ - test/dummy/app/assets/config/manifest.js
294
305
  - test/dummy/app/assets/stylesheets/application.css
295
306
  - test/dummy/app/assets/javascripts/application.js
296
307
  - test/dummy/public/422.html
@@ -1,5 +0,0 @@
1
- <navbar>
2
- <%= link_to I18n.t(:tasks_scheduler_daemon), tasks_scheduler_daemon_path %> |
3
- <%= link_to I18n.t(:scheduled_tasks), scheduled_tasks_path %> |
4
- <%= link_to I18n.t(:tasks_scheduler_status), status_scheduled_tasks_path %>
5
- </navbar>