修订版 | 6f2e8ef61c84c48e99a058b072171d5df6751e7a (tree) |
---|---|
时间 | 2017-08-31 01:17:44 |
作者 | HMML <hmml3939@gmai...> |
Commiter | HMML |
Add crono for scheduled cleanup.
@@ -59,6 +59,8 @@ gem 'paperclip' | ||
59 | 59 | gem 'rqrcode', github: 'sugi/rqrcode', branch: 'inline-svg' |
60 | 60 | gem 'rubyzip', '>= 1.0.0', require: 'zip' |
61 | 61 | #gem 'counter_culture', '~> 1.0' |
62 | +gem 'crono' | |
63 | +gem 'sinatra', '>= 2.0.0', require: false | |
62 | 64 | |
63 | 65 | group :development, :test do |
64 | 66 | # Call 'byebug' anywhere in the code to stop execution and get a debugger console |
@@ -109,6 +109,9 @@ GEM | ||
109 | 109 | concurrent-ruby (1.0.5) |
110 | 110 | crack (0.4.3) |
111 | 111 | safe_yaml (~> 1.0.0) |
112 | + crono (1.1.2) | |
113 | + activerecord (>= 4.0) | |
114 | + activesupport (>= 4.0) | |
112 | 115 | daemons (1.2.4) |
113 | 116 | database_cleaner (1.6.1) |
114 | 117 | debug_inspector (0.0.3) |
@@ -229,6 +232,7 @@ GEM | ||
229 | 232 | minitest (5.10.3) |
230 | 233 | multi_json (1.12.1) |
231 | 234 | multipart-post (2.0.0) |
235 | + mustermann (1.0.0) | |
232 | 236 | mysql2 (0.4.8) |
233 | 237 | nenv (0.3.0) |
234 | 238 | net-scp (1.2.1) |
@@ -273,6 +277,8 @@ GEM | ||
273 | 277 | public_suffix (2.0.5) |
274 | 278 | puma (3.9.1) |
275 | 279 | rack (2.0.3) |
280 | + rack-protection (2.0.0) | |
281 | + rack | |
276 | 282 | rack-test (0.6.3) |
277 | 283 | rack (>= 1.0) |
278 | 284 | rails (5.1.3) |
@@ -359,6 +365,11 @@ GEM | ||
359 | 365 | simple_form (3.5.0) |
360 | 366 | actionpack (> 4, < 5.2) |
361 | 367 | activemodel (> 4, < 5.2) |
368 | + sinatra (2.0.0) | |
369 | + mustermann (~> 1.0) | |
370 | + rack (~> 2.0) | |
371 | + rack-protection (= 2.0.0) | |
372 | + tilt (~> 2.0) | |
362 | 373 | slop (3.6.0) |
363 | 374 | spring (2.0.2) |
364 | 375 | activesupport (>= 4.2) |
@@ -435,6 +446,7 @@ DEPENDENCIES | ||
435 | 446 | capistrano-rails |
436 | 447 | capybara (~> 2.13) |
437 | 448 | coffee-rails (~> 4.2) |
449 | + crono | |
438 | 450 | daemons |
439 | 451 | database_cleaner |
440 | 452 | delayed_job_active_record |
@@ -477,6 +489,7 @@ DEPENDENCIES | ||
477 | 489 | sdoc (~> 0.4.0) |
478 | 490 | selenium-webdriver |
479 | 491 | simple_form |
492 | + sinatra (>= 2.0.0) | |
480 | 493 | spring |
481 | 494 | spring-commands-rspec |
482 | 495 | spring-watcher-listen (~> 2.0.0) |
@@ -1,2 +1,3 @@ | ||
1 | +# Procfile for develoment servers | |
1 | 2 | web: bundle exec bin/rails s -b 0 -p 3000 |
2 | 3 | webpack: bundle exec bin/webpack-dev-server --host 0.0.0.0 |
@@ -0,0 +1,3 @@ | ||
1 | +# Procfile for job workers | |
2 | +crono: bundle exec crono run | |
3 | +job: bundle exec bin/delayed_job run |
@@ -0,0 +1,34 @@ | ||
1 | +# cronotab.rb — Crono configuration file | |
2 | +# | |
3 | +# Here you can specify periodic jobs and schedule. | |
4 | +# You can use ActiveJob's jobs from `app/jobs/` | |
5 | +# You can use any class. The only requirement is that | |
6 | +# class should have a method `perform` without arguments. | |
7 | +# | |
8 | +# class TestJob | |
9 | +# def perform | |
10 | +# puts 'Test!' | |
11 | +# end | |
12 | +# end | |
13 | +# | |
14 | +# Crono.perform(TestJob).every 2.days, at: '15:30' | |
15 | +# | |
16 | + | |
17 | +require 'rake' | |
18 | + | |
19 | +Rails.app_class.load_tasks | |
20 | + | |
21 | +class CleanupDrafts | |
22 | + def perform | |
23 | + Rake::Task['drafts:cleanup'].invoke | |
24 | + end | |
25 | +end | |
26 | + | |
27 | +class CleanupDistSignals | |
28 | + def perform | |
29 | + Rake::Task['dist_signals:cleanup'].invoke | |
30 | + end | |
31 | +end | |
32 | + | |
33 | +Crono.perform(CleanupDrafts).every 1.day | |
34 | +Crono.perform(CleanupDistSignals).every 1.day |
@@ -24,7 +24,7 @@ server docker: { | ||
24 | 24 | commit: 'dr.nemui.org/mmw-job', |
25 | 25 | volume: "#{Dir.pwd}:/src", |
26 | 26 | env: { |
27 | - APP_SERVER_CMD: "bundle exec bin/delayed_job run" | |
27 | + APP_SERVER_CMD: "foreman start -f Procfile.job" | |
28 | 28 | } |
29 | 29 | }, user: 'rails:rails', roles: :app |
30 | 30 |
@@ -24,7 +24,7 @@ server docker: { | ||
24 | 24 | commit: 'dr.nemui.org/mmw-stage-job', |
25 | 25 | volume: "#{Dir.pwd}:/src", |
26 | 26 | env: { |
27 | - APP_SERVER_CMD: "bundle exec bin/delayed_job run" | |
27 | + APP_SERVER_CMD: "foreman start -f Procfile.job" | |
28 | 28 | } |
29 | 29 | }, user: 'rails:rails', roles: :app |
30 | 30 |
@@ -27,4 +27,8 @@ Rails.application.routes.draw do | ||
27 | 27 | get 'logout', to: 'welcome#logout' |
28 | 28 | |
29 | 29 | root 'welcome#index' |
30 | + | |
31 | + authenticate :user, lambda { |u| u.is_admin? } do | |
32 | + mount Crono::Web, at: '/crono' | |
33 | + end | |
30 | 34 | end |
@@ -0,0 +1,16 @@ | ||
1 | +class CreateCronoJobs < ActiveRecord::Migration[5.1] | |
2 | + def self.up | |
3 | + create_table :crono_jobs do |t| | |
4 | + t.string :job_id, null: false | |
5 | + t.text :log, limit: 1073741823 # LONGTEXT for MySQL | |
6 | + t.datetime :last_performed_at | |
7 | + t.boolean :healthy | |
8 | + t.timestamps null: false | |
9 | + end | |
10 | + add_index :crono_jobs, [:job_id], unique: true | |
11 | + end | |
12 | + | |
13 | + def self.down | |
14 | + drop_table :crono_jobs | |
15 | + end | |
16 | +end |
@@ -10,7 +10,7 @@ | ||
10 | 10 | # |
11 | 11 | # It's strongly recommended that you check this file into your version control system. |
12 | 12 | |
13 | -ActiveRecord::Schema.define(version: 20170817080018) do | |
13 | +ActiveRecord::Schema.define(version: 20170830152444) do | |
14 | 14 | |
15 | 15 | create_table "areas", force: :cascade do |t| |
16 | 16 | t.string "ns" |
@@ -24,6 +24,16 @@ ActiveRecord::Schema.define(version: 20170817080018) do | ||
24 | 24 | t.index ["ns", "code"], name: "index_areas_on_ns_and_code", unique: true |
25 | 25 | end |
26 | 26 | |
27 | + create_table "crono_jobs", force: :cascade do |t| | |
28 | + t.string "job_id", null: false | |
29 | + t.text "log", limit: 1073741823 | |
30 | + t.datetime "last_performed_at" | |
31 | + t.boolean "healthy" | |
32 | + t.datetime "created_at", null: false | |
33 | + t.datetime "updated_at", null: false | |
34 | + t.index ["job_id"], name: "index_crono_jobs_on_job_id", unique: true | |
35 | + end | |
36 | + | |
27 | 37 | create_table "delayed_jobs", force: :cascade do |t| |
28 | 38 | t.integer "priority", default: 0, null: false |
29 | 39 | t.integer "attempts", default: 0, null: false |
@@ -0,0 +1,10 @@ | ||
1 | +namespace :dist_signals do | |
2 | + desc "Cleanup old dist signals" | |
3 | + task cleanup: :environment do | |
4 | + DistSignal.transaction do | |
5 | + count = DistSignal.where(status: %i(completed failed ignored)). | |
6 | + where("created_at < ?", 1.week.ago).delete_all | |
7 | + Rails.logger.info "Remove old #{count} dist signals" | |
8 | + end | |
9 | + end | |
10 | +end |