1+ <?php
2+ /**
3+ * Copyright 2017 OpenStack Foundation
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
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ * Unless required by applicable law or agreed to in writing, software
9+ * distributed under the License is distributed on an "AS IS" BASIS,
10+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+ * See the License for the specific language governing permissions and
12+ * limitations under the License.
13+ **/
14+
15+ final class AssetsSyncRequestProcessorTask extends CronTask
16+ {
17+
18+ /**
19+ * @var ITransactionManager
20+ */
21+ private $ tx_manager ;
22+
23+ /**
24+ * AssetsSyncRequestProcessorTask constructor.
25+ * @param ITransactionManager $tx_manager
26+ */
27+ public function __construct (ITransactionManager $ tx_manager ){
28+ $ this ->tx_manager = $ tx_manager ;
29+ parent ::__construct ();
30+ }
31+ /**
32+ * @return void
33+ */
34+ public function run ()
35+ {
36+ try
37+ {
38+ $ init_time = time ();
39+ $ processed = $ this ->tx_manager ->transaction (function (){
40+ $ processed = 0 ;
41+ $ requests = AssetsSyncRequest::get ()->filter ([
42+ 'Processed ' => 0
43+ ])->sort ('ID ' , 'ASC ' );
44+
45+ foreach ($ requests as $ sync_request ){
46+ if (file_exists ($ requests ->From )){
47+ $ destination = sprintf ("%s/%s " , ASSETS_PATH , $ requests ->To );
48+ $ res = copy ($ requests ->From , $ destination );
49+ if (!$ res ){
50+ echo sprintf ("error copying file from %s to %s " , $ requests ->From , $ destination ).PHP_EOL ;
51+ continue ;
52+ }
53+ $ res = unlink ($ requests ->From );
54+ chown ($ destination , 'www-data ' );
55+ if (!$ res ){
56+ echo sprintf ("error removing file from %s " , $ requests ->From ).PHP_EOL ;
57+ }
58+ }
59+ $ sync_request ->markAsProcessed ();
60+ $ sync_request ->write ();
61+ $ processed ++;
62+ }
63+
64+ return $ processed ;
65+ });
66+ $ finish_time = time () - $ init_time ;
67+ echo 'processed records ' . $ processed . ' - time elapsed : ' .$ finish_time . ' seconds. ' ;
68+ }
69+ catch (Exception $ ex )
70+ {
71+ SS_Log::log ($ ex ->getMessage (), SS_Log::ERR );
72+ }
73+ }
74+ }
0 commit comments