• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

修订版6f2e8ef61c84c48e99a058b072171d5df6751e7a (tree)
时间2017-08-31 01:17:44
作者HMML <hmml3939@gmai...>
CommiterHMML

Log Message

Add crono for scheduled cleanup.

更改概述

差异

--- a/Gemfile
+++ b/Gemfile
@@ -59,6 +59,8 @@ gem 'paperclip'
5959 gem 'rqrcode', github: 'sugi/rqrcode', branch: 'inline-svg'
6060 gem 'rubyzip', '>= 1.0.0', require: 'zip'
6161 #gem 'counter_culture', '~> 1.0'
62+gem 'crono'
63+gem 'sinatra', '>= 2.0.0', require: false
6264
6365 group :development, :test do
6466 # Call 'byebug' anywhere in the code to stop execution and get a debugger console
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -109,6 +109,9 @@ GEM
109109 concurrent-ruby (1.0.5)
110110 crack (0.4.3)
111111 safe_yaml (~> 1.0.0)
112+ crono (1.1.2)
113+ activerecord (>= 4.0)
114+ activesupport (>= 4.0)
112115 daemons (1.2.4)
113116 database_cleaner (1.6.1)
114117 debug_inspector (0.0.3)
@@ -229,6 +232,7 @@ GEM
229232 minitest (5.10.3)
230233 multi_json (1.12.1)
231234 multipart-post (2.0.0)
235+ mustermann (1.0.0)
232236 mysql2 (0.4.8)
233237 nenv (0.3.0)
234238 net-scp (1.2.1)
@@ -273,6 +277,8 @@ GEM
273277 public_suffix (2.0.5)
274278 puma (3.9.1)
275279 rack (2.0.3)
280+ rack-protection (2.0.0)
281+ rack
276282 rack-test (0.6.3)
277283 rack (>= 1.0)
278284 rails (5.1.3)
@@ -359,6 +365,11 @@ GEM
359365 simple_form (3.5.0)
360366 actionpack (> 4, < 5.2)
361367 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)
362373 slop (3.6.0)
363374 spring (2.0.2)
364375 activesupport (>= 4.2)
@@ -435,6 +446,7 @@ DEPENDENCIES
435446 capistrano-rails
436447 capybara (~> 2.13)
437448 coffee-rails (~> 4.2)
449+ crono
438450 daemons
439451 database_cleaner
440452 delayed_job_active_record
@@ -477,6 +489,7 @@ DEPENDENCIES
477489 sdoc (~> 0.4.0)
478490 selenium-webdriver
479491 simple_form
492+ sinatra (>= 2.0.0)
480493 spring
481494 spring-commands-rspec
482495 spring-watcher-listen (~> 2.0.0)
--- a/Procfile
+++ b/Procfile
@@ -1,2 +1,3 @@
1+# Procfile for develoment servers
12 web: bundle exec bin/rails s -b 0 -p 3000
23 webpack: bundle exec bin/webpack-dev-server --host 0.0.0.0
--- /dev/null
+++ b/Procfile.job
@@ -0,0 +1,3 @@
1+# Procfile for job workers
2+crono: bundle exec crono run
3+job: bundle exec bin/delayed_job run
--- /dev/null
+++ b/config/cronotab.rb
@@ -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
--- a/config/deploy/docker-production.rb
+++ b/config/deploy/docker-production.rb
@@ -24,7 +24,7 @@ server docker: {
2424 commit: 'dr.nemui.org/mmw-job',
2525 volume: "#{Dir.pwd}:/src",
2626 env: {
27- APP_SERVER_CMD: "bundle exec bin/delayed_job run"
27+ APP_SERVER_CMD: "foreman start -f Procfile.job"
2828 }
2929 }, user: 'rails:rails', roles: :app
3030
--- a/config/deploy/docker-staging.rb
+++ b/config/deploy/docker-staging.rb
@@ -24,7 +24,7 @@ server docker: {
2424 commit: 'dr.nemui.org/mmw-stage-job',
2525 volume: "#{Dir.pwd}:/src",
2626 env: {
27- APP_SERVER_CMD: "bundle exec bin/delayed_job run"
27+ APP_SERVER_CMD: "foreman start -f Procfile.job"
2828 }
2929 }, user: 'rails:rails', roles: :app
3030
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -27,4 +27,8 @@ Rails.application.routes.draw do
2727 get 'logout', to: 'welcome#logout'
2828
2929 root 'welcome#index'
30+
31+ authenticate :user, lambda { |u| u.is_admin? } do
32+ mount Crono::Web, at: '/crono'
33+ end
3034 end
--- /dev/null
+++ b/db/migrate/20170830152444_create_crono_jobs.rb
@@ -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
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
1010 #
1111 # It's strongly recommended that you check this file into your version control system.
1212
13-ActiveRecord::Schema.define(version: 20170817080018) do
13+ActiveRecord::Schema.define(version: 20170830152444) do
1414
1515 create_table "areas", force: :cascade do |t|
1616 t.string "ns"
@@ -24,6 +24,16 @@ ActiveRecord::Schema.define(version: 20170817080018) do
2424 t.index ["ns", "code"], name: "index_areas_on_ns_and_code", unique: true
2525 end
2626
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+
2737 create_table "delayed_jobs", force: :cascade do |t|
2838 t.integer "priority", default: 0, null: false
2939 t.integer "attempts", default: 0, null: false
--- /dev/null
+++ b/lib/tasks/cleanup_dist_signals.rake
@@ -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