process {
    // Defaults
    container = "quay.io/bactopia/bactopia:${manifest.version}"
    cpus = {check_max('request', RESOURCES.MAX_CPUS, 'cpus' )}
    memory = {check_max(2.GB * task.attempt, RESOURCES.MAX_MEMORY, 'memory' )}
    time = {check_max( (params.min_time).m * task.attempt, (params.max_time).m, 'time' )}
    errorStrategy = 'retry'
    maxRetries = params.max_retry

    publishDir = [
        path: params.outdir,
        mode: params.publish_dir_mode,
        overwrite: params.force,
        saveAs: { filename -> 
            saveFiles(filename:filename, opts:options, prefix:prefix, run_name:params.run_name, wf:params.wf, rundir:rundir) 
        }
    ]

    // Process specific
    withLabel: 'assemble_genome' {
        memory = { meta.runtype == 'hybrid' || params.use_unicycler ? check_max((32.GB * task.attempt), RESOURCES.MAX_MEMORY, 'memory') : check_max((8.GB * task.attempt), RESOURCES.MAX_MEMORY, 'memory')}
        time   = { meta.runtype == 'hybrid' || params.use_unicycler ? check_max( 2.h * task.attempt, (params.max_time).m, 'time' ) : check_max( (params.min_time).m * task.attempt, (params.max_time).m, 'time' )}
    }

    // Modules imported from nf-core
    withLabel:process_single {
        cpus   = { check_max('request'          , RESOURCES.MAX_CPUS  , 'cpus'   )}
        memory = { check_max(4.GB * task.attempt, RESOURCES.MAX_MEMORY, 'memory' )}
        time   = { check_max( 1.h * task.attempt, 2.h * task.attempt  , 'time'   )}
    }
    withLabel: process_low {
        cpus   = {check_max('request'          , RESOURCES.MAX_CPUS  , 'cpus'   )}
        memory = {check_max(8.GB * task.attempt, RESOURCES.MAX_MEMORY, 'memory' )}
        time   = {check_max( 2.h * task.attempt, 2.h * task.attempt  , 'time'   )}
    }
    withLabel: process_medium {
        cpus   = {check_max('request'           , RESOURCES.MAX_CPUS  , 'cpus'   )}
        memory = {check_max(16.GB * task.attempt, RESOURCES.MAX_MEMORY, 'memory' )}
        time   = {check_max( 12.h * task.attempt, 12.h * task.attempt , 'time'   )}
    }
    withLabel: process_high {
        cpus   = {check_max('request'           , RESOURCES.MAX_CPUS  , 'cpus'   )}
        memory = {check_max(64.GB * task.attempt, RESOURCES.MAX_MEMORY, 'memory' )}
        time   = {check_max( 24.h * task.attempt, 24.h * task.attempt , 'time'   )}
    }
    withLabel: process_long {
        time = {check_max( 96.h * task.attempt, 96.h * task.attempt, 'time' )}
    }
    withLabel: process_high_memory {
        memory = {check_max(128.GB * task.attempt, RESOURCES.MAX_MEMORY, 'memory' )}
    }
    withLabel: error_ignore {
        errorStrategy = 'ignore'
    }
    withLabel: error_retry {
        errorStrategy = 'retry'
        maxRetries    = 2
    }
}
