Ejemplo n.º 1
0
def set_process_state_change_timestamp(process):
    """
    Set the global setting that reflects the last time a process changed state, for the process type
    of the given process, to the current timestamp. The process type will be determined based on
    the class of the calculation node it has as its database container.

    :param process: the Process instance that changed its state
    """
    from aiida.backends.utils import get_settings_manager
    from aiida.common import timezone
    from aiida.common.exceptions import UniquenessError
    from aiida.orm import ProcessNode, CalculationNode, WorkflowNode

    if isinstance(process.node, CalculationNode):
        process_type = 'calculation'
    elif isinstance(process.node, WorkflowNode):
        process_type = 'work'
    elif isinstance(process.node, ProcessNode):
        # This will only occur for testing, as in general users cannot launch plain Process classes
        return
    else:
        raise ValueError('unsupported calculation node type {}'.format(type(process.node)))

    key = PROCESS_STATE_CHANGE_KEY.format(process_type)
    description = PROCESS_STATE_CHANGE_DESCRIPTION.format(process_type)
    value = timezone.datetime_to_isoformat(timezone.now())

    try:
        manager = get_settings_manager()
        manager.set(key, value, description)
    except UniquenessError as exception:
        process.logger.debug('could not update the {} setting because of a UniquenessError: {}'.format(key, exception))
    def setUpBeforeMigration(self):
        from aiida.common import timezone

        db_setting_model = self.apps.get_model('db', 'DbSetting')

        self.settings_info['2daemon|task_stop|updater2'] = dict(
            key='2daemon|task_stop|updater2',
            datatype='date',
            dval=timezone.datetime_to_isoformat(timezone.now()),
            description='The last time the daemon finished to run '
            'the task \'updater\' (updater)'
        )
        self.settings_info['2daemon|task_start|updater2'] = dict(
            key='2daemon|task_start|updater2',
            datatype='date',
            dval=timezone.datetime_to_isoformat(timezone.now()),
            description='The last time the daemon started to run '
            'the task \'updater\' (updater)'
        )
        self.settings_info['2db|backend2'] = dict(
            key='2db|backend2',
            datatype='txt',
            tval='django',
            description='The backend used to communicate with the database.'
        )
        self.settings_info['2daemon|user2'] = dict(
            key='2daemon|user2',
            datatype='txt',
            tval='*****@*****.**',
            description='The only user that is allowed to run the AiiDA daemon on '
            'this DB instance'
        )
        self.settings_info['2db|schemaversion2'] = dict(
            key='2db|schemaversion2',
            datatype='txt',
            tval=' 1.0.8',
            description='The version of the schema used in this database.'
        )

        with transaction.atomic():
            for setting_info in self.settings_info.values():
                setting = db_setting_model(**setting_info)
                setting.save()
Ejemplo n.º 3
0
    def set_scheduler_state(self, state: 'JobState') -> None:
        """Set the scheduler state.

        :param state: an instance of `JobState`
        """
        from aiida.common import timezone
        from aiida.schedulers.datastructures import JobState

        if not isinstance(state, JobState):
            raise ValueError(f'scheduler state should be an instance of JobState, got: {state}')

        self.set_attribute(self.SCHEDULER_STATE_KEY, state.value)
        self.set_attribute(self.SCHEDULER_LAST_CHECK_TIME_KEY, timezone.datetime_to_isoformat(timezone.now()))
def transition_settings(apps, _):
    """ Migrate the DbSetting EAV val into the JSONB val column of the same table. """
    db_setting_model = apps.get_model('db', 'DbSetting')

    with transaction.atomic():
        total_settings_no = db_setting_model.objects.count()

        if total_settings_no == 0:
            return

        with click.progressbar(label='Updating settings',
                               length=total_settings_no,
                               show_pos=True) as pr_bar:
            fetcher = lazy_bulk_fetch(
                group_size, total_settings_no,
                db_setting_model.objects.order_by('id').all)
            error = False

            for batch in fetcher:
                for curr_dbsetting in batch:

                    # Migrating dbsetting.val
                    dt = curr_dbsetting.datatype
                    val = None
                    if dt == 'txt':
                        val = curr_dbsetting.tval
                    elif dt == 'float':
                        val = curr_dbsetting.fval
                        if math.isnan(val) or math.isinf(val):
                            val = str(val)
                    elif dt == 'int':
                        val = curr_dbsetting.ival
                    elif dt == 'bool':
                        val = curr_dbsetting.bval
                    elif dt == 'date':
                        val = datetime_to_isoformat(curr_dbsetting.dval)

                    curr_dbsetting.val = val

                    # Saving the result
                    curr_dbsetting.save()
                    pr_bar.update(1)

                    if error:
                        raise Exception(
                            'There has been some errors during the migration')
def attributes_to_dict(attr_list):
    """
    Transform the attributes of a node into a dictionary. It assumes the key
    are ordered alphabetically, and that they all belong to the same node.
    """
    d = {}

    error = False
    for a in attr_list:
        try:
            tmp_d = select_from_key(a.key, d)
        except ValueError:
            echo.echo_error(
                f"Couldn't transfer attribute {a.id} with key {a.key} for dbnode {a.dbnode_id}"
            )
            error = True
            continue
        key = a.key.split('.')[-1]

        if isinstance(tmp_d, (list, tuple)):
            key = int(key)

        dt = a.datatype

        if dt == 'dict':
            tmp_d[key] = {}
        elif dt == 'list':
            tmp_d[key] = [None] * a.ival
        else:
            val = None
            if dt == 'txt':
                val = a.tval
            elif dt == 'float':
                val = a.fval
                if math.isnan(val) or math.isinf(val):
                    val = str(val)
            elif dt == 'int':
                val = a.ival
            elif dt == 'bool':
                val = a.bval
            elif dt == 'date':
                val = datetime_to_isoformat(a.dval)

            tmp_d[key] = val

    return d, error