add weekly maintenance script

This commit is contained in:
albert
2013-03-31 14:11:48 -04:00
parent b025256480
commit fc32a0023c
9 changed files with 96 additions and 13 deletions

View File

@@ -1,17 +1,20 @@
class ApiCacheGenerator
def generate_tag_cache
File.open("#{RAILS_ROOT}/public/cache/tags-legacy.xml", "w") do |f|
f.puts('<?xml version="1.0" encoding="UTF-8"?>')
f.puts('<tags type="array">')
File.open("#{Rails.root}/public/cache/tags.json", "w") do |f|
f.print("[")
Tag.find_each do |tag|
name = CGI.escape_html(tag.name)
id = tag.id.to_s
created_at = tag.created_at.try(:strftime, '%Y-%m-%d %H:%M')
post_count = tag.post_count.to_s
category = tag.category
f.puts('<tag name="' + name + '" id="' + id + '" ambiguous="false" created_at="' + created_at + '" count="' + post_count + '" type="' + category + '"></tag>')
hash = {
"name" => tag.name,
"id" => tag.id,
"created_at" => tag.created_at,
"post_count" => tag.post_count,
"category" => tag.category
}
f.print(hash.to_json)
f.print(", ")
end
f.puts('</tags>')
f.seek(-2, IO::SEEK_END)
f.print("]\n")
end
end
end

View File

@@ -5,7 +5,6 @@ class DailyMaintenance
Upload.delete_all(['created_at < ?', 1.day.ago])
ModAction.delete_all(['created_at < ?', 3.days.ago])
Delayed::Job.delete_all(['created_at < ?'], 1.day.ago)
ApiCacheGenerator.new.generate_tag_cache
end
def prune_ad_hits

View File

@@ -0,0 +1,5 @@
class DailyMaintenance
def run
ApiCacheGenerator.new.generate_tag_cache
end
end

3
app/models/key_value.rb Normal file
View File

@@ -0,0 +1,3 @@
class KeyValue < ActiveRecord::Base
validates_uniqueness_of :key
end

View File

@@ -18,6 +18,10 @@ every 1.day, :at => "1:00 am" do
command "cd /var/www/danbooru2/current ; script/donmai/prune_backup_dbs"
end
every 1.week, :at => "1:30 am" do
runner "WeeklyMaintenance.new.run"
end
every 8.hours do
command "psql --set statement_timeout=0 -hdbserver -c \"vacuum analyze;\" danbooru2"
end

View File

@@ -0,0 +1,11 @@
class CreateKeyValues < ActiveRecord::Migration
def change
create_table :key_values do |t|
t.string :key, :null => false
t.text :value
t.timestamps
end
add_index :key_values, :key, :unique => true
end
end

View File

@@ -1869,6 +1869,38 @@ CREATE SEQUENCE janitor_trials_id_seq
ALTER SEQUENCE janitor_trials_id_seq OWNED BY janitor_trials.id;
--
-- Name: key_values; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE key_values (
id integer NOT NULL,
key character varying(255) NOT NULL,
value text,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
--
-- Name: key_values_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE key_values_id_seq
START WITH 1
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;
--
-- Name: key_values_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE key_values_id_seq OWNED BY key_values.id;
--
-- Name: mod_actions; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
@@ -3565,6 +3597,13 @@ ALTER TABLE ONLY ip_bans ALTER COLUMN id SET DEFAULT nextval('ip_bans_id_seq'::r
ALTER TABLE ONLY janitor_trials ALTER COLUMN id SET DEFAULT nextval('janitor_trials_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY key_values ALTER COLUMN id SET DEFAULT nextval('key_values_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
@@ -3854,6 +3893,14 @@ ALTER TABLE ONLY janitor_trials
ADD CONSTRAINT janitor_trials_pkey PRIMARY KEY (id);
--
-- Name: key_values_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
ALTER TABLE ONLY key_values
ADD CONSTRAINT key_values_pkey PRIMARY KEY (id);
--
-- Name: mod_actions_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
@@ -5662,6 +5709,13 @@ CREATE UNIQUE INDEX index_ip_bans_on_ip_addr ON ip_bans USING btree (ip_addr);
CREATE INDEX index_janitor_trials_on_user_id ON janitor_trials USING btree (user_id);
--
-- Name: index_key_values_on_key; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE UNIQUE INDEX index_key_values_on_key ON key_values USING btree (key);
--
-- Name: index_news_updates_on_created_at; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@@ -6354,4 +6408,6 @@ INSERT INTO schema_migrations (version) VALUES ('20130323160259');
INSERT INTO schema_migrations (version) VALUES ('20130326035904');
INSERT INTO schema_migrations (version) VALUES ('20130328092739');
INSERT INTO schema_migrations (version) VALUES ('20130328092739');
INSERT INTO schema_migrations (version) VALUES ('20130331180246');

1
public/cache/tags.json vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -1,5 +1,6 @@
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment'))
require 'benchmark'
kv = KeyValue.find_or_create_by_key("ApiCacheGenerator.generate_tag_cache")
kv.update_attribute(:value, "0")