discourse/spec/system/helpers/downloads.rb
Andrei Prigorshnev fbe0e4c78c
DEV: make sure we don't load all data into memory when exporting chat messages (#22276)
This commit makes sure we don't load all data into memory when doing CSV exports. 
The most important change here made to the recently introduced export of chat 
messages (3ea31f4). We were loading all data into memory in the first version, with 
this commit it's not the case anymore.

Speaking of old exports. Some of them already use find_each, and it worked as 
expected, without loading all data into memory. And it will proceed working as 
expected after this commit.

In general, I made sure this change didn't break other CSV exports, first manually, and 
then by writing system specs for them. Sadly, I haven't managed yet to make those 
specs stable, they work fine locally, but flaky in GitHub actions, so I've disabled them 
for now.

I'll be making more changes to the CSV exports code soon, those system specs will be 
very helpful. I'll be running them locally, and I hope I'll manage to make them stable 
while doing that work.
2023-07-12 18:52:18 +04:00

31 lines
526 B
Ruby

# frozen_string_literal: true
class Downloads
FOLDER = Rails.root.join("tmp/downloads")
TIMEOUT = 10
def self.wait_for_download
Timeout.timeout(TIMEOUT) { sleep 0.1 until downloaded? }
end
def self.clear
FileUtils.rm_rf(FOLDER)
end
private
def self.downloaded?
!downloading? && downloads.any?
end
def self.downloading?
downloads.grep(/\.crdownload$/).any?
end
def self.downloads
Dir[FOLDER.join("*")]
end
private_class_method :downloaded?, :downloading?, :downloads
end