16359c9da76d30902202ce19101f9b05c14a3f16
2 # Copyright 2016 RIFT.IO Inc
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # Author(s): Varun Prasad
17 # Creation Date: 09/25/2016
23 from gi
.repository
import (RwDts
as rwdts
)
24 import rift
.mano
.dts
as mano_dts
26 import rift
.downloader
as url_downloader
31 if sys
.version_info
< (3, 4, 4):
32 asyncio
.ensure_future
= asyncio
.async
34 class DownloadStatusPublisher(mano_dts
.DtsHandler
, url_downloader
.DownloaderProtocol
):
36 def __init__(self
, log
, dts
, loop
, project
):
37 super().__init
__(log
, dts
, loop
, project
)
41 def xpath(self
, download_id
=None):
42 return self
._project
.add_project("D,/rw-pkg-mgmt:download-jobs/rw-pkg-mgmt:job" +
43 ("[download-id='{}']".
44 format(download_id
) if download_id
else ""))
47 def _dts_publisher(self
, job
):
48 # Publish the download state
49 self
.reg
.update_element(
50 self
.xpath(download_id
=job
.download_id
), job
)
54 self
.reg
= yield from self
.dts
.register(xpath
=self
.xpath(),
55 flags
=rwdts
.Flag
.PUBLISHER|rwdts
.Flag
.CACHE|rwdts
.Flag
.NO_PREP_READ
)
57 assert self
.reg
is not None
60 self
._log
.debug("De-registering download status for project {}".
61 format(self
.project
.name
))
67 def _async_func(func
, fut
):
71 except Exception as e
:
74 def _schedule_dts_work(self
, download_job_msg
):
76 cort
= self
._dts
_publisher
(download_job_msg
)
77 # Use main asyncio loop (running in main thread)
78 newfunc
= functools
.partial(asyncio
.ensure_future
, cort
, loop
=self
.loop
)
79 fut
= concurrent
.futures
.Future()
80 # Schedule future in main thread immediately
81 self
.loop
.call_soon_threadsafe(DownloadStatusPublisher
._async
_func
, newfunc
, fut
)
85 self
.log
.error("Caught future exception during download: %s type %s", str(exc
), type(exc
))
89 def on_download_progress(self
, download_job_msg
):
90 """callback that triggers update.
92 # Trigger progess update
93 # Schedule a future in the main thread
94 self
._schedule
_dts
_work
(download_job_msg
)
96 def on_download_finished(self
, download_job_msg
):
97 """callback that triggers update.
100 # clean up the local cache
101 key
= download_job_msg
.download_id
102 if key
in self
.tasks
:
105 # Publish the final state
106 # Schedule a future in the main thread
107 self
._schedule
_dts
_work
(download_job_msg
)
110 def register_downloader(self
, downloader
):
111 downloader
.delegate
= self
112 future
= self
.loop
.run_in_executor(None, downloader
.download
)
113 self
.tasks
[downloader
.download_id
] = (downloader
, future
)
115 return downloader
.download_id
118 def cancel_download(self
, key
):
119 task
, future
= self
.tasks
[key
]
122 task
.cancel_download()
127 for task
, future
in self
.tasks
:
131 def deregister(self
):
132 """ de-register with dts """
133 if self
.reg
is not None:
134 self
.reg
.deregister()