]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
location(8): Allow limiting updates to once a day, week, month
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 Jul 2020 10:41:23 +0000 (10:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 10 Jul 2020 10:41:23 +0000 (10:41 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
man/location.txt
src/python/location.in

index 163cd59489751af2a31f11e85037a0bee4ae166d..df1da53405ad787e26e3fb2da9f47f8aa01d2cad 100644 (file)
@@ -13,7 +13,7 @@ location - Query the location database
 `location list-networks-by-flags [--anonymous-proxy|--satellite-provider|--anycast]`
 `location lookup ADDRESS [ADDRESS...]`
 `location search-as STRING`
-`location update`
+`location update [--cron=daily|weekly|monthly]`
 `location verify`
 
 == DESCRIPTION
@@ -93,6 +93,10 @@ or countries.
        It will terminate with a return code of zero if the database has been
        successfully updated. 1 on error, 2 on invalid call and 3 if the
        database was already the latest version.
+       +
+       The '--cron' option allows limiting updates to once a day ('daily'), once a week
+       ('weekly'), or once a month ('monthly'). If the task is being called, but the
+       database has been updated recently, an update will be skipped.
 
 'verify'::
        Verifies the downloaded database.
index a8048047c46d5a427230c0d365dab719b90fd071..c4987a5ccae6672606b60ad2602f770520bf42f6 100644 (file)
@@ -88,6 +88,10 @@ class CLI(object):
 
                # Update
                update = subparsers.add_parser("update", help=_("Update database"))
+               update.add_argument("--cron",
+                       help=_("Update the library only once per interval"),
+                       choices=("daily", "weekly", "monthly"),
+               )
                update.set_defaults(func=self.handle_update)
 
                # Verify
@@ -388,6 +392,27 @@ class CLI(object):
                                print(a)
 
        def handle_update(self, db, ns):
+               if ns.cron and db:
+                       now = datetime.datetime.utcnow()
+
+                       # Parse the database timestamp
+                       t = datetime.datetime.fromtimestamp(db.created_at)
+
+                       if ns.cron == "daily":
+                               delta = datetime.timedelta(days=1)
+                       elif ns.cron == "weekly":
+                               delta = datetime.timedelta(days=7)
+                       elif ns.cron == "monthly":
+                               delta = datetime.timedelta(days=30)
+
+                       # Check if the database has recently been updated
+                       if t >= (now - delta):
+                               log.info(
+                                       _("The datase has recently be updated recently (%s)") % \
+                                               format_timedelta(now - t),
+                               )
+                               return
+
                # Fetch the timestamp we need from DNS
                t = location.discover_latest_version()
 
@@ -549,6 +574,37 @@ class CLI(object):
                e.export(ns.directory, countries=countries, asns=asns, families=families)
 
 
+def format_timedelta(t):
+       s = []
+
+       if t.days:
+               s.append(
+                       _("One Day", "%(days)s Days", t.days) % { "days" : t.days, }
+               )
+
+       hours = t.seconds // 3600
+       if hours:
+               s.append(
+                       _("One Hour", "%(hours)s Hours", hours) % { "hours" : hours, }
+               )
+
+       minutes = (t.seconds % 3600) // 60
+       if minutes:
+               s.append(
+                       _("One Minute", "%(minutes)s Minutes", minutes) % { "minutes" : minutes, }
+               )
+
+       seconds = t.seconds % 60
+       if t.seconds:
+               s.append(
+                       _("One Second", "%(seconds)s Seconds", seconds) % { "seconds" : seconds, }
+               )
+
+       if not s:
+               return _("Now")
+
+       return _("%s ago") % ", ".join(s)
+
 def main():
        # Run the command line interface
        c = CLI()