Beispiel #1
0
def capture_task(header: str, data: dict) -> dict:
    try:
        # vcam = camera.VideoCamera()
        if not vcam:
            raise TaskError('Video Camera not initialized')
        else:
            frame = vcam.get_frame()
        ctime = datetime.datetime.now(pytz.timezone("Asia/Seoul"))
        fname = f'{header}_{ctime.strftime("%Y-%m-%dT%H-%M-%S-%f")}.jpg'
        path = f'/data/{fname}'
        res = cv2.imwrite(path, frame)
        body = {
            'target': data.get('target'),
            'path': fname,
            'device': data.get('device'),
            'created': ctime.isoformat(),
            # TODO 요청한 유저로 수정
            'created_by': None,
            'label': data.get('label'),
            # TODO 현재 오프셋 받아오게 수정
            'offset_x': 0,
            'offset_y': 0,
            'offset_z': 0,
            'pos_x': 0,
            'pos_y': 0,
            'pos_z': 0
        }
        if not res:
            raise TaskError('Nothing written by cv2')
        else:
            return body
    except TaskError as e:
        raise e
Beispiel #2
0
def update_categories():
    success = True
    lock_id = 'categories.update_categories'

    old_stdout = sys.stdout
    sys.stdout = mystdout = io.StringIO()

    try:
        with db_mutex(lock_id):
            # Commands don't return anything
            call_command('parse_categories')

    except DBMutexError:
        success = False
        print('update_categories: Could not obtain lock')

    except DBMutexTimeoutError:
        print('update_categories: Task completed but the lock timed out')

    except Exception as error:
        print(error)

        DBMutex.objects.filter(lock_id=lock_id).delete()
        success = False

    sys.stdout = old_stdout

    if not success:
        raise TaskError(mystdout.getvalue())

    return {
        "task": "update_categories",
        "params": {},
        "message": mystdout.getvalue()
    }
Beispiel #3
0
def stop_timelapse_task(key: str) -> bool:
    try:
        entry = RedBeatSchedulerEntry.from_key(key, app=app)
        entry.delete()
        return True
    except Exception as e:
        logger.error(traceback.format_exc())
        raise TaskError(e)
Beispiel #4
0
def cv_threshold(path, **kwargs) -> np.ndarray:
    try:
        src = np.array(Image.open('/data/' + path))
        output_path = '/data/cv/' + path
        if not os.path.exists('/data/cv'):
            os.mkdir('/data/cv')
        Image.fromarray(threshold.apply(src, **kwargs)).save(output_path)
        return output_path
    except Exception as e:
        logger.error(traceback.format_exc())
        raise TaskError(e)
Beispiel #5
0
def start_timelapse_task(header: str, run_every: float, expire_at: str,
                         data: dict) -> str:
    try:
        interval = celery.schedules.schedule(run_every=run_every)  # seconds
        entry = RedBeatSchedulerEntry('timelapse',
                                      'cam_task.capture',
                                      interval,
                                      args=[header, data],
                                      app=app)
        entry.save()
        return entry.key
    except Exception as e:
        logger.error(traceback.format_exc())
        raise TaskError(e)
Beispiel #6
0
def update_contracts(period=260, load=260, count=500, pause=1):
    success = True
    lock_id = 'contracts.update_contracts'

    old_stdout = sys.stdout
    sys.stdout = mystdout = io.StringIO()

    try:
        with db_mutex(lock_id):
            # Commands don't return anything
            call_command('load_fpds',
                         period=period,
                         load=load,
                         count=count,
                         pause=pause)

    except DBMutexError:
        success = False
        print('update_contracts: Could not obtain lock')

    except DBMutexTimeoutError:
        print('update_contracts: Task completed but the lock timed out')

    except Exception as error:
        print(error)

        DBMutex.objects.filter(lock_id=lock_id).delete()
        success = False

    sys.stdout = old_stdout

    if not success:
        raise TaskError(mystdout.getvalue())

    return {
        "task": "update_fpds",
        "params": {
            "period": period,
            "load": load,
            "count": count,
            "pause": pause
        },
        "message": mystdout.getvalue()
    }
Beispiel #7
0
def update_vendors_sam(tries=3, pause=1):
    success = True
    lock_id = 'vendors.update_vendors_sam'

    old_stdout = sys.stdout
    sys.stdout = mystdout = io.StringIO()

    try:
        with db_mutex(lock_id):
            # Commands don't return anything
            call_command('load_sam', tries=tries, pause=pause)

    except DBMutexError:
        success = False
        print('update_vendors_sam: Could not obtain lock')

    except DBMutexTimeoutError:
        print('update_vendors_sam: Task completed but the lock timed out')

    except Exception as error:
        print(error)

        DBMutex.objects.filter(lock_id=lock_id).delete()
        success = False

    sys.stdout = old_stdout

    if not success:
        raise TaskError(mystdout.getvalue())

    return {
        "task": "update_vendors_sam",
        "params": {
            "tries": tries,
            "pause": pause
        },
        "message": mystdout.getvalue()
    }
Beispiel #8
0
def prune_contracts(period=260):
    success = True
    lock_id = 'contracts.prune_contracts'

    old_stdout = sys.stdout
    sys.stdout = mystdout = io.StringIO()

    try:
        with db_mutex(lock_id):
            # Commands don't return anything
            call_command('prune_contracts', period=period)

    except DBMutexError:
        success = False
        print('prune_contracts: Could not obtain lock')

    except DBMutexTimeoutError:
        print('prune_contracts: Task completed but the lock timed out')

    except Exception as error:
        print(error)

        DBMutex.objects.filter(lock_id=lock_id).delete()
        success = False

    sys.stdout = old_stdout

    if not success:
        raise TaskError(mystdout.getvalue())

    return {
        "task": "prune_contracts",
        "params": {
            "period": period
        },
        "message": mystdout.getvalue()
    }
Beispiel #9
0
def mark_task_as_failed(task, reason=None):
    reason = reason or "Task failed"
    task.update_state(state=states.FAILURE, meta=reason)

    # ignore the task so no other state is recorded
    raise TaskError(reason)