Can you "if" on resource_class?


I was wondering if there is a way in the YAML to “if” the resource_class of an executor? I ask because I’d like to always do, say:

make -j$(nproc) install

but it turns out if I do that on a medium resource class, it falls apart (due to memory limits, I think), so I have to do:

make -j4 install

but on a large or higher, it seems okay. So I was wondering if there is anyway to do an if-test on the resource class so that if we had to change/reduce from large to medium the number of jobs my make does?

Hello, Matt!
First of all, here is a related feature request related to using the nproc: Have `nproc` accurately reporter number of CPUs available to container | Cloud Feature Requests | CircleCI Ideas. But I don’t see any new activities there from 2018.
The simplest solution is to define the resource_class value through the job parameters and add conditional steps for each resource class you plan to use:

      # any executor you use
        type: string
        default: 'medium'
    resource_class: << parameters.resource_class >>
      - when:
            equal: [medium, << parameters.resource_class >>]
            - run: make -j2 install
      - when:
            equal: [large, << parameters.resource_class >>]
            - run: make -j4 install

The number of available CPUs we can retrieve from the resource classes info page: CircleCI Resource Classes - CircleCI.
Defining conditional steps: Reusable Config Reference Guide - CircleCI

@batovpavlo Ooh. Thanks for the link and I’ve up-voted the feature request.

And your solution is good. I think I’ve “gotten around” some of this in my orb by having a parameter I can pass in for the number of jobs…but your way is probably better. I’ll look at adding this!

Matt, I recently asked CircleCI support about how to retrieve the number of vCPUs available for the current executor programmatically. The answer is that the CircleCI does not have a programmatic way of showing the number of CPUs used by an executor. The number of CPUs an executor has depends on your resource_class size, and the executor type (i.e Docker, MacOS, Machine executor, etc.).
Also, if you use the docker executor you can try to retrieve such info using docker inspect with the nanocpus flag, or docker stats to see available resources.
But I guess the simplest solution is that I presented above, or simply adjust the threads number for the make command manually along with changing the resource class.
If you need advanced and complex logic for your config you may also use the dynamic configuration feature: Using dynamic configuration - CircleCI. For example, with the programming language of your choice and some template engine.
Hope some of this info will help you!