GoogleCloudPlatform / app-gradle-plugin

The library has moved to https://github.com/GoogleCloudPlatform/appengine-plugins/tree/main/app-gradle-plugin
Apache License 2.0
153 stars 40 forks source link

spring boot's Scheduled task not worked in Google cloud app engine standard environment. #470

Closed pradeepsimba closed 1 year ago

pradeepsimba commented 1 year ago

gradle.build

    buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.cloud.tools:appengine-gradle-plugin:2.4.5'
    }
    }

    plugins {
    id 'application'
    id 'java'
    id 'org.springframework.boot' version '2.5.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    }

    apply plugin: 'com.google.cloud.tools.appengine'

    repositories {
    mavenCentral()
    maven { url "https://packages.jetbrains.team/maven/p/ij/intellij-dependencies" }
    flatDir {
        dirs 'lib'
    }
    google()
    }

    application {
    mainClass = "com.example.demo.DemoApplication"
    applicationName = 'gcloud'
    }

    dependencies {
    testImplementation platform('org.junit:junit-bom:5.9.1')
    testImplementation 'org.junit.jupiter:junit-jupiter'

    implementation 'com.google.firebase:firebase-database:20.0.1'
    implementation 'com.google.firebase:firebase-admin:9.0.0'
    // https://mvnrepository.com/artifact/org.json/json
    implementation group: 'org.json', name: 'json', version: '20230618'

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }

    test {
    useJUnitPlatform()
    }

    task getProjectId {
    doLast {
        def projectId = System.getenv('GOOGLE_CLOUD_PROJECT')
        if (projectId == null) {
        projectId = ServiceOptions.getDefaultProjectId()
        }
        println("Project ID: ${projectId}")
    }
    }

    appengine {
    deploy {

        projectId = 'gcloud config get-value project'.execute().text.trim()
        version = 3
        stopPreviousVersion = true
    }
    }

java file

    package com.example.demo;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;

    @SpringBootApplication
    @RestController
    @EnableScheduling
    public class DemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);
    }

    @Scheduled(fixedRate = 60000)
    public void myScheduledTask() {

        System.out.println("one minite");

    }

    @GetMapping("/gcloud")
    public String hello() {
        return "You are not hacked!";
    }

    }

app.yaml

runtime: java11
env: standard
instance_class: F4_1G
entrypoint: java -cp appengine-web.jar com.example.demo.DemoApplication

If run this code in my local computer it works smoothly.

I deployed my code with this cmd

  gradle appengineDeploy

But, after deploying in gcloud spring boot's Scheduled task not worked.

what I did wrong in this ?

I see console output using this cmd gcloud app logs tail -s default but nothing is printed.

If I go to my website. It comes like this

image

my website also doesn't work.

How do I solve this ?

meltsufin commented 1 year ago

App Engine Standard is a serverless environment which doesn't support background application tasks. For scheduled tasks, use cron.