賞金將在 7 天后到期。此問題的答案有資格獲得 200聲望賞金。 eat-sleep-code正在從有信譽的來源尋找答案:
我正在尋找一個解決代碼未在生產中執行的問題的答案。
我正在開發一個節點 Azure 函式(在 Linux 上)以動態創建 GIF 影像。這一切都在我本地的 MacBook 上運行得很好,但是當我部署到 Azure 時失敗了。
在日志中達到峰值時,我只是看到它Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException在部署到 Azure 時會拋出日志。
下面是我正在進行的代碼。我已經包含了完整的功能代碼,因為沒有明顯的(對我而言)錯誤來源的指示。有沒有人有任何想法可能導致這個問題?
import { AzureFunction, Context, HttpRequest } from "@azure/functions"
import { DateTime, Duration } from "luxon";
import { Canvas } from "canvas";
import * as fs from 'fs';
const os = require('os');
const { GifEncoder } = require("@skyra/gifenc")
const { buffer } = require('node:stream/consumers');
const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
try {
let countdownTo: string = req.query.countdownTo || DateTime.now().plus({days: 7}).toFormat("yyyy-LL-dd'T'T");
let width: number = parseInt(req.query.width) || 300;
let height: number = parseInt(req.query.height);
let frames: number = parseInt(req.query.frames) || 180;
let background: string = '#' (req.query.background || '000e4e');
let foreground: string = '#' (req.query.foreground || '00b7f1');
// Set height to 1/3 width if not specified
if (!height) {
height = width/3;
}
// ========================================================================
// Set upper and lower limits
width = Math.max(width, Math.min(120, 640));
height = Math.max(height, Math.min(120, 640));
frames = Math.max(frames, Math.min(1, 600));
// Time calculations
const nowDate: DateTime = DateTime.now()
const targetDate: DateTime = DateTime.fromISO(countdownTo); // "2022-11-01T12:00"
let timeDifference: Duration = targetDate.diff(nowDate);
// ========================================================================
const encoder: any = new GifEncoder(width, height);
const canvas: Canvas = new Canvas(width, height);
const ctx: any = canvas.getContext('2d');
let fileName = targetDate.toFormat('yyyyLLddHHmmss') '-' nowDate.toFormat('yyyyLLddHHmmss') '.gif';
// Specify temporary directory and create if it doesn't exist
const temporaryFileDirectory: string = os.tmpdir() '/tmp/';
if (!fs.existsSync(temporaryFileDirectory)) {
fs.mkdirSync(temporaryFileDirectory);
}
// Stream the GIF data into a file
let filePath: string = temporaryFileDirectory fileName '.gif';
const readStream = encoder.createReadStream()
readStream.pipe(fs.createWriteStream(filePath));
// Set the font size, style, and alignment
let fontSize: string = Math.floor(width/12) 'px';
let fontFamily: string = 'Arial';
ctx.font = [fontSize, fontFamily].join(' ');
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
// Start Encoding GIF
encoder.setRepeat(0).setDelay(1000).setQuality(10).start();
if (timeDifference.milliseconds <= 0) { // Date has passed
context.log('EXPIRED Date:', countdownTo);
// Set Canvas background color
ctx.fillStyle = background;
ctx.fillRect(0, 0, width, height);
// Print Text
ctx.fillStyle = foreground;
ctx.fillText('Date has passed!', (width/2), (height/2));
// Add frame to the GIF
encoder.addFrame(ctx);
}
else { // Date is in the future
timeDifference = targetDate.diff(DateTime.now(), ['days', 'hours', 'minutes', 'seconds']);
context.log('Date:', countdownTo);
for (let i: number = 0; i < frames; i ) {
// Extract the duration from the days
let days: number = Math.floor(timeDifference.days);
let hours: number = Math.floor(timeDifference.hours);
let minutes: number = Math.floor(timeDifference.minutes);
let seconds: number = Math.floor(timeDifference.seconds);
let daysDisplay: any = (days.toString().length == 1) ? '0' days : days;
let hoursDisplay: any = (hours.toString().length == 1) ? '0' hours : hours;
let minutesDisplay: any = (minutes.toString().length == 1) ? '0' minutes : minutes;
let secondsDisplay: any = (seconds.toString().length == 1) ? '0' seconds : seconds;
// Create the text to be displayed
let displayText: string = [daysDisplay, 'd ', hoursDisplay, 'h ', minutesDisplay, 'm ', secondsDisplay, 's'].join('');
//context.log(displayText);
// Set Canvas background color
ctx.fillStyle = background;
ctx.fillRect(0, 0, width, height);
// Print Text
ctx.fillStyle = foreground;
ctx.fillText(displayText, (width/2), (height/2));
// Add frame to the GIF
encoder.addFrame(ctx);
// Remove a second in preparation for the next frame
if (seconds < 1) {
if (minutes < 1) {
if (hours < 1) {
timeDifference = timeDifference.minus({days: 1});
}
timeDifference = timeDifference.minus({hours: 1});
}
timeDifference = timeDifference.minus({minutes: 1});
timeDifference = timeDifference.plus({seconds: 59});
}
else {
timeDifference = timeDifference.minus({seconds: 1});
}
}
}
// Encoding complete...
encoder.finish();
const fileData = await buffer(readStream);
//context.log(fileData);
context.res = {
headers: {
"Content-Type": "image/gif"
},
isRaw: true,
status: 200,
body: new Uint8Array(fileData)
};
fs.unlinkSync(filePath)
}
catch(ex: any) {
context.res = {
status: 200,
body: ex.message
};
}
};
export default httpTrigger;
uj5u.com熱心網友回復:
我發現這個問題與 Azure DevOps 管道的問題有關。管道表明它已成功運行,但它從未安裝任何 node_modules。
Function App 上的 node_modules 檔案夾在部署后是空的,導致對 Luxon 之類的東西的參考完全失敗。
我切換到 Github Actions 并且運行良好,因此顯然管道中的某些步驟不正確。
如果您遇到特定功能在本地完美運行但在生產中無法正常運行的問題,請仔細檢查以確保您的 CI/CD 管道正常運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/528892.html
