Task management by channels (first try)

This commit is contained in:
Andrea Fazzi 2021-10-20 12:06:24 +02:00
parent 46ba503776
commit 814e47ef40
5 changed files with 72 additions and 43 deletions

View file

@ -11,11 +11,38 @@ import (
"github.com/kkdai/youtube/v2" "github.com/kkdai/youtube/v2"
) )
const (
StatusDownloading = iota
StatusCompleted
StatusError
)
type Task struct {
Video *youtube.Video
Status int
}
var taskCh chan *Task
func init() { func init() {
log.SetPrefix("[youtube-dl-service] ") log.SetPrefix("[youtube-dl-service] ")
taskCh = make(chan *Task)
} }
func main() { func main() {
go func() {
log.Println("Start task scheduler...")
for {
select {
case task := <-taskCh:
log.Println(task.Status)
}
}
log.Println("Stop task scheduler...")
}()
r := gin.Default() r := gin.Default()
r.Use(cors.New(cors.Config{ r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:8080", "http://localhost:5000"}, AllowOrigins: []string{"http://localhost:8080", "http://localhost:5000"},
@ -48,6 +75,26 @@ func main() {
"duration": float64(video.Duration) / float64(time.Minute), "duration": float64(video.Duration) / float64(time.Minute),
"thumbnails": video.Thumbnails[0], "thumbnails": video.Thumbnails[0],
}) })
go func() {
log.Println("Download started.")
taskCh <- &Task{
video,
StatusDownloading,
}
// simulate a long task with time.Sleep(). 5 seconds
time.Sleep(5 * time.Second)
// note that you are using the copied context "cCp", IMPORTANT
log.Printf("Video with id %s downloaded.", video.ID)
taskCh <- &Task{
video,
StatusCompleted,
}
}()
}) })
r.Run() r.Run()

Binary file not shown.

View file

