Skip to content

Commit

Permalink
Calculate sleep time based on clock
Browse files Browse the repository at this point in the history
Speeds up dumping somewhat because processing and media download time is subtracted from the sleeping time; only the time actually necessary for complying with chunk_delay is spent sleeping.
  • Loading branch information
tvdstaaij committed Mar 4, 2017
1 parent 14b3d67 commit 601b776
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion telegram-history-dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'json'
require 'logger'
require 'socket'
require 'time'
require 'timeout'
require 'yaml'
require_relative 'dumpers/json'
Expand Down Expand Up @@ -73,6 +74,7 @@ def dump_dialog(dialog)
])
msg_chunk = nil
retry_count = 0
last_chunk_download_time = Time.now
loop do
if retry_count >= $config['chunk_retry']
$log.error('Failed to fetch chunk of %d messages from offset %d '\
Expand All @@ -84,6 +86,7 @@ def dump_dialog(dialog)
offset += $config['chunk_size']
break
end
last_chunk_download_time = Time.now
begin
Timeout::timeout($config['chunk_timeout']) do
msg_chunk = exec_tg_command('history', dialog['print_name'],
Expand Down Expand Up @@ -153,7 +156,11 @@ def dump_dialog(dialog)
end

keep_dumping = false if offset < cur_offset + $config['chunk_size']
sleep($config['chunk_delay']) if keep_dumping
if keep_dumping
time_to_sleep = last_chunk_download_time - Time.now +
$config['chunk_delay']
sleep(time_to_sleep) if time_to_sleep > 0
end
end
state = $dumper.end_dialog(dialog) || {}
cur_progress.dumper_state=(state)
Expand Down

0 comments on commit 601b776

Please sign in to comment.