]> git.treefish.org Git - photosort.git/blob - src/photosort-daemon.py
fixing bunch creation
[photosort.git] / src / photosort-daemon.py
1 #!/usr/bin/env python3
2
3 import argparse
4 import json
5 import logging
6 import os
7 import signal
8 import sys
9 import time
10
11 from bunch import Bunch
12
13 def handle_interrupt(sig, frame):
14     global stop
15     if os.getpid() == main_pid:
16         logging.info( "Got stop signal." )
17         stop = True
18
19 main_pid = os.getpid()
20 signal.signal(signal.SIGINT, handle_interrupt)
21 stop = False
22
23 parser = argparse.ArgumentParser(description='Photo Sort Daemon.')
24 parser.add_argument('config_file', type=str, help='path to config file')
25 parser.add_argument('-l', '--log-level', type=str, default='INFO', dest='log_lvl',
26                     choices=['DEBUG', 'INFO', 'WARNING'], help='select log level')
27
28 args = parser.parse_args()
29
30 logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s',
31                     level=logging.getLevelName(args.log_lvl),
32                     datefmt='%m/%d/%Y %H:%M:%S')
33
34 status = 0
35
36 with open(args.config_file) as f:
37     cfg = json.load(f)
38
39 bunch_idx = 1
40 bunches = []
41 for bunch_cfg in cfg['bunches']:
42     bunches.append( Bunch(bunch_idx, cfg['cache_dir'], bunch_cfg) )
43     bunch_idx += 1
44
45 for bunch in bunches:
46     bunch.start()
47
48 while not stop:
49     for bunch in bunches:
50         if not bunch.is_running():
51             stop = True
52             status = 1
53     time.sleep(2.0)
54
55 for bunch in bunches:
56     bunch.stop()
57
58 sys.exit(status)