Skip to content

Commit b91d6e9

Browse files
committed
[smarcet]
* added external assets sync tasks/model
1 parent 99ae287 commit b91d6e9

5 files changed

Lines changed: 133 additions & 1 deletion

File tree

assets_sync/_config.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
**/

assets_sync/_config/inyector.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
Name: 'assets-sync-injection'
3+
---
4+
Injector:
5+
AssetsSyncRequestProcessorTask:
6+
constructor:
7+
0: '%$TransactionManager'
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 AssetsSyncRequest extends DataObject
16+
{
17+
private static $db = [
18+
'From' => 'Text',
19+
'To' => 'Text',
20+
'Processed' => 'Boolean',
21+
'ProcessedDate' => 'SS_Datetime',
22+
];
23+
24+
/**
25+
* @return Object
26+
*/
27+
public function markAsProcessed(){
28+
if($this->Processed) return $this;
29+
30+
$this->Processed = true;
31+
$this->ProcessedDate = MySQLDatabase56::nowRfc2822();
32+
return $this;
33+
}
34+
}

cron_jobs_scheduler/_config/schedule.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,8 @@ jobs:
122122

123123
- name: "RebuildMemberSpammerEstimatorTask"
124124
cron_expression: "00 02 * * *" # run at 0200 AM every day
125-
enabled: 1
125+
enabled: 1
126+
127+
- name: "AssetsSyncRequestProcessorTask"
128+
cron_expression: "*/5 * * * *" # run every 5 minutes
129+
enabled: 1

0 commit comments

Comments
 (0)