Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
WipeRepo |
|
| 6.0;6 |
1 | 2 | /** |
2 | * Copyright (c) 2014-2015, Thindeck.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the thindeck.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | package com.thindeck.agents; | |
31 | ||
32 | import com.jcabi.aspects.Immutable; | |
33 | import com.jcabi.aspects.Tv; | |
34 | import com.jcabi.log.Logger; | |
35 | import com.jcabi.xml.XML; | |
36 | import com.thindeck.api.Agent; | |
37 | import java.io.IOException; | |
38 | import java.text.ParseException; | |
39 | import java.util.Collection; | |
40 | import java.util.Date; | |
41 | import java.util.concurrent.TimeUnit; | |
42 | import org.apache.commons.lang3.time.DateFormatUtils; | |
43 | import org.xembly.Directive; | |
44 | import org.xembly.Directives; | |
45 | ||
46 | /** | |
47 | * Remove repo if it's too old and still has no images. | |
48 | * | |
49 | * @author Yegor Bugayenko (yegor@teamed.io) | |
50 | * @version $Id$ | |
51 | * @since 0.1 | |
52 | */ | |
53 | @Immutable | |
54 | 1 | public final class WipeRepo implements Agent { |
55 | ||
56 | @Override | |
57 | public Iterable<Directive> exec(final XML deck) throws IOException { | |
58 | 0 | final Collection<XML> images = deck.nodes( |
59 | "/deck/images/image[repo=/deck/repo/name]" | |
60 | ); | |
61 | 0 | final Directives dirs = new Directives(); |
62 | 0 | if (images.isEmpty() && !deck.nodes("/deck/repo/name").isEmpty()) { |
63 | 0 | final Date today = new Date(); |
64 | final int age; | |
65 | try { | |
66 | 0 | age = (int) ((today.getTime() |
67 | - DateFormatUtils.ISO_DATETIME_FORMAT.parse( | |
68 | deck.xpath("/deck/repo/@added").get(0) | |
69 | ).getTime()) / TimeUnit.MINUTES.toMillis(1L)); | |
70 | 0 | } catch (final ParseException ex) { |
71 | 0 | throw new IOException(ex); |
72 | 0 | } |
73 | 0 | if (age > Tv.TEN) { |
74 | 0 | Logger.info( |
75 | this, "Repo %s still has no images for over %d mins", | |
76 | deck.xpath("/deck/repo/uri/text()"), age | |
77 | ); | |
78 | 0 | dirs.xpath("/deck/repo").remove(); |
79 | } | |
80 | } | |
81 | 0 | return dirs; |
82 | } | |
83 | ||
84 | } |