Skip to content

Commit

Permalink
More output when exceptions are caught
Browse files Browse the repository at this point in the history
  • Loading branch information
eschnett committed Mar 28, 2019
1 parent 723cfe9 commit 5ccd50d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
22 changes: 11 additions & 11 deletions src/DropboxCLI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,9 @@ function cmd_put(args)
end

upload_channel = Channel{Tuple{String, String}}(1000)
# upload_task = schedule(@task upload_many_files(auth, upload_channel))
upload_task = start_task(() -> upload_many_files(auth, upload_channel))
upload_task =
start_task(() -> upload_many_files(auth, upload_channel),
(:cmd_put, :upload_many_files, sources, destination))
for source in sources
# Distinguish between files and directories
if !ispath(source)
Expand Down Expand Up @@ -676,10 +677,9 @@ function upload_many_files(auth::Authorization,
while isready(upload_channel) || isopen(upload_channel)

upload_spec_channel = Channel{UploadSpec}(0)
# upload_spec_task =
# schedule(@task files_upload(auth, upload_spec_channel))
upload_spec_task =
start_task(() -> files_upload(auth, upload_spec_channel))
start_task(() -> files_upload(auth, upload_spec_channel),
(:upload_many_files, :files_upload))

max_tasks = 4
tasks = Task[]
Expand All @@ -691,12 +691,11 @@ function upload_many_files(auth::Authorization,
# TODO: run these truly in parallel, using multiple
# processes (this requires transferring the files_upload
# state to other processes)
# push!(tasks,
# schedule(@task upload_one_file(auth, i, source, destination,
# upload_spec_channel)))
push!(tasks,
start_task(() -> upload_one_file(auth, i, source, destination,
upload_spec_channel)))
upload_spec_channel),
(:upload_many_files, :upload_one_files,
i, source, destination)))
while length(tasks) >= max_tasks
yield()
filter!(!istaskdone, tasks)
Expand Down Expand Up @@ -743,9 +742,10 @@ function upload_one_file(auth::Authorization,
# content_hash = calc_content_hash(content)
# Read in chunks
data_channel = Channel{Vector{UInt8}}(0)
# content_hash_task = schedule(@task calc_content_hash(data_channel))
content_hash_task =
start_task(() -> calc_content_hash(data_channel))
start_task(() -> calc_content_hash(data_channel),
(:upload_one_file, :calc_content_hash,
i, source, destination))
open(source, "r") do io
while !eof(io)
chunksize = 4 * 1024 * 1024
Expand Down
24 changes: 14 additions & 10 deletions src/DropboxSDK.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ Create a new task from `fun` and schedule the task, similar to the
`@task` macro. Also register a hook that shows exception and backtrace
if the task fails.
"""
function start_task(fun::Function)::Task
function start_task(fun::Function, info=nothing)::Task
function wrap()
try
fun()
catch ex
println("Caught excpetion in task")
exstr = string(ex)
println(exstr[1:min(10000, end)])
println(catch_backtrace())
if info !== nothing
println("Context: ", string(info))
end
println("Exception: ", string(ex)[1:min(10000, end)])
println("Backtrace: ", catch_backtrace())
rethrow()
end
end
Expand Down Expand Up @@ -573,8 +575,8 @@ function files_upload(auth::Authorization,
session_id = nothing
offset = Int64(0)
data_channel = Channel{Vector{UInt8}}(0)
# content_hash_task = schedule(@task calc_content_hash(data_channel))
content_hash_task = start_task(() -> calc_content_hash(data_channel))
content_hash_task = start_task(() -> calc_content_hash(data_channel),
(:files_upload, :calc_content_hash, path))
for chunk in content_channel
isempty(chunk) && continue
if session_id === nothing
Expand Down Expand Up @@ -689,9 +691,10 @@ function files_upload(auth::Authorization,
# TODO: parallelize loop
upload_tasks = Task[]
for upload_spec in upload_spec_channel
# push!(upload_tasks, schedule(@task upload_one_file(auth, upload_spec)))
push!(upload_tasks,
start_task(() -> upload_one_file(auth, upload_spec)))
start_task(() -> upload_one_file(auth, upload_spec),
(:files_upload, :upload_one_file,
upload_spec.destination)))
end
upload_states = UploadState[]
for t in upload_tasks
Expand Down Expand Up @@ -813,8 +816,9 @@ function upload_one_file(auth::Authorization,
session_id = nothing
offset = Int64(0)
data_channel = Channel{Vector{UInt8}}(0)
# content_hash_task = schedule(@task calc_content_hash(data_channel))
content_hash_task = start_task(() -> calc_content_hash(data_channel))
content_hash_task = start_task(() -> calc_content_hash(data_channel),
(:upload_one_file, :calc_content_hash,
upload_spec.destination))
for chunk in upload_spec.content_channel
isempty(chunk) && continue
if session_id === nothing
Expand Down
24 changes: 12 additions & 12 deletions test/testsdk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ end
# @assert vcat(contents...) == content

data_channel = Channel{AbstractVector{UInt8}}(0)
# content_hash_task = schedule(@task calc_content_hash(data_channel))
content_hash_task = start_task(() -> calc_content_hash(data_channel))
content_hash_task =
start_task(() -> calc_content_hash(data_channel),
("Calculate content hash", :calc_content_hash))
for content in contents
put!(data_channel, content)
end
Expand Down Expand Up @@ -117,10 +118,9 @@ end

@testset "Upload file in chunks" begin
upload_channel = Channel{Vector{UInt8}}(0)
# upload_task = schedule(
# @task files_upload(auth, "/$folder/file1", upload_channel))
upload_task =
start_task(() -> files_upload(auth, "/$folder/file1", upload_channel))
start_task(() -> files_upload(auth, "/$folder/file1", upload_channel),
("Upload file in chunks", :files_upload, "/$folder/file1"))
put!(upload_channel, Vector{UInt8}("Hello, "))
put!(upload_channel, Vector{UInt8}("World!\n"))
close(upload_channel)
Expand All @@ -142,10 +142,10 @@ end

@testset "Upload empty file in chunks" begin
upload_channel = Channel{Vector{UInt8}}(0)
# upload_task = schedule(
# @task files_upload(auth, "/$folder/file2", upload_channel))
upload_task =
start_task(() -> files_upload(auth, "/$folder/file2", upload_channel))
start_task(() -> files_upload(auth, "/$folder/file2", upload_channel),
("Upload empty file in chunks", :files_upload,
"/$folder/file2"))
close(upload_channel)
metadata = fetch(upload_task)
@test metadata isa FileMetadata
Expand All @@ -166,8 +166,8 @@ end
const numfiles = 4
@testset "Upload several files" begin
upload_spec_channel = Channel{UploadSpec}(0)
# upload_task = schedule(@task files_upload(auth, upload_spec_channel))
upload_task = start_task(() -> files_upload(auth, upload_spec_channel))
upload_task = start_task(() -> files_upload(auth, upload_spec_channel),
("Upload several files", :files_upload))
for i in 0:numfiles-1
data_channel = Channel{Vector{UInt8}}(0)
put!(upload_spec_channel,
Expand Down Expand Up @@ -203,8 +203,8 @@ end

@testset "Upload zero files" begin
upload_spec_channel = Channel{UploadSpec}(0)
# upload_task = schedule(@task files_upload(auth, upload_spec_channel))
upload_task = start_task(() -> files_upload(auth, upload_spec_channel))
upload_task = start_task(() -> files_upload(auth, upload_spec_channel),
("Upload zero files", :files_upload))
close(upload_spec_channel)
metadatas = fetch(upload_task)
@test isempty(metadatas)
Expand Down

0 comments on commit 5ccd50d

Please sign in to comment.