Browse Source

support cron in docker

Nick Sweeting 5 years ago
parent
commit
235eb20dbd
4 changed files with 49 additions and 2 deletions
  1. 1 1
      Dockerfile
  2. 46 0
      archivebox/main.py
  3. 1 1
      docker-compose.yml
  4. 1 0
      setup.py

+ 1 - 1
Dockerfile

@@ -40,7 +40,7 @@ RUN groupadd --system $ARCHIVEBOX_USER \
 RUN apt-get update -qq \
 RUN apt-get update -qq \
     && apt-get install -qq -y --no-install-recommends \
     && apt-get install -qq -y --no-install-recommends \
         apt-transport-https ca-certificates gnupg2 zlib1g-dev \
         apt-transport-https ca-certificates gnupg2 zlib1g-dev \
-        dumb-init gosu unzip curl \
+        dumb-init gosu cron unzip curl \
     && rm -rf /var/lib/apt/lists/*
     && rm -rf /var/lib/apt/lists/*
 
 
 # Install apt dependencies
 # Install apt dependencies

+ 46 - 0
archivebox/main.py

@@ -967,9 +967,55 @@ def schedule(add: bool=False,
             stderr()
             stderr()
             stderr('    Make sure you have enough storage space available to hold all the data.')
             stderr('    Make sure you have enough storage space available to hold all the data.')
             stderr('    Using a compressed/deduped filesystem like ZFS is recommended if you plan on archiving a lot.')
             stderr('    Using a compressed/deduped filesystem like ZFS is recommended if you plan on archiving a lot.')
+            stderr('')
+    elif show:
+        if existing_jobs:
+            print('\n'.join(str(cmd) for cmd in existing_jobs))
+        else:
+            stderr('{red}[X] There are no ArchiveBox cron jobs scheduled for your user ({}).{reset}'.format(USER, **ANSI))
+            stderr('    To schedule a new job, run:')
+            stderr('        archivebox schedule --every=[timeperiod] https://example.com/some/rss/feed.xml')
+        raise SystemExit(0)
+
+    elif clear:
+        print(cron.remove_all(comment=CRON_COMMENT))
+        cron.write()
         raise SystemExit(0)
         raise SystemExit(0)
 
 
+    cron = CronTab(user=True)
+    cron = dedupe_cron_jobs(cron)
+    existing_jobs = list(cron.find_comment(CRON_COMMENT))
+
+    if foreground or run_all:
+        if not existing_jobs:
+            stderr('{red}[X] You must schedule some jobs first before running in foreground mode.{reset}'.format(**ANSI))
+            stderr('    archivebox schedule --every=hour https://example.com/some/rss/feed.xml')
+            raise SystemExit(1)
+
+        print('{green}[*] Running {} ArchiveBox jobs in foreground task scheduler...{reset}'.format(len(existing_jobs), **ANSI))
+        if run_all:
+            try:
+                for job in existing_jobs:
+                    sys.stdout.write(f'  > {job.command}')
+                    sys.stdout.flush()
+                    job.run()
+                    sys.stdout.write(f'\r  √ {job.command}\n')
+            except KeyboardInterrupt:
+                print('\n{green}[√] Stopped.{reset}'.format(**ANSI))
+                raise SystemExit(1)
 
 
+        if foreground:
+            try:
+                for job in existing_jobs:
+                    sys.stdout.write(f'  > {job.command}')
+                    sys.stdout.flush()
+                for result in cron.run_scheduler():
+                    print(result)
+            except KeyboardInterrupt:
+                print('\n{green}[√] Stopped.{reset}'.format(**ANSI))
+                raise SystemExit(1)
+
+    
 @enforce_types
 @enforce_types
 def server(runserver_args: Optional[List[str]]=None,
 def server(runserver_args: Optional[List[str]]=None,
            reload: bool=False,
            reload: bool=False,

+ 1 - 1
docker-compose.yml

@@ -31,7 +31,7 @@ services:
     # host machine, add tasks and see more info with archivebox schedule --help
     # host machine, add tasks and see more info with archivebox schedule --help
     # scheduler:
     # scheduler:
     #    image: nikisweeting/archivebox:latest
     #    image: nikisweeting/archivebox:latest
-    #    command: schedule --foreground
+    #    command: schedule --foreground --every=day --depth=1 'https://getpocket.com/users/USERNA<E/feed/all'
     #    environment:
     #    environment:
     #        - USE_COLOR=True
     #        - USE_COLOR=True
     #        - SHOW_PROGRESS=False
     #        - SHOW_PROGRESS=False

+ 1 - 0
setup.py

@@ -46,6 +46,7 @@ setuptools.setup(
         "ipython",
         "ipython",
         "youtube-dl",
         "youtube-dl",
         "python-crontab==2.5.1",
         "python-crontab==2.5.1",
+        "croniter==0.3.34",
         "w3lib==1.22.0",
         "w3lib==1.22.0",
         # Some/all of these will likely be added in the future:
         # Some/all of these will likely be added in the future:
         # wpull
         # wpull