@ -119,9 +119,6 @@ var app = (function () {
throw new Error('Function called outside component initialization'); throw new Error('Function called outside component initialization');
return current_component; return current_component;
} }
function onMount(fn) {
get_current_component().$$.on_mount.push(fn);
}
const dirty_components = []; const dirty_components = [];
const binding_callbacks = []; const binding_callbacks = [];
@ -918,10 +915,10 @@ var app = (function () {
const { Error: Error_1 } = globals; const { Error: Error_1 } = globals;
const file$2 = "src/Task.svelte"; const file$2 = "src/Task.svelte";
// (37:0) {:catch error} // (31:0) {:catch error}
function create_catch_block(ctx) { function create_catch_block(ctx) {
let p; let p;
let t_value = /*error*/ ctx[4].message + ""; let t_value = /*error*/ ctx[3].message + "";
let t; let t;
const block = { const block = {
@ -929,7 +926,7 @@ var app = (function () {
p = element("p"); p = element("p");
t = text(t_value); t = text(t_value);
set_style(p, "color", "red"); set_style(p, "color", "red");
add_location(p, file$2, 37, 2, 938); add_location(p, file$2, 31, 2, 847);
}, },
m: function mount(target, anchor) { m: function mount(target, anchor) {
insert_dev(target, p, anchor); insert_dev(target, p, anchor);
@ -945,14 +942,14 @@ var app = (function () {
block, block,
id: create_catch_block.name, id: create_catch_block.name,
type: "catch", type: "catch",
source: "(37:0) {:catch error}", source: "(31:0) {:catch error}",
ctx ctx
}); });
return block; return block;
} }
// (27:0) {:then video_info} // (21:0) {:then video_info}
function create_then_block(ctx) { function create_then_block(ctx) {
let a; let a;
let div3; let div3;
@ -993,25 +990,25 @@ var app = (function () {
t6 = space(); t6 = space();
small1 = element("small"); small1 = element("small");
t7 = text(/*url*/ ctx[0]); t7 = text(/*url*/ ctx[0]);
add_location(h5, file$2, 29, 49, 633); add_location(h5, file$2, 23, 49, 542);
attr_dev(div0, "class", "pb-2 flex-grow-1 bd-highlight"); attr_dev(div0, "class", "pb-2 flex-grow-1 bd-highlight");
add_location(div0, file$2, 29, 6, 590); add_location(div0, file$2, 23, 6, 499);
add_location(small0, file$2, 30, 42, 709); add_location(small0, file$2, 24, 42, 618);
attr_dev(div1, "class", "pb-2 px-2 bd-highlight"); attr_dev(div1, "class", "pb-2 px-2 bd-highlight");
add_location(div1, file$2, 30, 6, 673); add_location(div1, file$2, 24, 6, 582);
attr_dev(span, "class", "badge bg-primary"); attr_dev(span, "class", "badge bg-primary");
add_location(span, file$2, 31, 42, 786); add_location(span, file$2, 25, 42, 695);
attr_dev(div2, "class", "pb-2 px-2 bd-highlight"); attr_dev(div2, "class", "pb-2 px-2 bd-highlight");
add_location(div2, file$2, 31, 6, 750); add_location(div2, file$2, 25, 6, 659);
attr_dev(div3, "class", "d-flex bd-highlight"); attr_dev(div3, "class", "d-flex bd-highlight");
add_location(div3, file$2, 28, 4, 550); add_location(div3, file$2, 22, 4, 459);
if (!src_url_equal(img.src, img_src_value = /*video_info*/ ctx[1].thumbnails.URL)) attr_dev(img, "src", img_src_value); if (!src_url_equal(img.src, img_src_value = /*video_info*/ ctx[1].thumbnails.URL)) attr_dev(img, "src", img_src_value);
add_location(img, file$2, 33, 4, 852); add_location(img, file$2, 27, 4, 761);
add_location(small1, file$2, 34, 4, 895); add_location(small1, file$2, 28, 4, 804);
attr_dev(a, "href", "#"); attr_dev(a, "href", "#");
attr_dev(a, "class", "list-group-item list-group-item-action"); attr_dev(a, "class", "list-group-item list-group-item-action");
attr_dev(a, "aria-current", "true"); attr_dev(a, "aria-current", "true");
add_location(a, file$2, 27, 2, 466); add_location(a, file$2, 21, 2, 375);
}, },
m: function mount(target, anchor) { m: function mount(target, anchor) {
insert_dev(target, a, anchor); insert_dev(target, a, anchor);
@ -1043,14 +1040,14 @@ var app = (function () {
block, block,
id: create_then_block.name, id: create_then_block.name,
type: "then", type: "then",
source: "(27:0) {:then video_info}", source: "(21:0) {:then video_info}",
ctx ctx
}); });
return block; return block;
} }
// (25:16) <p>waiting...</p> {:then video_info} // (19:23) <p>waiting...</p> {:then video_info}
function create_pending_block(ctx) { function create_pending_block(ctx) {
let p; let p;
@ -1058,7 +1055,7 @@ var app = (function () {
c: function create() { c: function create() {
p = element("p"); p = element("p");
p.textContent = "waiting..."; p.textContent = "waiting...";
add_location(p, file$2, 25, 2, 427); add_location(p, file$2, 19, 2, 336);
}, },
m: function mount(target, anchor) { m: function mount(target, anchor) {
insert_dev(target, p, anchor); insert_dev(target, p, anchor);
@ -1073,7 +1070,7 @@ var app = (function () {
block, block,
id: create_pending_block.name, id: create_pending_block.name,
type: "pending", type: "pending",
source: "(25:16) <p>waiting...</p> {:then video_info}", source: "(19:23) <p>waiting...</p> {:then video_info}",
ctx ctx
}); });
@ -1092,10 +1089,10 @@ var app = (function () {
then: create_then_block, then: create_then_block,
catch: create_catch_block, catch: create_catch_block,
value: 1, value: 1,
error: 4 error: 3
}; };
handle_promise(/*promise*/ ctx[2], info); handle_promise(/*getVideoInfo*/ ctx[2](), info);
const block = { const block = {
c: function create() { c: function create() {
@ -1153,8 +1150,6 @@ var app = (function () {
} }
} }
onMount(getVideoInfo);
let promise = getVideoInfo();
const writable_props = ['url']; const writable_props = ['url'];
Object.keys($$props).forEach(key => { Object.keys($$props).forEach(key => {
@ -1165,25 +1160,18 @@ var app = (function () {
if ('url' in $$props) $$invalidate(0, url = $$props.url); if ('url' in $$props) $$invalidate(0, url = $$props.url);
}; };
$$self.$capture_state = () => ({ $$self.$capture_state = () => ({ url, video_info, getVideoInfo });
onMount,
url,
video_info,
getVideoInfo,
promise
});
$$self.$inject_state = $$props => { $$self.$inject_state = $$props => {
if ('url' in $$props) $$invalidate(0, url = $$props.url); if ('url' in $$props) $$invalidate(0, url = $$props.url);
if ('video_info' in $$props) $$invalidate(1, video_info = $$props.video_info); if ('video_info' in $$props) $$invalidate(1, video_info = $$props.video_info);
if ('promise' in $$props) $$invalidate(2, promise = $$props.promise);
}; };
if ($$props && "$$inject" in $$props) { if ($$props && "$$inject" in $$props) {
$$self.$inject_state($$props.$$inject); $$self.$inject_state($$props.$$inject);
} }
return [url, video_info, promise]; return [url, video_info, getVideoInfo];
} }
class Task extends SvelteComponentDev { class Task extends SvelteComponentDev {

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,4 @@
<script> <script>
import { onMount } from 'svelte';
export let url; export let url;
let video_info = {}; let video_info = {};
@ -16,13 +14,9 @@
} }
} }
onMount(getVideoInfo);
let promise = getVideoInfo();
</script> </script>
{#await promise} {#await getVideoInfo()}
<p>waiting...</p> <p>waiting...</p>
{:then video_info} {:then video_info}
<a href="#" class="list-group-item list-group-item-action" aria-current="true"> <a href="#" class="list-group-item list-group-item-action" aria-current="true">