tasks_scheduler 0.0.2

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.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +33 -0
  5. data/app/assets/javascripts/tasks_scheduler.js +59 -0
  6. data/app/assets/stylesheets/tasks_scheduler.scss +34 -0
  7. data/app/controllers/scheduled_tasks_controller.rb +45 -0
  8. data/app/helpers/scheduled_tasks_helper.rb +17 -0
  9. data/app/models/scheduled_task.rb +61 -0
  10. data/app/models/scheduled_task/checker.rb +54 -0
  11. data/app/models/scheduled_task/log.rb +28 -0
  12. data/app/models/scheduled_task/runner.rb +50 -0
  13. data/app/models/scheduled_task/status.rb +53 -0
  14. data/app/views/scheduled_tasks/log.html.erb +9 -0
  15. data/app/views/scheduled_tasks/status.html.erb +7 -0
  16. data/app/views/scheduled_tasks/status_content.html.erb +47 -0
  17. data/config/initializers/assets.rb +1 -0
  18. data/config/locales/en.yml +22 -0
  19. data/config/locales/pt-BR.yml +22 -0
  20. data/config/routes.rb +13 -0
  21. data/db/migrate/20161122123828_create_scheduled_tasks.rb +11 -0
  22. data/db/migrate/20161123130153_add_status_attributes_to_scheduled_tasks.rb +9 -0
  23. data/db/migrate/20161124200712_add_pid_to_scheduled_tasks.rb +5 -0
  24. data/db/migrate/20161128163702_add_args_to_scheduled_tasks.rb +5 -0
  25. data/exe/tasks_scheduler +19 -0
  26. data/exe/tasks_scheduler_run_task +14 -0
  27. data/lib/tasks_scheduler.rb +9 -0
  28. data/lib/tasks_scheduler/checker.rb +20 -0
  29. data/lib/tasks_scheduler/cron_parser_patch.rb +27 -0
  30. data/lib/tasks_scheduler/cron_scheduling_validator.rb +18 -0
  31. data/lib/tasks_scheduler/engine.rb +9 -0
  32. data/lib/tasks_scheduler/version.rb +3 -0
  33. data/test/dummy/README.rdoc +28 -0
  34. data/test/dummy/Rakefile +6 -0
  35. data/test/dummy/app/assets/javascripts/application.js +13 -0
  36. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  37. data/test/dummy/app/controllers/application_controller.rb +5 -0
  38. data/test/dummy/app/helpers/application_helper.rb +2 -0
  39. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  40. data/test/dummy/bin/bundle +3 -0
  41. data/test/dummy/bin/rails +4 -0
  42. data/test/dummy/bin/rake +4 -0
  43. data/test/dummy/bin/setup +29 -0
  44. data/test/dummy/config.ru +4 -0
  45. data/test/dummy/config/application.rb +25 -0
  46. data/test/dummy/config/boot.rb +5 -0
  47. data/test/dummy/config/database.yml +25 -0
  48. data/test/dummy/config/environment.rb +5 -0
  49. data/test/dummy/config/environments/development.rb +41 -0
  50. data/test/dummy/config/environments/production.rb +81 -0
  51. data/test/dummy/config/environments/test.rb +42 -0
  52. data/test/dummy/config/initializers/assets.rb +11 -0
  53. data/test/dummy/config/initializers/backtrace_silencers.rb +9 -0
  54. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  55. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  56. data/test/dummy/config/initializers/inflections.rb +16 -0
  57. data/test/dummy/config/initializers/mime_types.rb +4 -0
  58. data/test/dummy/config/initializers/session_store.rb +3 -0
  59. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  60. data/test/dummy/config/locales/en.yml +23 -0
  61. data/test/dummy/config/routes.rb +56 -0
  62. data/test/dummy/config/secrets.yml +22 -0
  63. data/test/dummy/db/schema.rb +31 -0
  64. data/test/dummy/public/404.html +67 -0
  65. data/test/dummy/public/422.html +67 -0
  66. data/test/dummy/public/500.html +66 -0
  67. data/test/dummy/public/favicon.ico +0 -0
  68. data/test/fixtures/scheduled_tasks.yml +4 -0
  69. data/test/models/scheduled_task_test.rb +72 -0
  70. data/test/test_helper.rb +36 -0
  71. metadata +223 -0
