Skip to content

Commit 0aea32f

Browse files
committed
fix: news タスクの役割を明確に分離
- news:fetch: RSS → YAML(フィード取得) - news:upsert: YAML → DB(データベース投入) dojos タスクと同様の明確な役割分離を実現: - dojos:update_db_by_yaml (YAML → DB) - dojos:migrate_adding_id_to_yaml (DB → YAML) upsert の命名規則に従い、YAML からの DB 投入を upsert とする。
1 parent 4cc74c0 commit 0aea32f

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

lib/tasks/news.rake

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ def item_to_hash(item)
3737
end
3838

3939
namespace :news do
40-
desc 'RSS フィードから最新ニュースを取得してデータベースに upsert'
41-
task upsert: :environment do
40+
desc 'RSS フィードを取得し、db/news.yml に保存'
41+
task fetch: :environment do
4242
# ロガー設定(ファイル+コンソール出力)
4343
file_logger = ActiveSupport::Logger.new('log/news.log')
4444
console = ActiveSupport::Logger.new(STDOUT)
4545
logger = ActiveSupport::BroadcastLogger.new(file_logger, console)
4646

47-
logger.info('==== START news:upsert ====')
47+
logger.info('==== START news:fetch ====')
4848

4949
# 既存の news.yml を読み込み
5050
yaml_path = Rails.root.join('db', 'news.yml')
@@ -125,9 +125,21 @@ namespace :news do
125125
end
126126

127127
logger.info("✅ Wrote #{sorted_items.size} items to db/news.yml (#{truly_new_items_sorted.size} new, #{updated_items.size} updated)")
128+
logger.info('==== END news:fetch ====')
129+
end
128130

129-
# データベースへの upsert 処理(統合部分)
130-
entries = sorted_items
131+
desc 'db/news.yml からデータベースに upsert'
132+
task upsert: :environment do
133+
file_logger = ActiveSupport::Logger.new('log/news.log')
134+
console = ActiveSupport::Logger.new(STDOUT)
135+
logger = ActiveSupport::BroadcastLogger.new(file_logger, console)
136+
137+
logger.info "==== START news:upsert ===="
138+
139+
yaml_path = Rails.root.join('db', 'news.yml')
140+
raw = YAML.safe_load(File.read(yaml_path), permitted_classes: [Time], aliases: true)
141+
142+
entries = raw['news'] || []
131143
new_count = 0
132144
updated_count = 0
133145

@@ -152,8 +164,8 @@ namespace :news do
152164
end
153165
end
154166

155-
logger.info "Imported #{new_count + updated_count} items to database (#{new_count} new, #{updated_count} updated)."
156-
logger.info('==== END news:upsert ====')
167+
logger.info "Upserted #{new_count + updated_count} items (#{new_count} new, #{updated_count} updated)."
168+
logger.info "==== END news:upsert ===="
157169
end
158170

159-
end
171+
end

0 commit comments

Comments
 (0)