process {
    // Defaults
    cpus = {check_max(1, MAX_CPUS, 'cpus' )}
    memory = {check_max(1.GB * task.attempt, MAX_MEMORY, 'memory' )}
    time = {check_max( (params.min_time).m * task.attempt, (params.max_time).m, 'time' )}
    errorStrategy = 'retry'
    maxRetries = params.max_retry

    // Full cpus
    withName: 'annotate_genome|antimicrobial_resistance|blast_genes|blast_primers|blast_proteins|mapping_query' {
        cpus = {check_max('request', MAX_CPUS, 'cpus')}
    }

    // Half cpus
    withName: 'qc_original_summary|qc_final_summary' {
        cpus = {check_max('request', MAX_CPUS_50, 'cpus' )}
    }

    withName: plasmid_blast {
        cpus = {check_max('request', MAX_CPUS, 'cpus' )}
        memory = 3.GB
    }

    withName: ariba_analysis {
        cpus = {task.attempt > 1 ? 1 : check_max('request', MAX_CPUS_75, 'cpus')}
        maxRetries = 1
        memory = {check_max(2.GB * task.attempt, MAX_MEMORY, 'memory' )}
        errorStrategy = { task.attempt > 1 && task.exitStatus == 1 ? 'ignore' : 'retry' }
    }

    withName: qc_reads {
        cpus = {check_max('request', MAX_CPUS, 'cpus' )}
        memory = {check_max(((params.qc_ram).GB * task.attempt), MAX_MEMORY, 'memory')}
    }

    withName: 'assemble_genome|assembly_qc' {
        cpus = {check_max('request', MAX_CPUS_75, 'cpus' )}
        memory = { sample_type == 'hybrid' ? check_max(((params.unicycler_ram).GB * task.attempt), MAX_MEMORY, 'memory') : check_max(((params.shovill_ram).GB * task.attempt), MAX_MEMORY, 'memory')}
        time = { sample_type == 'hybrid' ? check_max( (Math.max(params.min_time, 120)).m * task.attempt, (params.max_time).m, 'time' ) : check_max( (params.min_time).m * task.attempt, (params.max_time).m, 'time' )}
    }

    withName: 'call_variants|call_variants_auto' {
        cpus = {check_max('request', MAX_CPUS_75, 'cpus' )}
        memory = {check_max(((params.snippy_ram).GB * task.attempt), MAX_MEMORY, 'memory')}
    }

    withName: count_31mers {
        cpus = {check_max('request', MAX_CPUS, 'cpus' )}
        memory = {check_max(((params.cortex_ram).GB * task.attempt), MAX_MEMORY, 'memory')}
    }

    withName: minmer_query {
        cpus = {check_max( 'request', MAX_CPUS, 'cpus' )}
        memory = {check_max(((params.minmer_ram).GB * task.attempt), MAX_MEMORY, 'memory')}
    }

    withName: download_references {
        maxRetries = Math.max(params.max_retry, 6)
        errorStrategy = { task.attempt > 5 && task.exitStatus == 75 ? 'ignore' : 'retry' }
    }

    withName: 'gather_fastqs' {
        cpus = {check_max(4, MAX_CPUS, 'cpus')}
        maxForks = params.max_downloads
        maxRetries = 20
    }
}
