UNPKG

@mondaycom/apps-cli

Version:

A cli tool to manage apps (and monday-code projects) in monday.com

97 lines (96 loc) 5.04 kB
import { AuthenticatedCommand } from '../../commands-base/authenticated-command.js'; import { SchedulerFlags } from '../../consts/scheduler/flags.js'; import { SchedulerMessages } from '../../consts/scheduler/messages.js'; import { DynamicChoicesService } from '../../services/dynamic-choices-service.js'; import { PromptService } from '../../services/prompt-service.js'; import { SchedulerService } from '../../services/scheduler-service.js'; import { printJobs, validateCronExpression, validateTargetUrl } from '../../services/scheduler-service.utils.js'; import logger from '../../utils/logger.js'; import { chooseRegionIfNeeded, getRegionFromString } from '../../utils/region.js'; import { addPrefixIfNotExists } from '../../utils/urls-builder.js'; import { isDefined, isDefinedAndNotEmpty } from '../../utils/validations.js'; export default class SchedulerUpdate extends AuthenticatedCommand { static description = 'Update a scheduler job for an app'; static examples = [ '<%= config.bin %> <%= command.id %> -a APP_ID -n "my-job" -s "0 * * * *"', '<%= config.bin %> <%= command.id %> -a APP_ID -n "my-job" -u "my-endpoint"', '<%= config.bin %> <%= command.id %> -a APP_ID -n "my-job" -d "My description" -r 3 -b 10 -t 60', ]; static flags = SchedulerUpdate.serializeFlags(SchedulerFlags); DEBUG_TAG = 'scheduler_update'; async run() { const { flags } = await this.parse(SchedulerUpdate); let { appId, name, description, schedule, targetUrl, maxRetries, minBackoffDuration, timeout } = flags; const { region } = flags; const parsedRegion = getRegionFromString(region); try { if (!appId) appId = await DynamicChoicesService.chooseApp(); const selectedRegion = await chooseRegionIfNeeded(parsedRegion, { appId }); if (!name) name = await DynamicChoicesService.chooseSchedulerJob(appId, selectedRegion); // Get the current job details const jobs = await SchedulerService.listJobs(appId, selectedRegion); const currentJob = jobs.find(job => job.name === name); if (!currentJob) { throw new Error(`Job ${name} not found`); } logger.info(`All parameters are optional, press enter to skip`); // Only prompt for fields that weren't provided in flags if (!description) description = await PromptService.promptInput(SchedulerMessages.description, false, true); if (!schedule) schedule = await PromptService.promptInput(SchedulerMessages.schedule, false, true); if (schedule) validateCronExpression(schedule); if (!targetUrl) targetUrl = await PromptService.promptInput(SchedulerMessages.targetUrl, false, true); targetUrl = addPrefixIfNotExists(targetUrl, '/'); if (targetUrl) validateTargetUrl(targetUrl); if (!maxRetries) maxRetries = await PromptService.promptInputNumber(SchedulerMessages.maxRetries, false, true); if (!minBackoffDuration) minBackoffDuration = await PromptService.promptInputNumber(SchedulerMessages.minBackoffDuration, false, true); if (!timeout) timeout = await PromptService.promptInputNumber(SchedulerMessages.timeout, false, true); logger.debug(`Updating scheduler job ${name} for appId: ${appId}`, this.DEBUG_TAG); this.preparePrintCommand(this, { appId, name, description, schedule, targetUrl, maxRetries, minBackoffDuration, timeout, region: selectedRegion, }); // Create update payload with only the fields that were provided const updatePayload = {}; if (isDefinedAndNotEmpty(description)) updatePayload.description = description; if (isDefinedAndNotEmpty(schedule)) updatePayload.schedule = schedule; if (isDefinedAndNotEmpty(targetUrl)) updatePayload.targetUrl = targetUrl; if (isDefined(maxRetries) || isDefined(minBackoffDuration)) { updatePayload.retryConfig = { maxRetries, minBackoffDuration }; } if (isDefined(timeout)) updatePayload.timeout = timeout; if (isDefinedAndNotEmpty(updatePayload)) { const job = await SchedulerService.updateJob(appId, name, updatePayload, selectedRegion); printJobs([job]); logger.info(`Successfully updated job: ${name}`); } else { logger.info(`No changes to update for job: ${name}`); } } catch (error) { logger.debug(error, this.DEBUG_TAG); process.exit(1); } } }