@@ -0,0 +1,9 @@
1
+ <p>Log file: <%= @log_file %></p>
2
+ <% if File.exist?(@log_file) %>
3
+ <p>Content: </p>
4
+ <pre>
5
+ <%= File.read(@log_file) %>
6
+ </pre>
7
+ <% else %>
8
+ <p><em>File does not exist.</em></p>
9
+ <% end %>
@@ -0,0 +1,7 @@
1
+ <div id="TaskScheduler_Status_Status">
2
+ </div>
3
+ <div id="TaskScheduler_Status_Content">
4
+ </div>
5
+ <script>
6
+ TasksScheduler.Status.init('<%= status_content_scheduled_tasks_path %>', 5);
7
+ </script>
@@ -0,0 +1,47 @@
1
+ <p>
2
+ <strong>Last update: </strong>
3
+ <%= I18n.l(Time.zone.now, format: :short) %>
4
+ </p>
5
+ <table>
6
+ <thead>
7
+ <tr>
8
+ <th rowspan="2"><%= I18n.t('helpers.label.scheduled_task.task') %></th>
9
+ <th rowspan="2"><%= I18n.t('helpers.label.scheduled_task.scheduling') %></th>
10
+ <th rowspan="2"><%= I18n.t('helpers.label.scheduled_task.args') %></th>
11
+ <th rowspan="2"><%= I18n.t('helpers.label.scheduled_task.next_run') %></th>
12
+ <th rowspan="2"><%= I18n.t('helpers.label.scheduled_task.status') %></th>
13
+ <th colspan="2"><%= I18n.t('current_run') %></th>
14
+ <th colspan="3"><%= I18n.t('last_successful_run') %></th>
15
+ <th colspan="3"><%= I18n.t('last_unsuccessful_run') %></th>
16
+ </tr>
17
+ <tr>
18
+ <th><%= I18n.t('start') %></th>
19
+ <th><%= I18n.t('log') %></th>
20
+ <th><%= I18n.t('start') %></th>
21
+ <th><%= I18n.t('end') %></th>
22
+ <th><%= I18n.t('log') %></th>
23
+ <th><%= I18n.t('start') %></th>
24
+ <th><%= I18n.t('end') %></th>
25
+ <th><%= I18n.t('log') %></th>
26
+ </tr>
27
+ </thead>
28
+ <tbody>
29
+ <% @scheduled_tasks.each do |st| %>
30
+ <tr>
31
+ <td style="text-align: left"><%= st.task %></td>
32
+ <td style="text-align: left"><%= st.scheduling %></td>
33
+ <td style="text-align: left"><%= st.args %></td>
34
+ <td><%= scheduled_tasks_status_time(st.next_run) %></td>
35
+ <td class="status_<%= st.status %>"><%= st.status.upcase %></td>
36
+ <td><%= scheduled_tasks_status_time(st.last_run_start) %></td>
37
+ <td><%= scheduled_tasks_log(st, 'running') %></td>
38
+ <td><%= scheduled_tasks_status_time(st.last_run_successful_start) %></td>
39
+ <td><%= scheduled_tasks_status_time(st.last_run_successful_end) %></td>
40
+ <td><%= scheduled_tasks_log(st, 'successful') %></td>
41
+ <td><%= scheduled_tasks_status_time(st.last_run_unsuccessful_start) %></td>
42
+ <td><%= scheduled_tasks_status_time(st.last_run_unsuccessful_end) %></td>
43
+ <td><%= scheduled_tasks_log(st, 'unsuccessful') %></td>
44
+ </tr>
45
+ <% end %>
46
+ </tbody>
47
+ </table>
@@ -0,0 +1 @@
1
+ Rails.application.config.assets.precompile += %w( active_scaffold/indicator.gif )
@@ -0,0 +1,22 @@
1
+ en:
2
+ activerecord:
3
+ models:
4
+ scheduled_task:
5
+ one: Scheduled task
6
+ other: Scheduled tasks
7
+ helpers:
8
+ label:
9
+ scheduled_task:
10
+ args: Arguments
11
+ scheduling: Scheduling
12
+ task: Task
13
+ next_run: Next run
14
+ status: Status
15
+ current_run: Current run
16
+ end: End
17
+ last_successful_run: Last successful run
18
+ last_unsuccessful_run: Last unsuccessful run
19
+ log: Log
20
+ run_now: Run now
21
+ start: Start
22
+ tasks_scheduler_status: Scheduled tasks - Status
@@ -0,0 +1,22 @@
1
+ pt-BR:
2
+ activerecord:
3
+ models:
4
+ scheduled_task:
5
+ one: Tarefa agendada
6
+ other: Tarefas agendadas
7
+ helpers:
8
+ label:
9
+ scheduled_task:
10
+ args: Parâmetros
11
+ scheduling: Agendamento
12
+ task: Tarefa
13
+ next_run: Próxima execução
14
+ status: Status
15
+ current_run: Execução atual
16
+ end: Término
17
+ last_successful_run: Última execução com sucesso
18
+ last_unsuccessful_run: Última execução com falha
19
+ log: Log
20
+ run_now: Executar agora
21
+ start: Início
22
+ tasks_scheduler_status: Tarefas agendadas - Status
@@ -0,0 +1,13 @@
1
+ Rails.application.routes.draw do
2
+ resources(:scheduled_tasks) do
3
+ as_routes
4
+ collection do
5
+ get :status
6
+ get :status_content
7
+ end
8
+ member do
9
+ get :log
10
+ put :run_now
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ class CreateScheduledTasks < ActiveRecord::Migration
2
+ def change
3
+ create_table :scheduled_tasks do |t|
4
+ t.string :scheduling
5
+ t.string :task
6
+ t.datetime :next_run
7
+
8
+ t.timestamps null: false
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class AddStatusAttributesToScheduledTasks < ActiveRecord::Migration
2
+ def change
3
+ add_column :scheduled_tasks, :last_run_start, :datetime
4
+ add_column :scheduled_tasks, :last_run_successful_start, :datetime
5
+ add_column :scheduled_tasks, :last_run_successful_end, :datetime
6
+ add_column :scheduled_tasks, :last_run_unsuccessful_start, :datetime
7
+ add_column :scheduled_tasks, :last_run_unsuccessful_end, :datetime
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ class AddPidToScheduledTasks < ActiveRecord::Migration
2
+ def change
3
+ add_column :scheduled_tasks, :pid, :integer
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class AddArgsToScheduledTasks < ActiveRecord::Migration
2
+ def change
3
+ add_column :scheduled_tasks, :args, :string
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'daemons'
5
+ require 'fileutils'
6
+
7
+ def find_rails_root(dir = Dir.pwd)
8
+ fail 'Rails root not found' if dir == '' || dir == '/' || dir == '.'
9
+ return dir if File.exist?(File.expand_path('config/environment.rb', dir))
10
+ rails_root(File.dirname(dir))
11
+ end
12
+
13
+ rails_root = find_rails_root
14
+ dir = File.expand_path('tmp/pids', rails_root)
15
+ FileUtils.mkdir_p(dir)
16
+ Daemons.run_proc 'tasks_scheduler', dir_mode: :normal, dir: dir do
17
+ require File.join(rails_root, 'config', 'environment')
18
+ ::TasksScheduler::Checker.instance.run
19
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'daemons'
5
+
6
+ def find_rails_root(dir = Dir.pwd)
7
+ fail 'Rails root not found' if dir == '' || dir == '/' || dir == '.'
8
+ return dir if File.exist?(File.expand_path('config/environment.rb', dir))
9
+ rails_root(File.dirname(dir))
10
+ end
11
+
12
+ rails_root = find_rails_root
13
+ require File.join(rails_root, 'config', 'environment')
14
+ ::ScheduledTask.find(ARGV[0].to_i).run
@@ -0,0 +1,9 @@
1
+ require 'tasks_scheduler/engine'
2
+ require 'active_scaffold'
3
+ require 'parse-cron'
4
+ require 'tasks_scheduler/cron_scheduling_validator'
5
+ require 'tasks_scheduler/cron_parser_patch'
6
+ require 'tasks_scheduler/checker'
7
+
8
+ module TasksScheduler
9
+ end
@@ -0,0 +1,20 @@
1
+ module TasksScheduler
2
+ class Checker
3
+ include Singleton
4
+
5
+ CHECK_INTERVAL = 15
6
+
7
+ def run
8
+ running = true
9
+ Signal.trap('TERM') do
10
+ running = false
11
+ end
12
+ while running
13
+ Rails.logger.info('Checking all tasks...')
14
+ ::ScheduledTask.all.each(&:check)
15
+ Rails.logger.info("All tasks checked. Sleeping for #{CHECK_INTERVAL} second(s)...")
16
+ sleep(CHECK_INTERVAL)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ module TasksScheduler
2
+ module CronParserPatch
3
+ class TasksSchedulerTimeSource
4
+ class << self
5
+ def local(year, month, day, hour, min, second)
6
+ Time.utc(year, month, day, hour, min, second)
7
+ end
8
+
9
+ def now
10
+ Time.now.utc
11
+ end
12
+ end
13
+ end
14
+
15
+ def self.included(base)
16
+ base.class_eval do
17
+ def self.new(source, time_source = TasksSchedulerTimeSource)
18
+ super
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ unless ::CronParser.included_modules.include?(::TasksScheduler::CronParserPatch)
26
+ ::CronParser.include(::TasksScheduler::CronParserPatch)
27
+ end
@@ -0,0 +1,18 @@
1
+ module TasksScheduler
2
+ class CronSchedulingValidator < ActiveModel::EachValidator
3
+ def validate_each(record, attribute, value)
4
+ return if value_valid?(value)
5
+ record.errors[attribute] << (options[:message] ||
6
+ I18n.translate(:cron_scheduling_validator_error_message))
7
+ end
8
+
9
+ private
10
+
11
+ def value_valid?(value)
12
+ ::CronParser.new(value).next
13
+ true
14
+ rescue ArgumentError
15
+ false
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module TasksScheduler
2
+ class Engine < ::Rails::Engine
3
+ initializer :append_migrations do |app|
4
+ config.paths['db/migrate'].expanded.each do |expanded_path|
5
+ app.config.paths['db/migrate'] << expanded_path
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module TasksScheduler
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pathname'
3
+
4
+ # path to your application root.
5
+ APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
6
+
7
+ Dir.chdir APP_ROOT do
8
+ # This script is a starting point to setup your application.
9
+ # Add necessary setup steps to this file:
10
+
11
+ puts '== Installing dependencies =='
12
+ system 'gem install bundler --conservative'
13
+ system 'bundle check || bundle install'
14
+
15
+ # puts "\n== Copying sample files =="
16
+ # unless File.exist?("config/database.yml")
17
+ # system "cp config/database.yml.sample config/database.yml"
18
+ # end
19
+
20
+ puts "\n== Preparing database =="
21
+ system 'bin/rake db:setup'
22
+
23
+ puts "\n== Removing old logs and tempfiles =="
24
+ system 'rm -f log/*'
25
+ system 'rm -rf tmp/cache'
26
+
27
+ puts "\n== Restarting application server =="
28
+ system 'touch tmp/restart.txt'
29
+ end