def __call__(self, task: Task):
        task.complete(at=self._now)
        self._session.commit()

        # TODO: Do some more complex stuff here...

        return task
Ejemplo n.º 2
0
def csv_upload(self, path):
    file_path = path
    # Creating a new Task and assigning state to PROCESSING
    task_id = self.request.id
    task = Task(id=task_id, operation='Upload', state='PROCESSING')
    db.session.add(task)
    db.session.commit()
    with open(file_path, 'r') as csvfile:
        csvreader = csv.reader(csvfile, delimiter=',')
        fields = next(csvreader)
        # Deleting all previous entries
        # db.session.query(User).delete()
        # db.session.commit()
        for row in csvreader:
            # Keeping track of AsyncResult state
            if (AsyncResult(task_id, app=celery).state == 'PENDING'):
                date = datetime.strptime(row[5], "%Y-%m-%d").date()
                # Adding user to User model
                user = User(name=row[0],
                            age=row[1],
                            phone=row[2],
                            email=row[3],
                            address=row[4],
                            record_date=date,
                            task_id=task_id)
                db.session.add(user)
                db.session.commit()
            else:
                # Revoke Action on Upload Operation
                if (AsyncResult(task_id, app=celery).state == 'REVOKED'):
                    # Deleting all user entries on Revoke Action
                    entries = User.query.filter_by(task_id=task_id)
                    for entry in entries:
                        db.session.delete(entry)
                    # Keeping track of Revoked Tasks
                    rev = RevokedTask(task_id=task_id)
                    db.session.add(rev)
                    db.session.commit()
                    # Updating status of Task to REVOKED
                    task = Task.query.filter_by(id=task_id).first()
                    task.state = 'REVOKED'
                    db.session.add(task)
                    db.session.commit()
                    # Performing Revoke Operation
                    revoke(task_id, terminate=True, signal='SIGKILL')
                    raise Ignore
                # Pause Action on Upload Operation
                task = Task.query.filter_by(id=task_id).first()
                task.state = 'PAUSED'
                db.session.commit()
                return 'Uploading Operation Paused!!'
        # Success Action on Upload Operation
        task = Task.query.filter_by(id=task_id).first()
        task.state = 'SUCCESS'
        task.complete = True
        db.session.commit()
        return 'Uploaded Data!!'